昨天尝试着用spring mvc框架做文件上传,犯了挺多不该犯的毛病
问题1:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade
程序刚一运行访问就抛出了上面的类型转换异常信息,根据提示信息,找到了导致该异常的代码行:
MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request;
MultipartFile file=multipartRequest.getFile("upload");
这个问题花费了我很多时间和精力,查找了很多相关资料之后,发现我的配置文件里少了对multipart解析器的配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
添加上之后,运行,访问,上面的异常终于没有再出现,但是新的问题又来了
问题2:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
这次的问题很明确,缺少文件包,添加commons-fileupload-1.2.jar,
问题解决,接着又出新问题
问题3:
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
添加commons-io-1.3.1,问题解决,至此,整个程序终于可以完整的运行,正常上传
转自: http://blog.csdn.net/lingirl/archive/2007/07/29/1714806.aspx