开发者

How to get a MultipartHttpServletRequest from RequestContextHolder?

I have configured the access decision manager to check a request bef开发者_如何学JAVAore being processed by the servlet the key line is:-

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

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

I am using spring 2.5.


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

So:

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


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.

Here is nice tutorial on the same

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


Have you tried casting to MultipartHttpServletRequest?


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

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

in your app-config.xml.

This worked for me.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜