直接上主要代码:
public class MyCommonsMultipartResolver extends CommonsMultipartResolver implements java.io.Serializable{
private static final long serialVersionUID = 6601669570129283823L;
private HttpServletRequest request;
@Autowired
private MyProgressListener progressListener;
...
@Override
public MultipartParsingResult parseRequest(HttpServletRequest request)
throws MultipartException {
HttpSession session = request.getSession();
String encoding = "utf-8";
FileUpload fileUpload = prepareFileUpload(encoding);
progressListener.setSession(session);
fileUpload.setProgressListener(progressListener);
try {
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
} catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(),ex);
} catch (FileUploadException ex) {
/*
throw new MultipartException("Could not parse multipart servlet request", ex);
这里就不抛异常了
*/
//解决上传中断异常的问题。
List<FileItem> fileItems = new ArrayList<FileItem>();
return parseFileItems(fileItems, encoding);
}
}
...
}
接下来,在文件上传控制器里if(multipartFile==null){return;}一下就可以了