springMVC上传文件大小限制以及超过限制后的自定义处理

  1. 在xml中加下如下配置
  2.     
  3.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.
  4.       CommonsMultipartResolver">    
  5.         <property name="defaultEncoding" value="UTF-8"/>    
  6.             
  7.         <property name="maxUploadSize" value="200000"/>  
  8.             
  9.         <property name="resolveLazily" value="true"/>   
  10.     bean>  

 

 

Action中增加如下处理

@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;
    }

 

}

你可能感兴趣的