文件上傳大小限制導致上傳不成功,瀏覽器提示不友好處理(後端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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章