标签:boolean https and bean 否则 expect public upload ica
在SpringMVC中上传文件是比较方便的。主要分为以下几个步骤:
1)在applicationContext.xml中增加相应类的引用
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10"></property> <property name="maxInMemorySize" value="2103296"></property> </bean>
这里有一点需呀注意,bean中的id必须为"multipartResolver",不能用其它的名字。否则会出现java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest:
is
a MultipartResolver configured 这种异常。
2)在对应的Action类中直接使用MultipartFile类(org.springframework.web.multipart.MultipartFile)接收前台页面上传的文件。
@RequestMapping("saveFile") public ModelAndView saveFile(MultipartFile photo ) { String contentType = photo.getContentType(); String size = String.valueOf(photo.getSize()); boolean isEmpty = photo.isEmpty(); ModelAndView modelAndView = new ModelAndView("/message/show"); modelAndView.addObject("size", size); modelAndView.addObject("isEmtpy",isEmpty); modelAndView.addObject("contentType",contentType); return modelAndView; }
MultipartFile是CommonsMultipartResolver的父接口。这里有个tips,例子中用于接收文件的变量photo,必须与前端页面的file标签的name属性同名才行。
标签:boolean https and bean 否则 expect public upload ica
原文地址:http://www.cnblogs.com/kuillldan/p/7110398.html