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

读过程:

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

写过程:

  • 将数据先写入一个字节数组中(可能会导致内存溢出)
相关推荐
Moment14 分钟前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
Cobyte1 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
程序员侠客行2 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
Honmaple2 小时前
QMD (Quarto Markdown) 搭建与使用指南
后端
PP东2 小时前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable
invicinble2 小时前
springboot的核心实现机制原理
java·spring boot·后端
全栈老石3 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
space62123273 小时前
在SpringBoot项目中集成MongoDB
spring boot·后端·mongodb
Tony Bai4 小时前
再见,丑陋的 container/heap!Go 泛型堆 heap/v2 提案解析
开发语言·后端·golang
寻找奶酪的mouse4 小时前
30岁技术人对职业和生活的思考
前端·后端·年终总结