优秀的编程知识分享平台

网站首页 > 技术文章 正文

spring boot-MultipartFile 机制_spring boot multipartfile为null

nanyue 2025-08-31 06:57:20 技术文章 6 ℃

spring boot-MultipartFile 机制

spring boot MultipartFile 解析过程

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() 方法才会将文件数据加载到内存!!

上传时配置设置

spring:
servlet:
multipart:
enabled: true
file-size-threshold: 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 来实现

调用方

public RestTemplate getUploadRestTemplate() {

    CloseableHttpClient httpClient = HttpClients.custom()
    .setMaxConnTotal(100)
    .setMaxConnPerRoute(20)
    .build();

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); factory.setBufferRequestBody(false); factory.setConnectTimeout(30_000); factory.setReadTimeout(300_000); return new RestTemplate(factory); } @PostMapping(value = "/file/upload") public String uploadStream(@RequestParam("file") MultipartFile file) throws IOException { int bufSize = 64*1024; Resource resource = new InputStreamResource(new BufferedInputStream(file.getInputStream(),bufSize)) { @Override public long contentLength() { return -1; }

@Override public String getFilename() { return file.getName(); } };

file.transferTo();

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】,还是会报内存溢出,所以就不能加,如果要在微服务系统使用,要先自己使用负载均衡这套

服务器方

@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

读过程:

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

写过程:

  • 将数据先写入一个字节数组中(可能会导致内存溢出)
最近发表
标签列表