Announcement Announcement Module
Collapse
No announcement yet.
Multipart upload using annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thank you

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

      Comment

      Working...
      X