SpringMVC文件上传要注意的两个小点
如果出现:由于没有提供multi-part配置,无法处理parts
消息:Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: 由于没有提供multi-part配置,无法处理parts
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: 由于没有提供multi-part配置,无法处理parts
很可能是因为:id是否没有写成 multipartResolver
<bean id="commonsMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="#{50*1024*1024}"/>
</bean>
如果出现:Request processing failed; nested exception is java.lang.NullPointerException
很可能是因为:input标签里的name没有和控制层的形参名对应上
页面上:
<form method="post" enctype="multipart/form-data" action="upload">
File Upload: <input type="file" name="uploadFile">
<input type="submit" value="upload">
</form>
控制层:
@Controller
public class UploadController {
@RequestMapping("upload")
@ResponseBody
public String upload(MultipartFile uploadFile) throws IOException {
String filename = uploadFile.getOriginalFilename();
String uploadPath="D:\\uploadTest\\";
uploadFile.transferTo(new File(uploadPath+filename));
return "success";
}
}
江河不拒细流,故能成其深