You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

通过网关使用RestTemplate调用文件上传API时抛出IO异常求助

通过网关使用RestTemplate调用文件上传API时抛出IO异常求助

大家好,我碰到一个棘手的问题:通过网关用RestTemplate调用文件上传API时,总会抛出IO异常,哪怕是很小的文件也不行。直接调用API服务本身是正常的,但经过网关就报错,想请各位帮忙分析下问题出在哪。

错误堆栈信息

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Connection reset by peer] with root cause 
java.io.IOException: Connection reset by peer 
at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.8.0_333] 
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39) ~[na:1.8.0_333] 
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[na:1.8.0_333] 
at sun.nio.ch.IOUtil.read(IOUtil.java:197) ~[na:1.8.0_333] 
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:378) ~[na:1.8.0_333] 
... ... ... 
Caused by: java.io.IOException: Broken pipe 
at sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[na:1.8.0_333] 
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47) ~[na:1.8.0_333] 
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) ~[na:1.8.0_333] 
at sun.nio.ch.IOUtil.write(IOUtil.java:65) ~[na:1.8.0_333] 
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:469) ~[na:1.8.0_333]

RestTemplate调用代码

MultiValueMap<String, Object> dataMap = new LinkedMultiValueMap<>();
dataMap.add("files", new FileSystemResource(filePath));
dataMap.add("presetfolder", JSON.parse(""));

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("accept", "*/*");
headers.add("connection", "Keep-Alive");
headers.add("Accept-Charset", "utf-8");
headers.add("X-Token", xToken);
headers.add("Strapp", "");

HttpEntity<MultiValueMap<String, Object>> requestParams = new HttpEntity<>(dataMap, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Object> response = restTemplate.postForEntity(url, requestParams, Object.class);
Object body = response.getBody();
result = JSONObject.parseObject(JSONObject.toJSONString(body), Result.class);

网关配置(application.yml)

spring:
  application:
    name: 
  servlet:
    multipart:
      max-file-size: 1024MB
      max-request-size: 1024MB
      enabled: true
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 2000000
ribbon:
  ReadTimeout: 2000000
  ConnectTimeout: 2000000
eureka:
  enabled: true
zuul:
  host:
    socket-timeout-millis: 2000000
    connect-timeout-millis: 2000000
  routes:
    fileweb:
      path: /fileweb/**
      serviceId: fileweb

环境与已尝试的操作

  • 网关和目标API服务部署在同一台服务器,请求发起自另一台服务器,网关通过Docker部署
  • 先用curl测试上传,完全正常:
curl -X POST http://${server host and port}/fileweb/upload/uploadFile \
-H "Content-Type: multipart/form-data" \
-H "X-Token:" \
-F "files=@test.txt" \
-F 'presetfolder=[{"name":""},{"name":"","parentName":""}]'
  • 换成HttpUrlConnection替代RestTemplate调用,问题依然存在

现在实在搞不懂为什么curl能正常上传,但RestTemplate和HttpUrlConnection走网关就报连接重置/断管的错误,有没有大佬遇到过类似问题?或者能指点下排查方向?谢谢大家!

火山引擎 最新活动