I am getting error when i uploading 10 MB Size of csv file in Spring by using CommonsMultipartResolver library. I have done following setting in xml file Xml File Confi :
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- max upload size in bytes -->
<beans:property name="maxUploadSize" value="99971520" /> <!-- 99MB -->
<!-- max size of file in memory (in bytes) -->
<beans:property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
Controller Code:
@RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public @ResponseBody String uploadForm1(@ModelAttribute("admin") BillingAndRecon billingandrecon,@RequestParam String id,BindingResult result,Principal principal,@RequestParam MultipartFile file,HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {
File uploadFile = null;
String msg = "";
if (!file.getOriginalFilename().equals("")) {
logger.info("Before Multipart file get path >> ");
BillingAndReconServiceImpl asi = (BillingAndReconServiceImpl) this.billingAndReconService;// not correct!!
String uploadDirectoryPath = asi.getUploadDirectoryPath(); // not correct!!
uploadFile = new File( uploadDirectoryPath + file.getOriginalFilename());
logger.info("Before Multipart file get path uploadDirectoryPath >> "+uploadDirectoryPath);
file.transferTo(uploadFile);
}
}
Form Page:
<form:form action="./uploadForm" method="post" enctype="multipart/form-data" ModelAttribute=="admin">
<input type="file" name="file" />
<input type="text" name="id" />
</form:form>
But I am not understanding what is issue. I tried to set up size and setting header also enctype="multipart/form-data"
, but yet not resolved.
Given below are error:
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:954)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:892)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
As @ChristianMaioliM requested in comment, added more details about The problem in your code is the BindingResult parameters not following the model object.
The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more than one model object and Spring will create a separate BindingResult instance for each of them so the following sample won’t work
Refer docs Invalid ordering of BindingResult and @ModelAttribute
To resolve, change your controller method handler signature to follow parameter ordering between BindingResult & model object like:
From:
@RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public @ResponseBody String uploadForm1(@ModelAttribute("admin") BillingAndRecon billingandrecon,@RequestParam String id,BindingResult result,Principal principal,@RequestParam MultipartFile file,HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {
To:
@RequestMapping(value="/uploadForm",method = RequestMethod.POST)
public String uploadForm1(
@ModelAttribute("admin") BillingAndRecon billingandrecon,
BindingResult result,
Principal principal,
HttpSession session) throws ServiceException, DaoException, IllegalStateException, IOException {
//do file save here
return "some-view-name";
}
and in BillingAndRecon class add mulitpart/binding fields like:
public class BillingAndRecon {
private MultipartFile file;
private String id;
no-arg constructor;
getters;
setters;
}
Note: BindingResult argument should be after immediate of @ModelAttrubiute/@RequestBody
and jsp form:
<form:form action="${pageContext.request.contextPath}/uploadForm"
method="post"
enctype="multipart/form-data"
modelAttribute="admin">
<input type="file" name="file" />
<form:input path="id" />
</form:form>
and don't forget to add for binding instance in GET handler like:
@RequestMapping(value="/uploadForm",method = RequestMethod.GET)
public String uploadFormView(Model model){
model.addAttribute("admin", new BillingAndRecon());
return "your-upload-view-name";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With