---
title: "SpringMvc文件大小超过10M，自定义异常进行补捉"
author: "clgoogle"
date: "2013-02-28T11:40:28Z"
tags: []
categories: ["编程语言"]
slug: "springmvc-10m"
draft: false
lastmod: "2013-05-18T13:16:32Z"
id: 1817676
url: "https://clgoogle.iteye.com/blog/1817676"
---

[![](http://time-earn.com/images/468.gif)](http://time-earn.com/?ref=clgoogle)

 

spring配置文件加上  
&lt;!-- 文件上传--&gt;  
&lt;bean id="multipartResolver"  
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
p:defaultEncoding="utf-8" &gt;  
&lt;property name="resolveLazily" value="true"/&gt;  
&lt;property name="maxUploadSize"&gt;  
&lt;value&gt;1024\*1024&lt;/value&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

全局刷新的方式    
返回页面

@ExceptionHandler  
protected String exception(HttpServletRequest request,Exception e) {  
log.error(e.getMessage(), e);  
       if(e instanceof org.springframework.web.multipart.MaxUploadSizeExceededException){   
           request.setAttribute("exception", new RuntimeException("文件大小超过10M"));   
      } else{  
      request.setAttribute("exception", e);  
      }  
return "forward:/errorPage/500.jsp";  
}

异步刷新的方式，提交的请求时异步的  返回json数据  
//json返回  文件大小超过10M  
@ExceptionHandler(MaxUploadSizeExceededException.class)  
@ResponseBody  
protected String exceptionMaxUploadSizeExceededJson(HttpServletRequest request,Exception e) {  
return "Exceed";  
}  
前台判断即可