CommonsMultipartResolver

CommonsMultipartResolver

本文介绍了春天文件上传 - 获得期望MultipartHttpServletRequest:是一个MultipartResolver配置?错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用将Angular Web应用程序中的多个文件上传功能。目前,前端功能可以正常工作,但是每次上传都会抛出一个

  java.lang.IllegalStateException,java.io.IOException] :
java.lang.IllegalArgumentException:期望MultipartHttpServletRequest:
是一个MultipartResolver配置?

例外。

上传控制器定义为

  @Controller 
@PropertySource(classpath:application.properties)
public class FileUploadController {

@Resource
private环境env;

@RequestMapping(value =/ fileupload,method = RequestMethod.POST)
@ResponseBody
public List< String> fileUpload(@RequestParam(file)MultipartFile [] uploadFiles)抛出IllegalStateException,IOException {
//文件处理逻辑
}
}

在我的 AppConfig.java 类中,我声明了bean



pre
$ b $ public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding(utf-8);
commonsMultipartResolver.setMaxUploadSize(50000000);
返回commonsMultipartResolver;

$ / code>

然后用
$ b启动web应用程序$ b

  @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();

动态servlet = servletContext.addServlet(SERVLET_NAME,新的DispatcherServlet(ctx));
servlet.addMapping(/);
servlet.setLoadOnStartup(1);
//servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));



$ b我不认为这是由于缺少< form> 元素,因为我可以看到 Content-Tyle multipart / form -data ,请求有效负载设置得当。

 远程地址:192.168.33.10:80 
请求URL:http://dev.jason.com/rest/fileupload
请求方法:POST
状态码:500内部服务器错误
请求标题
接受:* / *
Accept-Encoding:gzip,deflate
Accept-Language:zh-CN,en; q = 0.8
Cache-Control:no-cache
连接:keep -alive
Content-Length:415235
Content-Type:multipart / form-data; border = ---- WebKitFormBoundaryBHlsldPQysTVpvwZ
Host:dev.jason.com
Origin:http://dev.jason.com
Pragma:no-cache
Referer:http: //dev.jason.com/
用户代理:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_10_1)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 38.0.2125.122 Safari / 537.36
Payload
------ WebKitFormBoundaryBHlsldPQysTVpvwZ
Content-Disposition:form-data; NAME = 文件; filename =IMG_5072.jpg
Content-Type:image / jpeg

------ WebKitFormBoundaryBHlsldPQysTVpvwZ--

请注意,包含

  @Bean 
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
multipartConfigFactory.setMaxFileSize(10MB);
multipartConfigFactory.setMaxRequestSize(50MB);
返回multipartConfigFactory.createMultipartConfig();

$ / code>

AppConfig.java

  servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));在我的servlet初始化类中,



任何帮助的赞赏!

解决方案

Spring的MVC堆栈要求 MultipartResolver bean被称为 multipartResolver 。将您的 @Bean 方法更改为

  @Bean 
public CommonsMultipartResolver multipartResolver(){



<$ p
$ public $ CommonsMultipartResolver commonsMultipartResolver(){


($ name =multipartResolver

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

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

@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setDefaultEncoding("utf-8");
    commonsMultipartResolver.setMaxUploadSize(50000000);
    return commonsMultipartResolver;
}

and start the web application with

@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.

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

servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));

in my servlet initializer class.

Any help is appreciated!

解决方案

Spring's MVC stack requires that the MultipartResolver bean be called multipartResolver. Change your @Bean method to

@Bean
public CommonsMultipartResolver multipartResolver(){

or

@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){

这篇关于春天文件上传 - 获得期望MultipartHttpServletRequest:是一个MultipartResolver配置?错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:57