spring mvc 上传文件错误总结

try {
	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
	MultipartFile file = multipartRequest.getFile("XXXid");
	System.out.println(file.getName());
	wb = new HSSFWorkbook(new POIFSFileSystem(file.getInputStream()));
} catch (IOException e) {
	e.printStackTrace();
	logger.error("批量修改配送信息文件读取失败");
}
<input type="file" id="XXXid"/>


这段代码报错:

org.springframework.web.util.NestedServletException: Request processing  
failed; nested exception is java.lang.ClassCastException: com.caucho.server.http.HttpServletRequestImpl  
cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest 

解决方案:

1.添加配置:

<bean id="multipartResolver"
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为10MB -->
        <property name="maxUploadSize">
        	<value>10485760</value>
    	</property>
</bean>

2.添加jar包:

保证有commons-fileupload.jar commons-io.jar这两个jar包

3.form标签添加 enctype="multipart/form-data"

上传文件保存方式:


保存文件到特定文件夹可以这么写

 File file = new File(“目标文件夹”,"想要保存后的文件名");

multipartFile.transferTo(file);






你可能感兴趣的:(springMVC,上传文件)