简体   繁体   English

无法使用ServletFileUpload上传100MB文件

[英]Unable to upload 100MB file using ServletFileUpload

I have to upload the 100MB file. 我必须上传100MB文件。 My front end part is Angular 4 and backend part is in Java and Spring 4 . 我的前端部分是Angular 4,后端部分是Java和Spring 4。 I have exposed it as REST endpoint. 我将其公开为REST端点。 Once i upload the file after sometime the connection get break and it does not return anything to the front end. 一旦我在一段时间后上传文件,连接就会中断,并且不会返回任何内容到前端。

@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);
    }

Large file uploaded fail with no stacktrace in Spring App is an indicator of wrong Tomcat configuration. 在Spring App中没有堆栈跟踪的大文件上传失败表明Tomcat配置错误。

Tomcat automatically drop connection if your file is too large (eventhough you set your spring.http.multipart.max-file-size and spring.http.multipart.max-request-size properties). 如果文件太大,Tomcat会自动断开连接(即使您设置了spring.http.multipart.max-file-sizespring.http.multipart.max-request-size属性)。

So to solve it, you should config tomcat to allow large file upload 因此,要解决此问题,您应该配置tomcat以允许上传大文件

Open $TOMCAT_HOME/webapps/manager/WEB-INF/web.xml and edit the following properties: 打开$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>

the multipart resolver size can also be specified in your application xml file. 也可以在应用程序xml文件中指定多部分解析器的大小。 You can either do it via that or via programitcally 您既可以通过它也可以通过编程来实现

Application xml 应用程式xml

To add it to your xml file you can add a bean like thus: 要将其添加到您的xml文件中,您可以像下面这样添加一个bean:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="16777215" />
</bean>

Programatically 编程方式

To add it programatically and possibly configurable try this: 要以编程方式添加并可能可配置,请尝试以下操作:

/**
 * 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM