首页 专题 文章 代码 归档
SpringMVC文件上传
2020.02.03 16:04 2020.02.03 16:24

1. 文件上传

1.1. 需要的jar包

  • commons-io
  • commons-fileupload
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1.2. 表单

<form action="${pageContext.request.contextPath}/upload/test" method="post" enctype="multipart/form-data">
    File:<input type="file" name="source"><br>
    <input type="submit" value="提交">
</form>

1.3. 上传解析器

<!--上传解析器
    id必须为multipartResolver
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--限制传递大小:bytes-->
    <property name="maxUploadSize" value="10485760"/>
</bean>

1.4. 测试

package com.misiai.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @RequestMapping("/test")
    public String test(MultipartFile source) {
        /*细节:
        * 变量名source必须和表单中的name属性值一样。
        * */
        // 存入D盘
        String originalFilename = source.getOriginalFilename();

        try {
            source.transferTo(new File("D:\\" + originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }
}

解释:

1、MultipartFile source 变量名必须和表单中的name属性值一样:

<form action="${pageContext.request.contextPath}/upload/test" method="post" enctype="multipart/form-data">
    File:<input type="file" name="source"><br>
    <!-- 必须和这里的source一样,不然会空指针异常的 -->
    <input type="submit" value="提交">
</form>

1.5. 一些常见普通处理

@RequestMapping("/test")
public String test(MultipartFile source, HttpSession session) {
    /*细节:
     * 变量名source必须和表单中的name属性值一样。
     * */
    // 原始名称
    String originalFilename = source.getOriginalFilename();
    // 定义全局唯一名(伪):
    String unique = UUID.randomUUID().toString();
    // 文件后缀
    String extension = FilenameUtils.getExtension(originalFilename);
    // 全局文件名:
    String uniqueFileName = unique + "." + extension;
    System.out.println("uniqueFileName = " + uniqueFileName);

    // 文件类型:
    String contentType = source.getContentType();
    System.out.println("contentType = " + contentType);

    // 获得磁盘,在webapp目录下有一个upload目录,且初始不要为空,不然编译时要背忽略
    String real_path = session.getServletContext().getRealPath("/upload");
    System.out.println("real_path = " + real_path);

    //上传文件,存入磁盘中
    try {
        source.transferTo(new File(real_path + "\\" + uniqueFileName));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "success";
}
本节阅读完毕! (分享
二维码图片 扫描关注我们哟