Spring多文件上传multipart配置

SpringMVC多文件上传:multipart配置

1.首先是项目引入依赖jar包:

这里用到的是commons包。个人认为用起来比较好用的。
pom.xml

        <!-- 上传组件包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>

2.配置Spring支持multipart文件上传:

spring的xml经典配置文件:
配置 multipartResolver

    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />
    </bean>

3.Controller处理映射

Controller处理映射中 比较关键的部分。

    @RequestMapping(consumes = "multipart/form-data",value = "/awards-create", method = RequestMethod.POST)
    public ModelAndView awardsCreate(HttpServletRequest request,ModelAndView mav,
            @RequestParam("pictureFile1") MultipartFile pictureFile1) throws IOException {  
        String rootPath = System.getProperty("catalina.home") + "/webapps_data";

        if(!(new File(rootPath).exists())) {
            //   ~/tomcat/webapps_data/    如果目录不存在,则创建之
            new File(rootPath).mkdir();
        }

        //获取类型。如.png   .jpg
        String file1Type = pictureFile1.getOriginalFilename().substring(
                pictureFile1.getOriginalFilename().lastIndexOf("."));

        String file1Path = rootPath + "/" + thesis.getId() + file1Type;

        //利用MultipartFile提供的方法,直接保存到文件,详见源码
        pictureFile1.transferTo(new File(file1Path));

至此,我们已经配置OK,然后前台的写法:

4.前台上传文件:

<label>选择文件:</label>
<input type="file" id="pictureFile1" name="pictureFile1" accept="image/*" class="required" aria-required="true">

最后,我们可以来看一下multipart请求与普通请求的区别:

Request Header


Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.9
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:66719
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryqavwlFLAAWz5R40N
Cookie:JSESSIONID=DC9979A274570B2891E7179FFE89EB9B
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/****/user/awards-create
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36

Request Payload

------WebKitFormBoundaryqavwlFLAAWz5R40N
Content-Disposition: form-data; name="name"

123213
------WebKitFormBoundaryqavwlFLAAWz5R40N
Content-Disposition: form-data; name="publishDate"

2018-04-06
------WebKitFormBoundaryqavwlFLAAWz5R40N
Content-Disposition: form-data; name="publishRange"

国内外公开发行
------WebKitFormBoundaryqavwlFLAAWz5R40N
Content-Disposition: form-data; name="picture01"; filename="2018-04-09 16-45-03 的屏幕截图.png"
Content-Type: image/png


------WebKitFormBoundaryqavwlFLAAWz5R40N
Content-Disposition: form-data; name="picture02"; filename="2018-04-09 16-32-30 的屏幕截图.png"
Content-Type: image/png


------WebKitFormBoundaryqavwlFLAAWz5R40N--
    @RequestMapping(consumes = "multipart/form-data",value = "/awards-create", method = RequestMethod.POST)
    public ModelAndView awardsCreate(HttpServletRequest request,ModelAndView mav,
            @RequestParam("pictureFile1") MultipartFile pictureFile1) throws IOException {  

        ......

        System.out.println("|||||||=====================" + pictureFile1.getContentType());
        System.out.println("|||||||=====================" + pictureFile1.getName());
        System.out.println("|||||||=====================" + pictureFile1.getOriginalFilename());

        int l = pictureFile1.getOriginalFilename().length();
        System.out.println("|||||||=====================" + l);

        int i = pictureFile1.getOriginalFilename().lastIndexOf(".");
        System.out.println("|||||||=====================" + i);
        System.out.println("|||||||=====================" +
                pictureFile1.getOriginalFilename().charAt(l-1));
        System.out.println("|||||||=====================" +
                pictureFile1.getOriginalFilename().substring(i-1));
|||||||=====================image/png
|||||||=====================pictureFile1
|||||||=====================2018-04-14 09-54-19 的屏幕截图.png
|||||||=====================29
|||||||=====================25
|||||||=====================g
2018-04-14 10:08:30 -45738 [http-bio-8080-exec-63] DEBUG   - Multipart file 'pictureFile1' with original filename [2018-04-14 09-54-19 的屏幕截图.png], stored at [/home/gaoyisheng/devel/tomcat/apache-tomcat-7.0.82/work/Catalina/localhost/trans/upload_bae19b7b_9b04_48b6_aad4_51f530a4ae39_00000043.tmp]: moved to [/home/gaoyisheng/devel/tomcat/tomcat/webapps_data/306_1.png]

参考资料:

<< Spring in action >> 4th edition

猜你喜欢

转载自blog.csdn.net/timo1160139211/article/details/79919704