springMvc perfectly solves the problem that the multipart / form-data method submits the request and the parameter cannot be obtained in the ServletRequest.getParameter method in the Filter

This article only talks about using it but not explaining the principle too much (because I haven't studied it too). I hope that friends who are familiar with the principle can share it and start below.

-------------------------------------------------- ---------------------------- Boring preface but still have to say --------------- -------------------------------------------------- -------------------------------------------------- -----

I encountered some problems when processing the uploaded file at the front end. The request submitted by the multipart / form-data method cannot be used in the Filter. The ServletRequest.getParameter method cannot get the parameters submitted together. The Internet says that the ServletRequest.getReader () method is needed to Get the request information ( RequestPayload ), and then parse the string to get the desired parameters. but! ServletRequest.getReader () can only be obtained once. If requestPayload is taken out of the interception, then you will not get the parameters and files in the spring controller! ! ! ! !

After groping here, I found that this is a detour. Since the parameters can be obtained from the springMVC interface, it means that spring has solved the problem very well.



! ! ! ! ! ! ! ! ! ! Here comes the dry goods

By looking at the source code of mvc, two classes were found,

1.1. Implemented the org.springframework.web.multipart.MultipartHttpServletRequest.java class of ServletRequest (the getParameter method of this class can get the parameters uploaded by multipart / form-data and non-multipart / form-data methods)

1.2. Org.springframework.web.multipart.MultipartResolver.java class for creating MultipartHttpServletRequest.java

Instructions



2.1 First configure MultipartHttpServletRequest in your spring configuration file

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

2.2 Inject MultipartHttpServletRequest into your interception

// 用于创建MultipartHttpServletRequest
private MultipartResolver multipartResolver = null;
	
@Override
public void init(FilterConfig arg0) throws ServletException {
// 注入bean
	multipartResolver = ((MultipartResolver)ApplicationContextUtil.getContext().getBean("multipartResolver", MultipartResolver.class));
}


 
 

2.3 Replace your ServletRequest with MultipartHttpServletRequest in your top-level interceptor

        /**
	 * 获取 request
	 * @param req
	 * @return
	 */
	private ServletRequest getRequest(ServletRequest req){
		String enctype = req.getContentType();
		if (StringUtils.isNotBlank(enctype) && enctype.contains("multipart/form-data"))
			// 返回 MultipartHttpServletRequest 用于获取 multipart/form-data 方式提交的请求中 上传的参数
			return multipartResolver.resolveMultipart((HttpServletRequest) req);
		else 
			return req;
	}


2.4 Give MultipartHttpServletRequest obtained by getRequest (req) in 2.3 above to chain.doFilter (MultipartHttpServletRequest, resp); let the request continue to execute. After all interceptors and controllers can continue to get parameters

2.5 gone






Published 17 original articles · won 24 · views 280,000 +

Guess you like

Origin blog.csdn.net/qq_22956867/article/details/51437905