• SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver


    一、什么是multipart

    The Spittr application calls for file uploads in two places. When a new user registers with the application, you’d like them to be able to provide a picture to associate with their profile. And when a user posts a new Spittle , they may want to upload a photo to go along with their message.The request resulting from a typical form submission is simple and takes the form of multiple name-value pairs separated by ampersands. For example, when submitting the registration form from the Spittr application, the request might look like this:

    firstName=Charles&lastName=Xavier&email=professorx%40xmen.org
    &username=professorx&password=letmein01

    Although this encoding scheme is simple and sufficient for typical text-based form submissions, it isn’t robust enough to carry binary data such as an uploaded image. In contrast, multipart form data breaks a form into individual parts, with one part per field. Each part can have its own type. Typical form fields have textual data in their parts, but when something is being uploaded, the part can be binary, as shown in the following multipart request body:

    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="firstName"
    Charles
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="lastName"
    Xavier
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="email"
    charles@xmen.com
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="username"
    professorx
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="password"
    letmein01
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW
    Content-Disposition: form-data; name="profilePicture"; filename="me.jpg"
    Content-Type: image/jpeg
    [[ Binary image data goes here ]]
    ------WebKitFormBoundaryqgkaBn8IHJCuNmiW--

    In this multipart request, the profilePicture part is noticeably different from the other parts. Among other things, it has its own Content-Type header indicating that it’s a JPEG image. And although it may not be obvious, the body of the profile-Picture part is binary data instead of simple text.

    二、在Spring中上传文件有多少种方法

    DispatcherServlet doesn’t implement any logic for parsing the data in a multipart request. Instead, it delegates to an implementation of Spring’s MultipartResolver strategy interface to resolve the content in a multipart request. Since Spring 3.1,Spring comes with two out-of-the-box implementations of MultipartResolver to choose from:

     CommonsMultipartResolver —Resolves multipart requests using Jakarta Commons FileUpload
     StandardServletMultipartResolver —Relies on Servlet 3.0 support for multipart requests (since Spring 3.1)

    三、配置StandardServletMultipartResolver 

    1.Java

    (1)

    @Bean
    public MultipartResolver multipartResolver() throws IOException {
        return new StandardServletMultipartResolver();
    }

      

    (2)设置上传条件:If you’re configuring DispatcherServlet in a servlet initializer class that implements WebApplicationInitializer , you can configure multipart details by calling setMultipartConfig() on the servlet registration, passing an instance of MultipartConfigElement .

    DispatcherServlet ds = new DispatcherServlet();
    Dynamic registration = context.addServlet("appServlet", ds);
    registration.addMapping("/");
    registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads"));

    (3)设置上传条件:If you’ve configured DispatcherServlet in a servlet initializer class that extends AbstractAnnotationConfigDispatcherServletInitializer or AbstractDispatcherServletInitializer 

    @Override
    protected void customizeRegistration(Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads"));
            
    }

    (4)设置其他上传条件

     The maximum size (in bytes) of any file uploaded. By default there is no limit.
     The maximum size (in bytes) of the entire multipart request, regardless of how many parts or how big any of the parts are. By default there is no limit.
     The maximum size (in bytes) of a file that can be uploaded without being written to the temporary location. The default is 0, meaning that all uploaded files will be written to disk.
    For example, suppose you want to limit files to no more than 2 MB , to limit the entire request to no more than 4 MB , and to write all files to disk. The following use of MultipartConfigElement sets those thresholds:

    1 @Override
    2 protected void customizeRegistration(Dynamic registration) {
    3     registration.setMultipartConfig(new MultipartConfigElement("/tmp/spittr/uploads",2097152, 4194304, 0));
    4 }

      

    2.在xml中设置上传约束 

     1 <servlet>
     2     <servlet-name>appServlet</servlet-name>
     3     <servlet-class>
     4         org.springframework.web.servlet.DispatcherServlet
     5     </servlet-class>
     6     <load-on-startup>1</load-on-startup>
     7     <multipart-config>
     8         <location>/tmp/spittr/uploads</location>
     9         <max-file-size>2097152</max-file-size>
    10         <max-request-size>4194304</max-request-size>
    11     </multipart-config>
    12 </servlet>

      

    四、配置CommonsMultipartResolver 

     1.Java

    (1)

    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

    (2)设置约束

    1 @Bean
    2 public MultipartResolver multipartResolver() throws IOException {
    3     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    4     multipartResolver.setUploadTempDir(new FileSystemResource("/tmp/spittr/uploads"));
    5     return multipartResolver;
    6 }

    (3)设置约束

    1 @Bean
    2 public MultipartResolver multipartResolver() throws IOException {
    3     CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    4     multipartResolver.setUploadTempDir(new FileSystemResource("/tmp/spittr/uploads"));
    5     multipartResolver.setMaxUploadSize(2097152);
    6     multipartResolver.setMaxInMemorySize(0);
    7     return multipartResolver;
    8 }

    Here you’re setting the maximum file size to 2 MB and the maximum in-memory size to 0 bytes. These two properties directly correspond to MultipartConfigElement ’s second and fourth constructor arguments, indicating that no files larger than 2 MB may be uploaded and that all files will be written to disk no matter what size. Unlike MultipartConfigElement , however, there’s no way to specify the maximum multipart request size.

  • 相关阅读:
    Mac下安装Navicat12之破解
    重新认识spring
    冒泡排序(二)
    算法入门篇(一)
    elasticsearch复杂查询-----2
    idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?
    idea 多模块项目依赖父工程class找不到问题
    apache-maven-3.3.9集成apache-tomcat-7.0.72实现热部署配置细节
    WebService CXF调试常见报错及解决方案
    通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案
  • 原文地址:https://www.cnblogs.com/shamgod/p/5245188.html
Copyright © 2020-2023  润新知