Java Code Examples for org.springframework.web.multipart.MultipartResolver#resolveMultipart()

The following examples show how to use org.springframework.web.multipart.MultipartResolver#resolveMultipart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebUtils.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 从request中获得参数,并返回可读的Map
 * application/x-www-form-urlencode
 * application/json
 * application/json;charset=UTF-8
 * multipart/form-data
 *
 * @param request
 * @return
 */
public static Map<String, String> getParameterMap(HttpServletRequest request) {
    String contentType = request.getHeader(org.springframework.http.HttpHeaders.CONTENT_TYPE);
    Map<String, String> returnMap = new HashMap();
    if (contentType != null && contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
        // form-data表单
        MultipartResolver multipartResolver = SpringContextHolder.getBean(MultipartResolver.class);
        MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request);
        returnMap = conventMap(multiReq.getParameterMap());
    } else if (MediaType.APPLICATION_JSON_VALUE.equals(contentType) || MediaType.APPLICATION_JSON_UTF8_VALUE.equals(contentType)) {
        // json表单
        String body = getBodyString(request);
        if (StringUtils.isNotBlank(body)) {
            try {
                returnMap = JSONObject.parseObject(body, Map.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else {
        // 普通表单
        returnMap = conventMap(request.getParameterMap());
    }
    // 参数Map
    return returnMap;
}
 
Example 2
Source File: WebUtil.java    From hdw-dubbo with Apache License 2.0 6 votes vote down vote up
/**
 * 从request中获得参数,并返回可读的Map
 * application/x-www-form-urlencode
 * application/json
 * application/json;charset=UTF-8
 * multipart/form-data
 *
 * @param request
 * @return
 */
public static Map<String, String> getParameterMap(HttpServletRequest request) {
    String contentType = request.getHeader(org.springframework.http.HttpHeaders.CONTENT_TYPE);
    Map<String, String> returnMap = new HashMap();
    if (contentType != null && contentType.contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
        // form-data表单
        MultipartResolver multipartResolver = SpringUtil.getBean(MultipartResolver.class);
        MultipartHttpServletRequest multiReq = multipartResolver.resolveMultipart(request);
        returnMap = conventMap(multiReq.getParameterMap());
    } else if (MediaType.APPLICATION_JSON_VALUE.equals(contentType) || MediaType.APPLICATION_JSON_UTF8_VALUE.equals(contentType)) {
        // json表单
        String body = getBodyString(request);
        if (StringUtils.isNoneBlank(body)) {
            try {
                returnMap = JSONObject.parseObject(body, Map.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else {
        // 普通表单
        returnMap = conventMap(request.getParameterMap());
    }
    // 参数Map
    return returnMap;
}
 
Example 3
Source File: MultipartFilter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check for a multipart request via this filter's MultipartResolver,
 * and wrap the original request with a MultipartHttpServletRequest if appropriate.
 * <p>All later elements in the filter chain, most importantly servlets, benefit
 * from proper parameter extraction in the multipart case, and are able to cast to
 * MultipartHttpServletRequest if they need to.
 */
@Override
protected void doFilterInternal(
		HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	MultipartResolver multipartResolver = lookupMultipartResolver(request);

	HttpServletRequest processedRequest = request;
	if (multipartResolver.isMultipart(processedRequest)) {
		if (logger.isTraceEnabled()) {
			logger.trace("Resolving multipart request");
		}
		processedRequest = multipartResolver.resolveMultipart(processedRequest);
	}
	else {
		// A regular request...
		if (logger.isTraceEnabled()) {
			logger.trace("Not a multipart request");
		}
	}

	try {
		filterChain.doFilter(processedRequest, response);
	}
	finally {
		if (processedRequest instanceof MultipartHttpServletRequest) {
			multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
		}
	}
}
 
Example 4
Source File: MultipartFilter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check for a multipart request via this filter's MultipartResolver,
 * and wrap the original request with a MultipartHttpServletRequest if appropriate.
 * <p>All later elements in the filter chain, most importantly servlets, benefit
 * from proper parameter extraction in the multipart case, and are able to cast to
 * MultipartHttpServletRequest if they need to.
 */
@Override
protected void doFilterInternal(
		HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	MultipartResolver multipartResolver = lookupMultipartResolver(request);

	HttpServletRequest processedRequest = request;
	if (multipartResolver.isMultipart(processedRequest)) {
		if (logger.isTraceEnabled()) {
			logger.trace("Resolving multipart request");
		}
		processedRequest = multipartResolver.resolveMultipart(processedRequest);
	}
	else {
		// A regular request...
		if (logger.isTraceEnabled()) {
			logger.trace("Not a multipart request");
		}
	}

	try {
		filterChain.doFilter(processedRequest, response);
	}
	finally {
		if (processedRequest instanceof MultipartHttpServletRequest) {
			multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
		}
	}
}
 
Example 5
Source File: MultipartFilter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check for a multipart request via this filter's MultipartResolver,
 * and wrap the original request with a MultipartHttpServletRequest if appropriate.
 * <p>All later elements in the filter chain, most importantly servlets, benefit
 * from proper parameter extraction in the multipart case, and are able to cast to
 * MultipartHttpServletRequest if they need to.
 */
@Override
protected void doFilterInternal(
		HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	MultipartResolver multipartResolver = lookupMultipartResolver(request);

	HttpServletRequest processedRequest = request;
	if (multipartResolver.isMultipart(processedRequest)) {
		if (logger.isDebugEnabled()) {
			logger.debug("Resolving multipart request [" + processedRequest.getRequestURI() +
					"] with MultipartFilter");
		}
		processedRequest = multipartResolver.resolveMultipart(processedRequest);
	}
	else {
		// A regular request...
		if (logger.isDebugEnabled()) {
			logger.debug("Request [" + processedRequest.getRequestURI() + "] is not a multipart request");
		}
	}

	try {
		filterChain.doFilter(processedRequest, response);
	}
	finally {
		if (processedRequest instanceof MultipartHttpServletRequest) {
			multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
		}
	}
}
 
Example 6
Source File: MultipartFilter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check for a multipart request via this filter's MultipartResolver,
 * and wrap the original request with a MultipartHttpServletRequest if appropriate.
 * <p>All later elements in the filter chain, most importantly servlets, benefit
 * from proper parameter extraction in the multipart case, and are able to cast to
 * MultipartHttpServletRequest if they need to.
 */
@Override
protected void doFilterInternal(
		HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	MultipartResolver multipartResolver = lookupMultipartResolver(request);

	HttpServletRequest processedRequest = request;
	if (multipartResolver.isMultipart(processedRequest)) {
		if (logger.isDebugEnabled()) {
			logger.debug("Resolving multipart request [" + processedRequest.getRequestURI() +
					"] with MultipartFilter");
		}
		processedRequest = multipartResolver.resolveMultipart(processedRequest);
	}
	else {
		// A regular request...
		if (logger.isDebugEnabled()) {
			logger.debug("Request [" + processedRequest.getRequestURI() + "] is not a multipart request");
		}
	}

	try {
		filterChain.doFilter(processedRequest, response);
	}
	finally {
		if (processedRequest instanceof MultipartHttpServletRequest) {
			multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
		}
	}
}