You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用REST API上传Multipart文件时遇ByteArrayInputStream序列化错误

问题:封装MultipartFile上传REST API时出现序列化错误

调用REST API上传文件,将MultipartFile封装进UploadFileObject后发送请求,触发错误:No serializer found for class java.io.ByteArrayInputStream。

相关代码如下:

public class UploadFileObject implements Serializable{

  MultipartFile file;

//getters and setters
}

public void uploadFile() {

Path path = Paths.get("C:/Accounts.txt");
byte[] content = null;

try{
    content =  Files.readAllBytes(path);   
} catch(final IOException e){
}

MultipartFile file = new MockMultipartFile("ABC.txt","ABC.txt","text/plain", content);
    
UploadFileObject obj = new UploadFileObject();
obj.setFile(file);

HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set(MediaType.MULTIPART_FORM_DATA);
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.set("Content-type", MediaType.APPLICATION_JSON_VALUE);

HttpEntity<?> requestEntity = new HttpEntity<>(obj, headers);

String result = getRestTemplate().postForEntity("url", requestEntity, String.class);

}

已配置application.yml:

spring.serialization.fail-on-empty-beans: false

pom.xml引入了2.12.3版本的jackson系列依赖。


错误原因

  1. 请求格式冲突:同时在Header中设置了MULTIPART_FORM_DATA和APPLICATION_JSON两种Content-Type,RestTemplate会优先按JSON序列化请求体,但MultipartFile内部的ByteArrayInputStream无法被Jackson序列化,导致报错。
  2. 请求体封装方式错误:上传文件需遵循multipart/form-data规范提交,不能将MultipartFile封装成普通Java对象用JSON格式发送。

解决方案

改用MultiValueMap构建符合multipart/form-data规范的请求体,统一配置请求头:

修改后的代码:

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.Collections;

public void uploadFile() {
    Path path = Paths.get("C:/Accounts.txt");
    byte[] content = null;
    try {
        content = Files.readAllBytes(path);
    } catch (final IOException e) {
        // 建议添加异常处理逻辑,如打印日志或抛出业务异常
        e.printStackTrace();
    }

    MultipartFile file = new MockMultipartFile("file", "ABC.txt", "text/plain", content);

    // 构建multipart请求体,支持同时传递文件和其他参数
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", file);
    // 若需附加其他参数,直接add即可:body.add("paramKey", paramValue);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

    ResponseEntity<String> result = getRestTemplate().postForEntity("url", requestEntity, String.class);
    // 可根据响应结果做后续处理
}

关键修改点

  • 使用LinkedMultiValueMap存储文件及参数,匹配multipart/form-data的结构要求。
  • 统一设置Header的Content-Type为MULTIPART_FORM_DATA,移除冲突的JSON格式配置。
  • 正确构建HttpEntity,将MultiValueMap作为请求体传递。

内容的提问来源于stack exchange,提问作者Nishant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:05:36