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

2022-09-01 14:20:13

我正在尝试使用角度文件上传在我的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
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 初始值设定项类中。

任何帮助是值得赞赏的!


答案 1

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

@Bean
public CommonsMultipartResolver multipartResolver(){

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

答案 2

只需将其添加到服务器上下文中即可.xml

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

没有其他需要。


推荐