springmvc如何拦截上传文件最大限制异常
1个回答
2017-02-01 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
在applicationContext.xml中添加: <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 --> <property name="defaultEncoding" value="utf-8"></property> <property name="resolveLazily" value="true"></property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 --> <property name="defaultEncoding" value="utf-8"></property> <property name="resolveLazily" value="true"></property> </bean> 只需在控制层 @Excep... 在applicationContext.xml中添加:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->
<property name="defaultEncoding" value="utf-8"></property>
<property name="resolveLazily" value="true"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->
<property name="defaultEncoding" value="utf-8"></property>
<property name="resolveLazily" value="true"></property>
</bean>
只需在控制层
@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);
}
即可拦截到上传文件大小
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->
<property name="defaultEncoding" value="utf-8"></property>
<property name="resolveLazily" value="true"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过20M。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="2000000"/><!-- 1G 1073741824 -->
<property name="defaultEncoding" value="utf-8"></property>
<property name="resolveLazily" value="true"></property>
</bean>
只需在控制层
@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);
}
即可拦截到上传文件大小
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询