文件上传通过HTTP PUT请求 [英] File Upload via HTTP PUT Request

查看:549
本文介绍了文件上传通过HTTP PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何想法的产品或图书馆,如Apache Commons FileUpload将处理PUT文件上传?



任何友好的建议或方向将非常赞赏!

全文:

我们正在开始为我们的java实现一个文件上传rest(like)服务webapp,但似乎没有任何简单的解决方案来处理文件上传通过HTTP PUT方法。

我们希望能找到像 Apache Commons FileUpload 项目,但不仅仅是处理基于表单的文件上传的HTML和/或multipart / form-data。

我们非常喜欢FileUpload临时存储文件的能力,当被问到时移动这些文件,然后清理在不再使用临时文件之后。我们也喜欢Spring会自动将MultipartFile列表绑定到我们的命令对象,并且只有当它进入我们的其他基于HTML格式的文件上传控制器时才能使用。



Spring MVC(3.2.3.RELEASE)
  • Tomcat 7

  • 我们正在尝试遵循分层架构(UI,服务/业务逻辑,持久性)


    感谢您的时间!




    下面的url是一个例子,显示了从请求的InputStream。代码完成工作,但它不是生产质量。



    https://boplicity.nl/confluence/display/spring/Using+HTTP+PUT+and+Spring+MVC+to +上传文件






    我们使用下面的curl命令来测试我们的web服务:

      curl -v -k -X PUT --data-binary @c:/java/files/tempfilename.txthttps:// localhost:8443 / api / file / tempfilename.txt 

    然后xwoker给出了下面这个很好的例子: / p>

      curl -v -X PUT -Tmyfilehttp:// localhost:8080 / mytargetfilename 
    解决方案



  • 所有这些都是重写 isMultipart()在定制方法= http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/multipart/MultipartResolver.htmlrel =nofollow noreferrer> MultipartResolver 类。 p>

      import org.apache.commons.fileupload.FileUploadBase; 
    import org.apache.commons.fileupload.servlet.ServletRequestContext;

    import javax.servlet.http.HttpServletRequest;

    public class PostAndPutCommonsMultipartResolver extends CommonsMultipartResolver {

    private static final String POST_METHOD =POST;
    private static final String PUT_METHOD =PUT;

    @Override
    public boolean isMultipart(HttpServletRequest request){

    boolean isMultipartRequest = false;

    如果(请求!= NULL){

    如果(POST_METHOD.equalsIgnoreCase(request.getMethod())|| PUT_METHOD.equalsIgnoreCase(request.getMethod())){

    isMultipartRequest = FileUploadBase.isMultipartContent(new ServletRequestContext(request));
    }
    }

    返回isMultipartRequest;






    $ b

    真正重要的是默认的 MultipartResolver 延伸使得 isMultipart()方法将返回一个真正的POST或PUT请求。



    通常有两个默认MultipartResolver实现: CommonsMultipartResolver (用于Apache Commons FileUpload)和 StandardServletMultipartResolver (与Servlet 3.0+ Part API一起使用)。

    我们使用 Apache Commons FileUpload 扩展了CommonsMultipartResolver类。



    有关于 MultipartResolver的Javadoc 页面,说明如何正确地为您的应用程序定义一个MultipartResolver(重点是我的):

    >

    不存在用于Spring
    DispatcherServlets的默认解析器实现
    ,因为应用程序可能会选择解析其自身的
    多部分请求。要定义一个实现,请在DispatcherServlet的应用程序
    上下文中创建一个ID为multipartResolver的bean
    。这样的解析器会被应用到由
    DispatcherServlet处理的所有请求中。

    对于一个xml配置的应用程序来说,如下所示:

     < bean id =multipartResolverclass =< package>。< name> .PostAndPutCommonsMultipartResolver /> 

    对于配置了注解的应用程序,它看起来接近于以下内容:

      @Bean(名称= multipartResolver)
    公共是CommonsMultipartResolver createMultipartResolver(){
    返回新PostAndPutCommonsMultipartResolver();
    }

    有关MultipartResolver的注释配置的更多信息


    Does anyone have any idea of any products or libraries like Apache Commons FileUpload that will deal with PUT file uploads?

    Any friendly advice or direction would be very much appreciated!

    Full Story:

    We are starting to implement a file upload rest(like) service for our java webapp, but there doesn't seem to be any 'easy' solutions for dealing with file uploads via the HTTP PUT method.

    We are hoping to find a library like the Apache Commons FileUpload project, but something that doesn't only deal with "Form-based File Upload in HTML" and/or "multipart/form-data".

    We really like FileUpload's ability to store files temporarily, move those files when asked, and then clean up the temporary files after they are no longer used. We also like the fact that Spring will automajically bind the MultipartFile List to our command object and its just available for us to use when it gets into our other html form based file upload controllers.

    Full Stack Background:

    • Spring MVC (3.2.3.RELEASE)
    • Tomcat 7
    • We are trying to follow a layered architecture (UI, services/business logic, persistence)

    Thank you for your time!


    The following url is an example that shows the ability to upload a file from the request's InputStream. The code gets the work done but it isn't quite production quality.

    https://boplicity.nl/confluence/display/spring/Using+HTTP+PUT+and+Spring+MVC+to+upload+files


    We are using the following curl command to test our webservice:

    curl -v -k -X PUT --data-binary @"c:/java/files/tempfilename.txt" https://localhost:8443/api/file/tempfilename.txt
    

    xwoker then gave the following nice curl example:

    curl -v -X PUT -T "myfile" http://localhost:8080/mytargetfilename
    

    解决方案

    It is quite easy to get spring to respond correctly to a File Upload request for a HTTP PUT Method.

    All it takes is overriding the isMultipart() method in a customized MultipartResolver class.

    import org.apache.commons.fileupload.FileUploadBase;
    import org.apache.commons.fileupload.servlet.ServletRequestContext;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class PostAndPutCommonsMultipartResolver extends CommonsMultipartResolver {
    
        private static final String POST_METHOD = "POST";
        private static final String PUT_METHOD = "PUT";
    
        @Override
        public boolean isMultipart(HttpServletRequest request) {
    
            boolean isMultipartRequest = false;
    
            if (request != null) {
    
                if (POST_METHOD.equalsIgnoreCase(request.getMethod()) || PUT_METHOD.equalsIgnoreCase(request.getMethod())) {
    
                    isMultipartRequest = FileUploadBase.isMultipartContent(new ServletRequestContext(request));
                }
            }
    
            return isMultipartRequest;
        }
    }
    

    What is really important is that the default MultipartResolver is extended so that the isMultipart() method will return a true for either a POST or PUT request.

    In general there are two default MultipartResolver implementations: CommonsMultipartResolver (used with Apache Commons FileUpload) and StandardServletMultipartResolver (used with Servlet 3.0+ Part API).

    Since we are using Apache Commons FileUpload we extended the CommonsMultipartResolver class.

    There is documentation on the MultipartResolver's Javadoc page that explains how to properly define a customized MultipartResolver for your application (emphasis is mine):

    There is no default resolver implementation used for Spring DispatcherServlets, as an application might choose to parse its multipart requests itself. To define an implementation, create a bean with the id "multipartResolver" in a DispatcherServlet's application context. Such a resolver gets applied to all requests handled by that DispatcherServlet.

    For an xml configured application it will look close to the following:

    <bean id="multipartResolver" class="<package>.<name>.PostAndPutCommonsMultipartResolver"/>
    

    For an annotation configured application it will look close to the following:

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver createMultipartResolver() {
        return new PostAndPutCommonsMultipartResolver();
    }
    

    More information on Annotation configuration of MultipartResolver.

    这篇关于文件上传通过HTTP PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆