我有一个用Spring Security和OAuth2保护的@Controller,我试图让我的用户上传一个文件:
@Controller
@RequestMapping(value = "/api/image")
public class ImageController {
@PreAuthorize("hasAuthority('ROLE_USER')")
@RequestMapping(value = "/upload", method = RequestMethod.PUT)
public @ResponseBody Account putImage(@RequestParam("title") String title, MultipartHttpServletRequest request, Principal principal){
// Some type of file processing...
System.out.println("-------------------------------------------");
System.out.println("Test upload: " + title);
System.out.println("Test upload: " + request.getFile("file").getOriginalFilename());
System.out.println("-------------------------------------------");
return ((Account) ((OAuth2Authentication) principal).getPrincipal());
}
}当我尝试上传文件和标题时,我得到了以下异常。我将Content-Type标头设置为multipart/form-data。
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.connector.RequestFacade@1aee75b7]]
at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.java:84)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)如何在Spring Security背后上传文件?看起来请求永远不会转换成MultiPartHttpServerRequest,所以它不能工作?
如果我更改我的方法签名以获取@RequestParam MultipartFile,那么我会得到一个异常,如下所示:
DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'imageController'
DEBUG ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG ResponseStatusExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG DefaultHandlerExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG DispatcherServlet - Could not complete request
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.util.Assert.notNull(Assert.java:112)我的...but中确实配置了一个MultipartResolver:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="268435456"/> <!-- 256 megs -->
</bean>我确实看过this blog post about getting this working under Spring 3.0 --但我正在努力保持更新,目前使用的是3.1。有没有更新过的补丁?
发布于 2013-02-26 04:02:01
问题是我使用的是PUT而不是POST。Commons FileUpload被硬编码为只接受文件的POST请求。
检查那里的isMultipartContent method。要解决此问题,请使用POST或扩展该类,并覆盖该方法以按您喜欢的方式工作。
我为这个问题打开了FILEUPLOAD-214。
发布于 2013-07-07 03:50:37
为了解决这个问题,请不要使用spring MultiPartHttpServerRequest,而是将请求作为HttpServletRequest,使用apache commons fileupload库解析来自PUT方法的请求,并对文件进行处理。下面是一些示例代码:
ServletFileUpload fileUpload = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> fileItems = fileUpload.parseRequest(httpServletRequest);
InputStream in = fileItems.get(0).getInputStream();
...发布于 2014-07-22 02:53:09
在Config.groovy中
确保启用了multipart,
// whether to disable processing of multi part requests
grails.web.disable.multipart=false在控制器中添加Post方法
def upload(){
MultipartHttpServletRequest mpr = (MultipartHttpServletRequest)request;
if(request instanceof MultipartHttpServletRequest)
{
CommonsMultipartFile f = (CommonsMultipartFile) mpr.getFile("myFile");
println f.contentType
f.transferTo()
if(!f.empty)
flash.message = 'success'
else
flash.message = 'file cannot be empty'
}
else
flash.message = 'request is not of type MultipartHttpServletRequest'}有了这些,我可以上传文件,没有任何相关的Spring Security。
https://stackoverflow.com/questions/15058548
复制相似问题