首页 > 代码库 > springmvc上传文件 踩过的坑

springmvc上传文件 踩过的坑

spring-root.xml中配置

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="http://www.mamicode.com/9000000"/>
<property name="maxInMemorySize" value="http://www.mamicode.com/512"/>
</bean>

controller

@RequestMapping(value="http://www.mamicode.com/upload.do",method = RequestMethod.POST)
@ResponseBody
public String batchImport(@RequestParam MultipartFile myFile, HttpServletRequest request,HttpServletResponse response) {

 

form

<form action="upload.do" enctype="multipart/form-data" method="post">
<input type="file" name="myFile" />
<input type="submit" value="http://www.mamicode.com/Upload!" />
</form>

当Web请求到达DispatcherServlet并等待处理的时候,DispatcherServlet首先会检查能否从自己的WebApplicationContext中找到一个名称为multipartResolver(由DispatcherServet的MULTIPART_RESOLVER_BEAN_NAME决定)实例.如果能获得这个实例,DispatcherServlet将通过MultipartResolver的isMultipart(request)方法检查当前Web请求是否为multipart类型.如果是,DispatcherServlet将调用MultipartResolver的resolveMultipart(request)方法,返回MultipartHttpServletRequest,否则返回HttpServletRequest.

这里说下我踩到的坑,网上说基本配置到这里基本上就完成了文件的上传功能,但是这里说下我踩到的坑,一定要把表单的提交方式设置成post,controller也要这样更改!!

 

springmvc上传文件 踩过的坑