spring中,文件上传对文件大小的处理(三)_自定义捕捉controller中MaxUploadSizeExceededException
首先,在spring的xml中配置,惰性加载,延迟文件解析以便在Controller 中捕获文件大小异常
在xml中加下如下配置 <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons. CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="200000"/> <!--resolveLazily属性启用是为了推迟文件解析,以便在UploadController 中捕获文件大小异常--> <property name="resolveLazily" value="true"/> </bean>
controller方法中:
@Controller
public class TestAction{
@RequestMapping(value = "/encoder/importEncoder")
@ResponseBody
public ModelAndView importEncoder(MultipartFile encoderFile, Integer unitId) {
//代码省略
}
/***
**异常处理
***/
@ExceptionHandler(Exception.class)
public @ResponseBody ModelAndView ExceptionHandler(Exception exceededException) {
ActionResult result = new ActionResult(false);
if (ValidateUtil.isNotNull(exceededException) && (exceededException.getCause() instanceof MaxUploadSizeExceededException)) {
result.setMessage("文件导入超过" + ConstparamEncoder.getFaceImportZipFileMaxSize() + "字节限制,请分配次导入!");
AjaxUtil.ajaxWrite(JsonUtils.object2Json(result));
}else {
LogUtils.logException(exceededException);
result.setMessage("出现异常信息");
AjaxUtil.ajaxWrite(JsonUtils.object2Json(result));
}
return null;
}
}

浙公网安备 33010602011771号