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

喜欢而已 提交于 2020-02-16 16:30:59

文件上传:

  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
#上传单个文件最大大小1MBspring.servlet.multipart.max-file-size=1MB
#多文件上传时总大小100MBspring.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 "上传失败!";
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!