Results 1 to 3 of 3

Thread: Multipart upload using annotations

  1. #1
    Join Date
    Oct 2008
    Posts
    1

    Default Multipart upload using annotations

    I am trying to do a multipart file upload.

    I am annotating my controller with
    Code:
    Controller, RequestMapping and RequestParam
    annotations. ( I would have put the atSigns's in there but apparently that means I am linking to another site outside of the forum, and I can't do that until I have posted 5 times, seems like a bug to me, but that's an aside)

    When the form is submitted I receive missing parameter exceptions, if I removed the enctype from the form, they resolve, but I lose the multipart file.
    Code:
    <form action="uploadSubmit.view" enctype="multipart/form-data" method="POST">
    I have a multipart resolver declared in my applicationContext

    Code:
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    The controller looks like this
    Code:
    Controller
    public class UploadDestUrlReportController {
    
    	RequestMapping("/upload.view")
    	public ModelAndView handleUrlReport() {
    //do stuff
    		return new ModelAndView("upload", modelMap);
    	}
    
    	RequestMapping("/uploadSubmit.view")
    	public ModelAndView handleUrlReportUpload(HttpServletResponse response,
    			RequestParam("id") int id,
    			RequestParam("id2") int id2,
    			RequestParam("file") MultipartFile f) throws IOException {
    		Map<String, Object> modelMap = new HashMap<String, Object>();
    		if (f == null) {
    			modelMap.put("msg", "null file");
    			return new ModelAndView("upload", modelMap);
    		}
    		modelMap.put("msg", "File ( " + f.getName() + ") uploaded");
    		return new ModelAndView("upload", modelMap);
    	}
    
    }

    Any ideas on what I could be missing?

    Thanks,

  2. #2
    Join Date
    Nov 2008
    Posts
    1

    Default

    I am sure you figured this out by now, but I will reply anyway, just in case :-)

    In the controller:

    @RequestMapping(value="/upload.view")
    public ModelAndView upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile f) {
    if (f == null) {
    return new ModelAndView("upload", "msg", "The file is null.");
    }

    return new ModelAndView("upload", "msg", "File ( " + f.getOriginalFilename() + ") successfully uploaded.");
    }

    And in the appliction context:

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    <!-- Max size in bytes. -->
    <property name="maxUploadSize" value="100000"/>
    </bean>

    In my implementation, this is all I needed. Hope this helps.

  3. #3
    Join Date
    Jun 2009
    Posts
    7

    Thumbs up Thank you

    @yodub
    Thank you for posting the message anyway, it was a big help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •