再谈SpringMvc文件上传异常处理
@ExceptionHandler(MaxUploadSizeExceededException.class) public void handleException(MaxUploadSizeExceededException ex, HttpServletResponse response) throws Exception { StringBuilder error = new StringBuilder(); if (ex instanceof MaxUploadSizeExceededException) { MaxUploadSizeExceededException ms = (MaxUploadSizeExceededException) ex; SizeLimitExceededException cause = (SizeLimitExceededException) ms.getCause(); long maxUploadSize = ms.getMaxUploadSize(); String actualSize = String.valueOf(cause.getActualSize()); double parseDouble = Double.parseDouble(actualSize) / 1024 / 1024; BigDecimal b = new BigDecimal(parseDouble); double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); error.append("最大上传文件为:" + maxUploadSize / 1024 / 1024).append("M;"); error.append("实际文件大小为:").append(d).append("M"); System.out.println(error.toString()); } error.append("上传文件出错"); System.out.println(error.toString()); }
XML配置
微信公众号