Java 如何从 RequestContextHolder 获取 MultipartHttpServletRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6628510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:23:14  来源:igfitidea点击:

How to get a MultipartHttpServletRequest from RequestContextHolder?

javaspring-mvcmultipartform-data

提问by jaseFace

I have configured the access decision manager to check a request before being processed by the servlet the key line is:-

我已将访问决策管理器配置为在被 servlet 处理之前检查请求,关键行是:-

HttpServletRequest request = (HttpServletRequest) RequestContextHolder.currentRequestAttributes().getRequest(); 

All good. However when the request is enctype="multipart/form-data"how do I get hold of the MultipartHttpServletRequestwhen RequestContextHolder.currentRequestAttributes().getRequest()only returns HttpServletRequest?

都好。然而当请求enctype="multipart/form-data"我怎么得到的保持MultipartHttpServletRequest时,RequestContextHolder.currentRequestAttributes().getRequest()只有收益HttpServletRequest

I am using spring 2.5.

我正在使用 spring 2.5。

回答by matt b

Have you tried casting to MultipartHttpServletRequest?

你试过投射到MultipartHttpServletRequest吗?

回答by skaffman

MultipartHttpServletRequestis n Spring-specific interface for handling multipart form submissions. The default implementation is DefaultMultipartHttpServletRequest, which has a constructor that takes a HttpServletRequest.

MultipartHttpServletRequest是用于处理多部分表单提交的特定于 Spring 的接口。默认实现是DefaultMultipartHttpServletRequest,它有一个带有HttpServletRequest.

So:

所以:

HttpServletRequest originalRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
MultipartHttpServletRequest multiPartRequest = new DefaultMultipartHttpServletRequest(originalRequest);

回答by vcg

If you are using spring-mvc, make sure you put this line

如果您使用的是 spring-mvc,请确保放置此行

<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

in your app-config.xml.

在您的 app-config.xml 中。

This worked for me.

这对我有用。

回答by varobj

I don't think you can get DefaultMultipartHttpServletRequest from RequestContextHolder. DefaultMultipartHttpServletRequest really implements HttpServletRequest. But there're 2 request instances if you use CommonsMultipartResolver. One is DefaultMultipartHttpServletRequest instance, and another is HttpServletRequest instance. Actually I don't know how to get the first instance from RequestContextHolder. You can get the second instance from it.

我认为您无法从 RequestContextHolder 获得 DefaultMultipartHttpServletRequest。DefaultMultipartHttpServletRequest 真正实现了 HttpServletRequest。但是如果您使用 CommonsMultipartResolver,则有 2 个请求实例。一个是 DefaultMultipartHttpServletRequest 实例,另一个是 HttpServletRequest 实例。实际上我不知道如何从 RequestContextHolder 获取第一个实例。您可以从中获取第二个实例。

回答by Anant Laxmikant Bobde

Apart from having

除了拥有

<form method=<method> action=<url> enctype="multipart/form-data"></form>

you have to have

你必须有

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

in your spring configuration file.

在您的 spring 配置文件中。

Here is nice tutorial on the same

这是关于相同的很好的教程

http://techdive.in/spring/spring-file-upload

http://techdive.in/spring/spring-file-upload