我必须上传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);
}
答案 0 :(得分:2)
在Spring App中没有堆栈跟踪的大文件上传失败表明Tomcat配置错误。
如果文件太大(即使您设置了spring.http.multipart.max-file-size
和spring.http.multipart.max-request-size
属性,Tomcat也会自动断开连接。)
因此,要解决此问题,您应该配置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>
答案 1 :(得分:0)
也可以在应用程序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;
}
}