如何拦截上传大文件的时候spring抛出的异常
展开全部
只需在控制层:
@ExceptionHandler
public ModelAndView doException(Exception e, HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
if (e instanceof MaxUploadSizeExceededException) {
long maxSize = ((MaxUploadSizeExceededException) e).getMaxUploadSize();
map.put("error", "上传文件太大,不能超过" + maxSize / 1024 + "k");
// response.setHeader("Content-type", "text/html;charset=UTF-8");
// // 设置默认编码
// response.setCharacterEncoding("UTF-8");
// response.getWriter().write("上传文件太大,不能超过" + maxSize / 1024 + "k");
} else if (e instanceof RuntimeException) {
map.put("error", "未选中文件");
} else {
map.put("error", "上传失败");
}
return new ModelAndView("upload", map);
}
@ExceptionHandler
public ModelAndView doException(Exception e, HttpServletRequest request) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
if (e instanceof MaxUploadSizeExceededException) {
long maxSize = ((MaxUploadSizeExceededException) e).getMaxUploadSize();
map.put("error", "上传文件太大,不能超过" + maxSize / 1024 + "k");
// response.setHeader("Content-type", "text/html;charset=UTF-8");
// // 设置默认编码
// response.setCharacterEncoding("UTF-8");
// response.getWriter().write("上传文件太大,不能超过" + maxSize / 1024 + "k");
} else if (e instanceof RuntimeException) {
map.put("error", "未选中文件");
} else {
map.put("error", "上传失败");
}
return new ModelAndView("upload", map);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询