通过Zuul上传大文件 [英] Uploading large files via Zuul
问题描述
我在通过zuul上传大文件时遇到了问题. 我正在使用apache-commons文件上传( https://commons.apache.org/proper/commons-fileupload/)来传输大文件,以及我在前面使用zuul. 在我的Spring Boot应用程序中,我禁用了Spring提供的上传功能,以使用apache commons中的上传功能:
I've faced a problem uploading big files through zuul. I'm using apache-commons file upload(https://commons.apache.org/proper/commons-fileupload/) to stream large files as well as I use zuul on the front. In my Spring Boot application I have disabled upload provided by Spring to use the one from 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行为).有没有办法在zuul中使用apache-commons流选项?
If I access my application directly(without zuul), file upload works as intended. However, if it's accessed through zuul, FileItemIterator doesn't have items to traverse and request finishes with error immediately(ERR_CONNECTION_RESET). For zuul I have also disabled multipart given by Spring. Otherwise, it works. However, the files are not streamed. They are loaded completely and only after I get inside the controller(regular Spring behavior). Is there a way to use apache-commons streaming options with zuul?
推荐答案
我找到了一个解决方案. 基本上在这里描述:
I've found a solution. It's basically described here:
http://cloud.spring.io/spring- cloud-static/spring-cloud.html#_uploading_files_through_zuul
我做了什么使它起作用.只需一步一步即可:
What I did to make it work. Just step by step:
- 要绕过Spring DispatcherServlet,我更改了URL:
发件人: http://localhost:8081/MyService/file
收件人: http://localhost:8081/zuul/MyService/file
-
保留对Spring分段上传的禁用:
Preserved disabling of Spring multipart upload:
spring:
http:
multipart:
enabled: false
以下标头不是必需的. 传输编码:分块
The following header is not required. Transfer-Encoding: chunked
我尝试上传一个没有大文件的文件,这很好.
I tried to upload a large file without that one and it was fine.
这篇关于通过Zuul上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!