springboot 几种文件上传方式

  基于springboot 2.3.4.RELEASE。 pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>            

  提交配置属注意项:1、POST提交; 2、enctype="multipart/form-data"

 

第一种:使用spring的 MultipartFile 接口来接收

<form action="/file/upload1" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
    @RequestMapping(value = " file/upload1")
    public String  fileUpload1(@RequestParam("file") MultipartFile file) throws IOException {
        String path=uploadPath+ File.separator + "upload1" + File.separator +file.getOriginalFilename();        

        File destination = new File(path);
        if (!destination.getParentFile().exists()){
            FileUtils.forceMkdirParent(destination); //使用commons-io的工具类
        }
        file.transferTo(destination);
        return "success:" + path;
    }

  默认文件大小在1MB, 可以使用配置文件application.yml进行调节:

spring:
  servlet:
    multipart:
      max-file-size: 10MB  #每个文件大小
      max-request-size: 30MB  #请求上传文件总量

 

第二种,使用具体实现类CommonsMultipartFile。 如果参数过多也可以封装成对象进行传递

public class UploadParam {
    private int tradeDate;
    private CommonsMultipartFile file;
    //省略set get
}

前端html的写法与第一种无异。

    @RequestMapping("upload2")
    public String  fileUpload2(UploadParam param) throws IOException {
        String file = uploadPath + File.separator + "upload2" + File.separator + param.getFile().getOriginalFilename();
        File destination = new File(file);
        if (!destination.getParentFile().exists()){
            FileUtils.forceMkdirParent(destination);
        }
        param.getFile().transferTo(destination);
        return "success: " + file;
    }

 

第三种,多文件上传。参数属性名一样即可。

public class MultiFileParam {

    private int tradeDate;
    private List<CommonsMultipartFile> file;
    //省略set get 
}
    @RequestMapping("upload3")
    public String  fileUpload3(MultiFileParam param) throws IOException {
        String file = uploadPath + File.separator + "upload3" ;
        File destination = new File(file);
        if (!destination.exists()){
            FileUtils.forceMkdirParent(destination);
        }

        param.getFile().forEach(f->{
            File dest = new File(file + File.separator + f.getOriginalFilename());
            try {
                f.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        return "success: " + file;
    }

 

<form action="/upload3" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>

 

常见异常及处理:

1、fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

超过默认大小。通过配置文件调节即可。

spring.servlet.multipart.max-file-size=30Mb

spring.servlet.multipart.max-request-size=30Mb

 

2、java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException。缺少依赖包

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
        </dependency>

 

3、'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file': no matching editors or conversion strategy found] 使用实现类,必需实例加载

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxInMemorySize(10*1024*1024);
        multipartResolver.setMaxUploadSize(30*1024*1024);

        return multipartResolver;
    }

 

posted @ 2020-12-02 00:03  落孤秋叶  阅读(3976)  评论(0编辑  收藏  举报