SpringBoot整合WEB开发--(三)文件上传

2020-02-16 16:03:19来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

SpringBoot整合WEB开发--(三)文件上传

文件上传:

  Java中文件上传一共涉及到两个组件,CommonsMultipartResolver和StandardServletMultipartResolver,其中CommonsMultipartResolver使用commons-fileupload来处理multipart请求,StandardServletMultipartResolver基于Servlet3.0来处理multipart请求的。如果使用StandardServletMultipartResolver则不需要额外的jar包。Tomcat7.0开始支持Servlet3.0,SpringBoot2.0.4内置Tomcat是8.5.32,所以可以直接使用StandardServletMultipartResolver。

  SpringBoot提供的文件上传自动化配置类MultipartAutoConfiguration中,

@ConditionalOnMissingBean,如果没有提供MultipartResolver,那么默认StandardServletMultipartResolver
 @ConditionalOnMissingBean({MultipartResolver.class})
    public StandardServletMultipartResolver multipartResolver() {
        StandardServletMultipartResolver multipartResolver = new StandardServletMultipartResolver();
        multipartResolver.setResolveLazily(this.multipartProperties.isResolveLazily());
        return multipartResolver;
    }

 

 

1.单文件上传:

  创建upload.html文件

 

 controller:

  @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        System.out.println(realPath);
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
            return filePath;
        } catch (IOException e) {
//            e.printStackTrace();
        }
        return "上传失败!";
    }

在application.properties中配置对文件上传的更多细节

#开启文件上传支持
spring.servlet.multipart.enabled=true #文件写入磁盘阈值
spring.servlet.multipart.file
-size-threshold=0 #上传文件临时保存位置
spring.servlet.multipart.location
=E:\\temp #上传单个文件最大大小1MB
spring.servlet.multipart.max
-file-size=1MB #多文件上传时总大小100MB
spring.servlet.multipart.max
-request-size=100MB
#文件是否延迟解析false spring.servlet.multipart.resolve
-lazily=false

 

 

2.多文件上传

  upload.html

<form action="/uploads" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadFiles" value="请选择文件" multiple>
    <input type="submit" value="上传">
</form>

controller:

 @PostMapping("/uploads")
    public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) {
        for (MultipartFile uploadFile : uploadFiles) {
            String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
            System.out.println(realPath);
            String format = sdf.format(new Date());
            File folder = new File(realPath + format);
            if (!folder.isDirectory()) {
                folder.mkdirs();
            }
            String oldName = uploadFile.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            try {
                uploadFile.transferTo(new File(folder, newName));
                String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
                return filePath;
                System.out.println(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "上传失败!";
    }

 


原文链接:https://www.cnblogs.com/crazy-lc/p/12316964.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Kafka系列2:深入理解Kafka生产者

下一篇:SpringBoot整合WEB开发--(四)@ControllerAdvice