我正在尝试使用angular-file-upload在我的Angular Web应用程序中合并多个文件上传功能。目前,前端功能正常,但每次上载尝试都会抛出一个

java.lang.IllegalStateException,java.io.IOException]:
    java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest:
        is a MultipartResolver configured?

异常(exception)。

上载 Controller 定义为
@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
    }
 }

在我的AppConfig.java类中,我声明了bean
@Bean
public CommonsMultipartResolver commonsMultipartResolver(){
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setDefaultEncoding("utf-8");
    commonsMultipartResolver.setMaxUploadSize(50000000);
    return commonsMultipartResolver;
}

并使用以下命令启动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));
}

我不认为这是由于我的Angular View 中缺少<form>元素造成的,因为我可以看到Content-Tylemultipart/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--

请注意,当包含
@Bean
public MultipartConfigElement multipartConfigElement(){
    MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
    multipartConfigFactory.setMaxFileSize("10MB");
    multipartConfigFactory.setMaxRequestSize("50MB");
    return multipartConfigFactory.createMultipartConfig();
}

AppConfig.java中并取消注释该命令
servlet.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));

在我的servlet初始化程序类中。

任何帮助表示赞赏!

最佳答案

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

@Bean
public CommonsMultipartResolver multipartResolver(){

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

10-07 16:13