Java Spring 文件上传 - 获得预期的 MultipartHttpServletRequest:是否配置了 MultipartResolver?错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27050018/
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
Spring file upload - getting Expected MultipartHttpServletRequest: is a MultipartResolver configured? error
提问by Jason
I'm trying to incorporate multiple file upload functionality in my Angular web application using angular-file-upload. Currently, the front end functionality works, but each upload attempt throws a
我正在尝试使用angular-file-upload在我的 Angular Web 应用程序中合并多个文件上传功能。目前,前端功能有效,但每次上传尝试都会抛出一个
java.lang.IllegalStateException,java.io.IOException]:
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest:
is a MultipartResolver configured?
exception.
例外。
The Upload Controller is defined as
上传控制器定义为
@Controller
@PropertySource("classpath:application.properties")
public class FileUploadController {
@Resource
private Environment env;
@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
@ResponseBody
public List<String> fileUpload(@RequestParam("file") MultipartFile[] uploadFiles) throws IllegalStateException, IOException {
//file processing logic
}
}
In my AppConfig.java
class, I declare the bean
在我的AppConfig.java
班级中,我声明了 bean
@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("utf-8");
commonsMultipartResolver.setMaxUploadSize(50000000);
return commonsMultipartResolver;
}
and start the web application with
并使用以下命令启动 Web 应用程序
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
ctx.setServletContext(servletContext);
ctx.refresh();
Dynamic servlet = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
//servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
}
I do not think it is due to the lack of a <form>
element in my Angular view, because I can see that Content-Tyle
is multipart/form-data
and that the Request Payload is set appropriately.
我不认为这是由于缺乏一个的<form>
在我的角度视图元素,因为我可以看到,Content-Tyle
是multipart/form-data
和请求负载设置为合适。
Remote Address:192.168.33.10:80
Request URL:http://dev.jason.com/rest/fileupload
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:415235
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryBHlsldPQysTVpvwZ
Host:dev.jason.com
Origin:http://dev.jason.com
Pragma:no-cache
Referer:http://dev.jason.com/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36
Request Payload
------WebKitFormBoundaryBHlsldPQysTVpvwZ
Content-Disposition: form-data; name="file"; filename="IMG_5072.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryBHlsldPQysTVpvwZ--
Note that this issue still occurs when including
请注意,包含时仍然会出现此问题
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
multipartConfigFactory.setMaxFileSize("10MB");
multipartConfigFactory.setMaxRequestSize("50MB");
return multipartConfigFactory.createMultipartConfig();
}
in AppConfig.java
and uncommenting the command
在AppConfig.java
命令中取消注释
servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));
in my servlet initializer class.
在我的 servlet 初始值设定项类中。
Any help is appreciated!
任何帮助表示赞赏!
采纳答案by Sotirios Delimanolis
Spring's MVC stack requires that the MultipartResolver
bean be called multipartResolver
. Change your @Bean
method to
Spring 的 MVC 堆栈要求MultipartResolver
调用 bean multipartResolver
。将您的@Bean
方法更改为
@Bean
public CommonsMultipartResolver multipartResolver(){
or
或者
@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
回答by Patrice
Just add this to servler-context.xml
只需将此添加到 servler-context.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="268435456"/>
</bean>
Nothing else needed.
其他什么都不需要。
回答by Flavio Troia
You have to add in webmvc-context.xml this:
您必须在 webmvc-context.xml 中添加以下内容:
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/>