我在通过 zuul 上传大文件时遇到问题。
我正在使用 apache-commons 文件上传(https://commons.apache.org/proper/commons-fileupload/ https://commons.apache.org/proper/commons-fileupload/)来传输大文件以及我在前面使用 zuul 。
在我的 Spring Boot 应用程序中,我禁用了 Spring 提供的上传功能,以使用 apache commons 中的上传功能:
spring:
http:
multipart:
enabled: false
控制器看起来像这样:
public ResponseEntity insertFile(@PathVariable Long profileId,
HttpServletRequest request) throws Exception {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator uploadItemIterator = upload.getItemIterator(request);
if (!uploadItemIterator.hasNext()) {
throw new FileUploadException("FileItemIterator was empty");
}
while (uploadItemIterator.hasNext()) {
FileItemStream fileItemStream = uploadItemIterator.next();
if (fileItemStream.isFormField()) {
continue;
}
//do stuff
}
return new ResponseEntity(HttpStatus.OK);
}
如果我直接访问我的应用程序(没有 zuul),文件上传将按预期进行。但是,如果通过 zuul 访问,FileItemIterator 没有要遍历的项目,请求会立即完成并出现错误(ERR_CONNECTION_RESET)。对于 zuul,我还禁用了 Spring 提供的 multipart。否则,它会起作用。但是,文件不会被传输。它们仅在我进入控制器后才完全加载(常规 Spring 行为)。有没有办法将 apache-commons 流选项与 zuul 一起使用?
我找到了解决方案。
基本上描述如下:
http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_uploading_files_through_zuul http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_uploading_files_through_zuul
我做了什么让它发挥作用。只需一步一步:
- 为了绕过 Spring DispatcherServlet,我更改了 URL:
From:
http://localhost:8081/MyService/文件 http://localhost:8081/MyService/file
To:
http://localhost:8081/zuul/MyService/文件 http://localhost:8081/zuul/MyService/file
-
保留禁用 Spring 分段上传:
spring:
http:
multipart:
enabled: false
不需要以下标头。
传输编码:分块
我尝试在没有该文件的情况下上传一个大文件,结果很好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)