spring boot --- 文件上传

在spring  mvc 中,对文件上传做了简化,但是在spring  boot 中更对 文件上传做了简化,在java 中文件上传共需要涉及到两个组件,一个是CommonsMultipartReslover,另一个是StandardServletMultipartResolver,StandardServletMultipartResolver是在Servlet3.0来处理的,tomcat7.0就默认支持Servlet3.0,所以可以直接使用,但是在spring boot 2.0.4 内嵌的 tomcat 是 8.5.32版本,所以可以直接使用,而spring boot 提供了文件上传的自动化配置类MultipartAutoConfiguration中,默认也是采用的StandardServletNulipartResolver 。源码如下

如果没有提供MultipartResolver,那么就会默认采用MultipartResolver 中的 StandardServletMultipartResolver ,所以在使用 spring boot 完成文件上传,基本上可以说是零配置

文件上传案例:

 html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFile" value="请选择文件">
        <input type="submit" value="上传">
    </form>
</body>
</html>

response 请求

@RestController
public class FileUploadController {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

    @PostMapping("/upload")
    public String upload(MultipartFile  uploadFile, HttpServletRequest request) throws FileNotFoundException {
        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = this.format.format(new Date());
        File file = new File(realPath + format);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        try {
            
                String filename = uploadFile .getOriginalFilename();
                String substring = filename.substring(filename.lastIndexOf("."), filename.length());
                String string = UUID.randomUUID().toString() + substring;
                uploadFile.transferTo(new File(format, string));
                String scheme = request.getScheme();
                System.out.println("scheme : +scheme");
                String serverName = request.getServerName();
                System.out.println("serverName " + serverName);
                return scheme + "://" + serverName + ":" + request.getServerPort() + "/uploadFile" + file + substring;
            
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
        return "";
    }
}

该代码在使用的时候,在获取当前项目路径的时候,会出现路径找不到的问题,对此我没有找到解决的方案,有知道的或者解决的还希望下方留言,

在开发的时候,会对文件上传进行配置,下面是文件的一些简单的配置

# 表示 是否开启文件上传
spring.servlet.multipart.enabled=true
# 表示写入磁盘的阈值
spring.servlet.multipart.file-size-threshold=0
# 表示文件保存的地址
spring.servlet.multipart.location= D:\\NewSpringCloudCode
# 表示多文件上海窜的总大小
spring.servlet.multipart.max-request-size= 10mb
# 表示单个文件上传的总大小
spring.servlet.multipart.max-file-size= 1MB 
# 表示 文件是否延迟解析
spring.servlet.multipart.resolve-lazily=false

上传多文件

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFile" value="请选择文件" multiple>
        <input type="submit" value="上传">
    </form>
</body>
</html>

后端处理

@RestController
public class FileUploadController {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

    @PostMapping("/upload")
    public String upload(MultipartFile[] uploadFile, HttpServletRequest request) throws FileNotFoundException {
        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = this.format.format(new Date());
        File file = new File(realPath + format);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        try {
            for (int i = 0; i < uploadFile.length; i++) {
                String filename = uploadFile[i].getOriginalFilename();
                String substring = filename.substring(filename.lastIndexOf("."), filename.length());
                String string = UUID.randomUUID().toString() + substring;
                uploadFile[i].transferTo(new File(format, string));
                String scheme = request.getScheme();
                System.out.println("scheme : +scheme");
                String serverName = request.getServerName();
                System.out.println("serverName " + serverName);
                return scheme + "://" + serverName + ":" + request.getServerPort() + "/uploadFile" + file + substring;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "文件上传失败";
        }
        return "";
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值