我正在编写一个过滤器(也尝试使用HandlerInterceptor),并且需要访问MultiPart消息的各个部分-包括参数(表单字段)和文件。下面的示例代码仅向我提供标头和常规请求信息,对于任何正文/内容请求(例如, getParameter或getPart。
public class SampleFilter implements Filter {
@Override
public void doFilter (ServletRequest req, ServletResponse resp, FilterChain filterchain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
log.info(request.getContentType()); //Works OK
log.info(request.getHeader("X-Some-Header")); //Works OK
log.info(String.valueOf(request.getContentLength())); //Works OK
log.info(request.getParameter("some-form-field")); //Does NOT work
for (Part p : request.getParts()) //Does NOT work
{
log.info("Part:" + p.getName());
}
}
我已经创建了这个版本的HandlerInterceptor版本,因为我相信这会在DespatcherServlet处理完多部分结构之后在管道中发生。这使我能够从请求中获取表单字段/“参数”,但文件部分仍然没有运气。
public class SampleHandler extends HandlerInterceptorAdapter {
@Override
public boolean preHandle (HttpServletRequest request, HttpServletResponse resp, Object handler) throws Exception {
log.info(request.getContentType());
log.info(request.getHeader("X-sample-header"));
log.info(String.valueOf(request.getContentLength()));
for (String msg : request.getParameterValues("sample-field"))
{
log.info(msg);
}
log.info(String.valueOf(request.getParts().size())); // Shows 0
for (Part p : request.getParts())
{
log.info("Part:" + p.getName());
}
return true;
到目前为止,我发现的大多数示例都涉及到普通控制器中的多部分功能(请参阅下文,我已经知道了)-我专门在寻找Filter或HandlerInterceptor或类似的预控制器钩子。
我有一个成功实现MultiPart功能的RestController,但是不确定如何将此方法或类似方法应用于Filter:
@RestController
@RequestMapping (value = {"/**"})
@Slf4j
public class SampleController {
@RequestMapping(value = {"/**"}, method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public ResponseEntity<String> SampleMultipartController(@RequestHeader Map<String, String> httpheaders,
@RequestParam("param1") String param1,
@RequestParam("param2") List<String> param2,
@RequestParam ("file") List<MultipartFile> files) throws IOException {
//etc...
我定义了一个Commons multipartResolver:
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
final int MAX_UPLOAD_SIZE = (100 * 1024 * 1024);
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE + MAX_UPLOAD_SIZE/5);
multipartResolver.setMaxUploadSizePerFile(MAX_UPLOAD_SIZE);
multipartResolver.setMaxInMemorySize(MAX_UPLOAD_SIZE/100);
return multipartResolver;
}
最佳答案
我会在doFilter
中执行以下操作:
@Override
public void doFilter (ServletRequest req, ServletResponse resp, FilterChain filterchain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
URI uri = new URI(request.getRequestURI());
Map<String, String> map = Arrays.stream(uri.getRawQuery().split("&"))
.collect(Collectors.toMap(param -> param.split("=")[0], param -> param.split("=")[0]));
}
然后,在地图中输入参数。