最近在写一个 web 端的私人网盘服务,测试发现上传 2G 以上大文件时 后台会出现异常,请问有大佬做过相关的需求吗?怎么解决这类问题?
异常日志如下
ERROR 19593 --- [io-18073-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 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.impl.IOFileUploadException: Processing of multipart/form-data request failed. java.io.EOFException] with root cause
java.io.EOFException: null ...
我修改了好多参数也不好使
@Configuration
@Slf4j
public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
log.info("Init EmbeddedTomcatConfig...");
((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setMaxConnections(3000);
protocol.setMaxThreads(800);
protocol.setAcceptCount(200);
protocol.setSelectorTimeout(30000);
protocol.setSessionTimeout(60000 * 2);
protocol.setConnectionTimeout(60000 * 5);
protocol.setDisableUploadTimeout(false);
protocol.setConnectionUploadTimeout(60000 * 10);
}
});
}
}
application 参数
spring.servlet.multipart.max-request-size=-1 spring.servlet.multipart.max-file-size=-1 server.tomcat.max-swallow-size=-1 server.tomcat.max-http-form-post-size=-1
控制层
@ResponseBody
@ApiOperation(value = "上传文件",notes = "上传文件")
@RequestMapping(value = "/FilesUpload",method = RequestMethod.POST)
public BaseResponse uploadFiles(
@RequestParam(required = true) MultipartFile files,
HttpServletRequest request,
HttpServletResponse response
) {
if (files.isEmpty() || files.getSize() == 0) {
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
return BaseResponse.initErrorBaseResponse("不能上传空文件!");
}
try {
return BaseResponse.initSuccessBaseResponse(fileExecuteService.uploadFiles(files,request), "操作成功");
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
return BaseResponse.initErrorBaseResponse(e.getMessage());
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.