3

I have Spring MVC based REST application. Trying to upload a file. Here is the code snippet.

webmvc-config.xml

<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> <property name="maxUploadSize" value="2000000" /> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="0" /> <property name="defaultContentType"> <ref bean="htmlMediaType" /> </property> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="file" value="multipart/form-data"/> </map> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller"> <bean class="org.springframework.oxm.castor.CastorMarshaller" /> </property> </bean> </list> </property> 

FileUploadController.java

@Controller @RequestMapping("/appfiles") public class FileUploadController { private String saveDirectory = "C:/Downloads/"; @RequestMapping(value = "/appfiles", method = RequestMethod.POST, consumes="multipart/form-data", produces="application/json") public ResponseEntity<List<Map<String, String>>> save( @RequestParam("file") MultipartFile file) { System.out.println("received file with original filename: " + file.getOriginalFilename()); // List<MultipartFile> files = uploadForm.getFiles(); List<Map<String, String>> response = new ArrayList<Map<String, String>>(); Map<String, String> responseMap = new HashMap<String, String>(); List<String> fileNames = new ArrayList<String>(); if (null != file) { // for (MultipartFile multipartFile : files) { String fileName = file.getOriginalFilename(); fileNames.add(fileName); try { file.transferTo(new File(saveDirectory + file.getOriginalFilename())); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } responseMap.put("displayText", file.getOriginalFilename()); responseMap.put("fileSize", ""+file.getSize()); response.add(responseMap); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Accept", "application/json"); return new ResponseEntity<List<Map<String, String>>>(response, httpHeaders, HttpStatus.OK); } } 

I get the following error when I use chrome extension "postman"

Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163) org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139) org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1020) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:883) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

postname submission is done with the following details:

Headers conent-type: multipart/form-data Form-data: file=<filename.txt> Method=POST 

Can you please let me know what is wrong with my code?

Thanks & Regards

4
  • How are you POSTing your data? Commented Feb 25, 2013 at 10:57
  • I am using postman addon for chrome as my REST client. I set content-type=multipart/form-data and accept=application/json in header fields. Uploaded the file using form-data fields. I created an attribute called "file" in form-data and upload the file there. and no other attributes set while making "POST" call. Commented Feb 25, 2013 at 12:07
  • It sounds like the request was missing the form boundaries. Do you have any means of looking at the actual HTTP request? I have a feeling your request is missing some crucial pieces. Also, it might help to try to post using a dummy HTML form, just get a baseline as to what the request SHOULD look like. Commented Feb 25, 2013 at 13:47
  • Expanding on my last comments: Things to check - You set a boundary in your Content-Type header and your boundary matches whats used in the body. Some explaination can be found here: en.wikipedia.org/wiki/MIME#Multipart_messages Commented Feb 25, 2013 at 13:49

1 Answer 1

4

I also faced the same issue, but when I try it without any Header value it was work fine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.