【问题标题】:Unable to upload 100MB file using ServletFileUpload无法使用 ServletFileUpload 上传 100MB 文件
【发布时间】:2018-09-05 12:52:54
【问题描述】:

我必须上传 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);
    }

【问题讨论】:

    标签: java spring


    【解决方案1】:

    大文件上传失败,Spring App 中没有堆栈跟踪,表明 Tomcat 配置错误。

    如果您的文件太大,Tomcat 会自动断开连接(即使您设置了 spring.http.multipart.max-file-sizespring.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>
    

    【讨论】:

    • 如果我在 Weblogic 上部署我的战争会怎样?
    • 我对 WebLogic 没有经验。但是this might help
    • 我照你说的做了,但还是一样
    【解决方案2】:

    多部分解析器的大小也可以在您的应用程序 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;
      }
    }
    

    【讨论】:

    • 如果我在请求到达控制器之前执行此多部分处理
    • 这将配置您的多部分流程,您无需执行任何其他操作。
    猜你喜欢
    • 2012-10-14
    • 1970-01-01
    • 2020-07-27
    • 2017-01-24
    • 2020-06-05
    • 2011-08-14
    • 1970-01-01
    • 2019-04-23
    • 2011-06-18
    相关资源
    最近更新 更多