[英]Unable to upload 100MB file using ServletFileUpload
我必须上传100MB文件。 我的前端部分是Angular 4,后端部分是Java和Spring 4。 我将其公开为REST端点。 一旦我在一段时间后上传文件,连接就会中断,并且不会返回任何内容到前端。
@SuppressWarnings("unchecked")
@RequestMapping(value = "/insertDoc.action", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> insertDoc(final HttpServletRequest request,
final HttpServletResponse response, @RequestParam(name = "docType", required = false) final String docType) {
List<DocumentMetadataVO> docIdList = new ArrayList<DocumentMetadataVO>();
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(new ServletRequestContext(request));
if (isMultipart) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
List<FileItem> items = upload.parseRequest(request);
for (FileItem fileItem : items) {
DocumentMetadataVO documentMetadataVO = new DocumentMetadataVO();
documentMetadataVO.setFileData(fileItem.get());
documentMetadataVO.setDocumentName(fileItem.getName());
documentMetadataVO.setUploadDate(new Date());
logger.info("File Name is::" + documentMetadataVO.getDocumentName());
documentMetadataVO.setDocType(docType);
String docId = commonService.insertDocument(request, documentMetadataVO);
documentMetadataVO.setDocId(docId);
docIdList.add(documentMetadataVO);
}
}
} catch (Exception e) {
logger.error(e);
return new ResponseEntity<Map<String, Object>>(getModelMapError(e.getMessage()),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<Map<String, Object>>(getMap(docIdList), HttpStatus.OK);
}
在Spring App中没有堆栈跟踪的大文件上传失败表明Tomcat配置错误。
如果文件太大,Tomcat会自动断开连接(即使您设置了spring.http.multipart.max-file-size
和spring.http.multipart.max-request-size
属性)。
因此,要解决此问题,您应该配置tomcat以允许上传大文件
打开$TOMCAT_HOME/webapps/manager/WEB-INF/web.xml
并编辑以下属性:
<!-- 150MB max -->
<multipart-config>
<max-file-size>157286400</max-file-size>
<max-request-size>209715200</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
也可以在应用程序xml文件中指定多部分解析器的大小。 您既可以通过它也可以通过编程来实现
应用程式xml
要将其添加到您的xml文件中,您可以像下面这样添加一个bean:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="16777215" />
</bean>
编程方式
要以编程方式添加并可能可配置,请尝试以下操作:
/**
* Configuration to set the multipart Resolver bean and the max upload size
*
*/
@Configuration
@Component
public class MultipartResolverBeanConfig {
/**
* Create the multipart resolver bean
* Set the maximum upload file size
* @return
*/
@Bean(name="multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(157286400L);
return multipartResolver;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.