<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)
}
即可拦截到上传文件大小
/**的意思是所有文件夹及里面的子文件夹
/*是所有文件夹,不含子文件夹
/是web项目的根目录
比如配置自动册氏扫描mapping.xml
<property name="mapperLocations" value="classpath:com/test/mapping/**/*.xml"></property>这是扫描mapping下子目录的.xml文件,丛姿局包含了mapping目录下和其子目录下的.xml
<property name="mapperLocations" value="classpath:com/test/mapping/*/*.xml"></property>这是扫描mapping下文件夹下的.xml文件,如果mapping包下有.xml文件是不会渗让被扫描的,必须是在mapping目录下的子文件夹下里面的.xml才行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)