1

I have to upload the 100MB file. My front end part is Angular 4 and backend part is in Java and Spring 4 . I have exposed it as REST endpoint. 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);
    }
0

2 Answers 2

2

Large file uploaded fail with no stacktrace in Spring App is an indicator of wrong Tomcat configuration.

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).

So to solve it, you should config tomcat to allow large file upload

Open $TOMCAT_HOME/webapps/manager/WEB-INF/web.xml and edit the following properties:

<!-- 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>
Sign up to request clarification or add additional context in comments.

3 Comments

What if i deploy my war on Weblogic ?
I have no exp with WebLogic. But this might help
I did as you mentioned but it is still the same
0

the multipart resolver size can also be specified in your application xml file. You can either do it via that or via programitcally

Application xml

To add it to your xml file you can add a bean like thus:

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

2 Comments

If i do this multipart process the request before it reaches to controller
This will configure your multipart process and you do not have to do anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.