<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/htmlcharset=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/htmlcharset=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)
}
即可拦截到上传文件大小
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)