文件上传大小限制导致上传不成功,浏览器提示不友好处理(后端SpringMVC,前端vue)

前端错误提示

Console:https://file.xhkjedu.com/upload/upload.do net::ERR_CONNECTION_RESET
NetWork:Referrer Policy: no-referrer-when-downgrade

解决方法:
application.xml文件不使用Spring MVC提供的文件上传大小限制属性;
拦截器加判断。

config.properties

#文件上传大小限制100M
maxSize=104857600

application.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!--<property name="maxUploadSize" value="104857600"/>-->
    <!--<property name="maxInMemorySize" value="4096"/>-->
</bean>

拦截器代码

public class RequestInterceptor implements HandlerInterceptor {
	private static final Logger logger = Logger.getLogger(RequestInterceptor.class);
	private static Integer maxSize = Integer.valueOf(ZJ_ConfigUtils.getProperty("maxSize"));//文件上传大小限制
	
	/**
	 * 完成页面的render后调用
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {

	}

	/**
	 * 在调用controller具体方法后拦截
	 */
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
		
		System.out.println("拦截方法后的请求地址:" + request.getRequestURI());

		
	}

	/**
	 * 在调用controller具体方法前拦截
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		
		logger.info("请求的uri:" + request.getRequestURI() + "\n" + "请求的参数:" + ZJ_JacksonUtils.toJson(request.getParameterMap()));
		//判断是否文件上传
		if(request!=null && ServletFileUpload.isMultipartContent(request)) {
			ServletRequestContext ctx = new ServletRequestContext(request);
			//获取上传文件尺寸大小
			long requestSize = ctx.contentLength();
			if (requestSize > maxSize) {
				//当上传文件大小超过指定大小限制后,模拟抛出MaxUploadSizeExceededException异常
				//throw new MaxUploadSizeExceededException(maxSize);
				ResultVo result = new ResultVo();
				result.setCode(1);
				result.setMsg("上传文件大小【"+requestSize+"】超过指定大小【"+maxSize+"】限制");
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=UTF-8");
				response.getWriter().print(ZJ_JacksonUtils.toJson(result));
				return false;
			}
		}
		return true;
	}

}

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd

       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<mvc:default-servlet-handler/>
	
	<!-- 开启注解模式驱动 -->
    <mvc:annotation-driven />
	<!--  只扫描含@Controller注解的包,避免重复扫描 -->
   	<context:component-scan base-package="com.XXX.controller" use-default-filters="true" />
   	
     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	       <property name="messageConverters">
	           <list>
	               <bean class="org.springframework.http.converter.StringHttpMessageConverter">
	                   <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
	               </bean>
	               <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	                   <property name="supportedMediaTypes">
	                       <list>
	                           <value>application/json;charset=UTF-8</value>
	                           <value>text/html;charset=UTF-8</value>
	                       </list>
	                   </property>
	               </bean>
	           </list>
	       </property>
	   </bean>

    <!-- 拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/**/fonts/*"/>
				<mvc:exclude-mapping path="/**/*.css"/>
				<mvc:exclude-mapping path="/**/*.js"/>
				<mvc:exclude-mapping path="/**/*.png"/>
				<mvc:exclude-mapping path="/**/*.gif"/>
				<mvc:exclude-mapping path="/**/*.jpg"/>
				<mvc:exclude-mapping path="/**/*.jpeg"/>
				<mvc:exclude-mapping path="/**/*.woff"/>
				<mvc:exclude-mapping path="/**/*.doc"/>
				<mvc:exclude-mapping path="/**/*.docx"/>
				<mvc:exclude-mapping path="/**/*.class"/>
				<mvc:exclude-mapping path="/**/*.ppt"/>
            <bean class="com.XXX.interceptors.RequestInterceptor">
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章