最近开发springmvc文件上传的功能遇到几个问题,这里记录下
1,java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
错误原因:缺少jar包依赖添加
解决办法:POM中添加依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileupload-version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${io-version}</version>
</dependency>
2,Failedtoconvert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile];
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type
[org.springframework.web.multipart.MultipartFile]: no matching editors or conversion strategy found
错误原因:get方式提交
解决办法:controller上加method,form表单添加 method
@RequestMapping(value = "/upload",method = { RequestMethod.POST })
method="post"
3,springmvc引入静态文件404问题
原因:配置dispather后,所有的请求都通过spring,但是并没有处理*/jquery.js的controller,所以404
解决办法:web.xml添加如下代码(必须在dispather配置之前):
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
还有一种解决办法:mvc.xml中添加如下代码
<mvc:resources mapping="/js/**" location="/js/" />