Spring文件上传 - 获得预期的MultipartHttpServletRequest:是否配置了MultipartResolver?错误

时间:2014-11-20 21:36:28

标签: java angularjs spring file-upload

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

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

异常。

上传控制器定义为

@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视图中缺少<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初始化程序类中。

感谢任何帮助!

3 个答案:

答案 0 :(得分:30)

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

@Bean
public CommonsMultipartResolver multipartResolver(){

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

答案 1 :(得分:5)

只需将其添加到servler-context.xml

即可
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize" value="268435456"/>
</bean>

没有其他需要。

答案 2 :(得分:-1)

您必须在webmvc-context.xml中添加:

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