Spring 3.0 Download MultipartFile

I am converting a Java web application into a Spring framework and appreciate some tips on the issues that I have to deal with when downloading files. The source code was written using org.apache.commons.fileupload.

  • Can Spring MultipartFile wrap org.apache.commons.fileupload or can I exclude this dependency from my POM file?

  • I saw the 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";
        }
    }
    

    Initially, I tried to follow this example, but always got an error, because I could not find this query parameter. So, in my controller, I did 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 works. If someone tells me why @RequestParam does not work for me, I would appreciate it. BTW I have

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

in my servlet context file.

+3
source share
6
  • spring shareons-fileupload, . , spring
  • MultipartFile , @RequestParam(..)
+2

  • shareup-fileupload pom,
  • multipartResolver bean ( ),
  • @RequestParam ( "file" ) MultipartFile handleFormUpload
  • enctype my jsp: <form:form method="POST" action="/form" enctype="multipart/form-data" >

.

+2

.

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

sysntax - @RequestParam (value = " ", = true), mode over request param frm Url.

0

In POST, you will send parameters only to the request body, not to the URL (for which you use @RequestParams)

That is why your second method worked.

0
source

Spring introduced MVC 3.2 support for Servet 3.0. Therefore, you must enable the download of public files if you are using earlier versions of Spring.

0
source

All Articles