spring boot-MultipartFile 机制

spring boot-MultipartFile 机制

spring boot MultipartFile 解析过程

bash 复制代码
sequenceDiagram  
participant Client as 客户端  
participant ServletContainer as Servlet容器  
participant Spring as Spring MVC  
  
Client->>ServletContainer: 发送multipart/form-data请求  
ServletContainer->>ServletContainer: 启动HTTP协议解析  
ServletContainer->>ServletContainer: 根据Content-Type识别multipart  
ServletContainer->>ServletContainer: 创建临时存储结构(内存/磁盘)  
ServletContainer->>Spring: 将解析后的数据封装为Request对象  
Spring->>Spring: 触发MultipartResolver解析  

只有在 MultipartFile.getBytes() 方法才会将文件数据加载到内存!!

上传时配置设置

yaml 复制代码
spring:  
servlet:  
multipart:  
enabled: true  
file-size-threshold: 1MB # 1MB 以下用内存,以上用磁盘  
max-file-size: 5GB # 单个文件最大  
max-request-size: 10GB # 整个请求最大  
location: /data/tmp # 指定专用临时目录  

问题

在使用 Feign 上传文件时,如果上传大文件会报错, Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Required array length 2147483639 + 9 is too large

所以使用 RestTemplate 来实现

调用方

java 复制代码
/**  
* 使用@LoadBalanced注解不能进行流式上传  
* @return  
*/  
public RestTemplate getUploadRestTemplate() {  
    // 使用 Apache HttpClient 作为底层实现,使用 流式上传  
    CloseableHttpClient httpClient = HttpClients.custom()  
    .setMaxConnTotal(100) // 最大连接数  
    .setMaxConnPerRoute(20) // 每个路由的最大连接数  
    .build();  

    HttpComponentsClientHttpRequestFactory factory =  
    new HttpComponentsClientHttpRequestFactory(httpClient);  
    factory.setBufferRequestBody(false);  
    factory.setConnectTimeout(30_000); // 连接超时 30s  
    factory.setReadTimeout(300_000); // 读取超时 5min(大文件需延长)  
    return new RestTemplate(factory);  
}  
  
  
@PostMapping(value = "/file/upload")  
public String uploadStream(@RequestParam("file") MultipartFile file) throws IOException {  
    // 3. 构建 Multipart 请求体  
    // 2. 包装文件流为 Resource(关键:实现 contentLength() 返回 -1)  
    int bufSize = 64*1024; //默认8kb  
        Resource resource = new InputStreamResource(new BufferedInputStream(file.getInputStream(),bufSize)) {  
        @Override  
        public long contentLength() {  
        return -1; // 触发分块传输  
        }  

        @Override  
        public String getFilename() {  
        return file.getName(); // 确保服务器能获取文件名  
        }  
    };  

    file.transferTo();  

    // 4. 设置请求头  
    HttpHeaders headers = new HttpHeaders();  
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
    ResponseEntity<String> response =  
    //手动进行负载  
    getUploadRestTemplate().exchange("http://localhost:8081/upload-file4?name="+file.getOriginalFilename(),  
    HttpMethod.POST,  
    new HttpEntity<>(resource,headers), String.class);  

    System.out.println(JSON.toJSONString(response));  

    return "success";  
}  

这里比较坑的是加了@LoadBalanced 注解之后的RestTemplate ,会缓存 请求数据【详见InterceptingClientHttpRequest】,还是会报内存溢出,所以就不能加,如果要在微服务系统使用,要先自己使用负载均衡这套

服务器方

java 复制代码
  
//这样直接从request 获取文件流,就不解析MultipartFile,效率也要好点  
@PostMapping(value = "/upload-file4", consumes = "application/octet-stream")  
public String uploadStream3(HttpServletRequest request,@RequestParam("name") String name) throws Exception {  
    try (InputStream inputStream = request.getInputStream()) {  
    Files.copy(inputStream, Paths.get("d:/" + System.currentTimeMillis() + "_" + name));  
    return "Upload success!";  
    }  
}  

spring 处理 application/octet-stream 资源类型

spring boot 在处理 application/octet-stream 请求类型时,使用 ResourceHttpMessageConverter 类处理

ResourceHttpMessageConverter

读过程:

  • 支持流式读
  • 支持将数据全部加载到字节数组中(可能会导致内存溢出)

写过程:

  • 将数据先写入一个字节数组中(可能会导致内存溢出)
相关推荐
Victor35641 分钟前
MySQL(181)如何在MySQL中管理大数据量的表?
后端
Victor3561 小时前
MySQL(182) 如何进行MySQL的分库分表?
后端
你的人类朋友8 小时前
✨什么是SaaS?什么是多租户?
后端·架构·设计
M1A18 小时前
全球语言无障碍:Unicode标准解读与技术演进史
后端
无限大68 小时前
多数元素问题:从暴力美学到摩尔投票神仙解法
后端
无限大68 小时前
《计算机“十万个为什么”》之 面向对象 vs 面向过程:编程世界的积木与流水线
后端
洛可可白8 小时前
Spring Boot 应用结合 Knife4j 进行 API 分组授权管理配置
java·spring boot·后端
Livingbody9 小时前
基于ERNIE-4.5-0.3B医疗领域大模型一站式分布式训练部署
后端
程序员爱钓鱼9 小时前
Go语言实战案例:使用sync.Mutex实现资源加锁
后端·go·trae
程序员爱钓鱼9 小时前
Go语言实战案例:使用context控制协程取消
后端·google·trae