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);
}
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>
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.