4

I am converting Java web application to Spring framework and appreciate some advice on the issues I am facing with the file upload. Original code was written using org.apache.commons.fileupload.

  1. Does Spring MultipartFile wraps org.apache.commons.fileupload or I can exclude this dependency from my POM file?

  2. I have seen following example:

    @RequestMapping(value = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { byte[] bytes = file.getBytes(); // store the bytes somewhere return "redirect:uploadSuccess"; } else { return "redirect:uploadFailure"; } } 

    Originally I tried to follow this example but was always getting an error as it couldn't find this request param. So, in my controller I have done the following:

    @RequestMapping(value = "/upload", method = RequestMethod.POST) public @ResponseBody ExtResponse upload(HttpServletRequest request, HttpServletResponse response) { // Create a JSON response object. ExtResponse extResponse = new ExtResponse(); try { if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFiles("file"); InputStream input = file.getInputStream(); // do the input processing extResponse.setSuccess(true); } } catch (Exception e) { extResponse.setSuccess(false); extResponse.setMessage(e.getMessage()); } return extResponse; } 

and it is working. If someone can tell me why @RequestParam did not work for me, I will appreciate. BTW I do have

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2097152"/> </bean> 

in my servlet context file.

6 Answers 6

3

I had to

  1. add commons-fileupload dependency to my pom,
  2. add multipartResolver bean (mentioned in the question),
  3. use @RequestParam("file") MultipartFile file in the handleFormUpload method and
  4. add enctype in my jsp : <form:form method="POST" action="/form" enctype="multipart/form-data" >

to get it to work.

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

Comments

2
  1. spring does not have a dependency on commons-fileupload, so you'll need it. If it's not there spring will use its internal mechanism
  2. You should pass a MultipartFile as a method parameter, rather than @RequestParam(..)

2 Comments

What do you mean to pass it as a method parameter? Who will extract it from the request and supply it to the method?
Spring Framework or Spring Boot will not introduce dependency to commons-fileupload. However Spring Cloud Open Feign will introduce this dependency by org.springframework.cloud:spring-cloud-openfeign-core.
1

This works for me.

@RequestMapping(value = "upload.spr", method = RequestMethod.POST) public ModelAndView upload(@RequestParam("file") MultipartFile file, HttpServletResponse response) { // handle file here } 

Comments

0

The General sysntax for request param is this @RequestParam(value="Your value", required=true), mode over request param is used to get a value frm the Url.

Comments

0

In a POST you will only send the params in the request body, not in the URL (for which you use @RequestParams)

Thats why your second method worked.

Comments

0

In Spring MVC 3.2 support for Servet 3.0 was introduced. So you need to include commons-file upload if you use earlier versions of Spring.

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.