gpt4 book ai didi

java - 无法使用 ServletFileUpload 上传 100MB 文件

转载 作者:行者123 更新时间:2023-11-30 02:03:03 26 4
gpt4 key购买 nike

我必须上传 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 应用程序中大文件上传失败且没有堆栈跟踪,这表明 Tomcat 配置错误。

如果文件太大,Tomcat 会自动断开连接(即使您设置了 spring.http.multipart.max-file-sizespring.http.multipart.max-request-大小属性)。

所以要解决这个问题,你应该配置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>

关于java - 无法使用 ServletFileUpload 上传 100MB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52185633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com