java - Spring MVC : Bad request (parameter missing) on file upload even when required parameters are present

标签 java spring spring-mvc

我有一个文件上传 Controller ,其方法如下所示:

@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = {"*/*", "application/json"})
public @ResponseBody ScriptUploadResponse upload(@RequestParam("userId") Long userId, @RequestParam("script") MultipartFile file) {
    return scriptService.upload(userId, file);
}

这在 Spring 3 中使用基于 XML 的配置工作得很好。我最近使用 Spring 4 转移到基于 Java 的配置。当我上传文件时,我收到 400: Bad request ,并提示未提供 userId 。但是当我在浏览器中查看请求时,我看到的是:

------WebKitFormBoundaryoJhTJ817NockqUSY
Content-Disposition: form-data; name="userId"

1
------WebKitFormBoundaryoJhTJ817NockqUSY
Content-Disposition: form-data; name="script"; filename="script.js"
Content-Type: application/javascript


------WebKitFormBoundaryoJhTJ817NockqUSY--

Spring 声明:

HTTP Status 400 - Required Long parameter 'userId' is not present

当有效负载显示它存在时,为什么 Spring 说我没有提供 userId

更新

我已在 RequestParamMethodArgumentResolver.java(内部 Spring 类)中放置了断点,我可以在 HttpServletRequest 上看到 getParts()对象根本不返回任何部分。我不确定为什么会发生这种情况,但这似乎是问题的根源。从浏览器中我可以看到正在发出的请求,但无论出于何种原因,多部分数据都无法通过。

最佳答案

我能够弄清楚这一点。要启用对多部分文件的支持,您必须以某种方式进行配置。令人沮丧的是,这方面的文档很难找到,而且 Spring 的相关文档似乎不完整或者只与基于 XML 的配置相关。我不确定我是否只是找错了地方或者什么,但即使使用谷歌,我也无法找到一个地方来解释如何设置它。不管怎样,就这样吧。

您首先必须在 Web 配置中包含一个 Bean。我只是将以下内容添加到我的配置类(扩展 WebMvcConfigurerAdapter):

@Bean
public MultipartResolver multipartResolver() {
    return new StandardServletMultipartResolver();
}

但这还不够。如果您使用 Servlet 3.0 并且还使用基于 Java 的配置,则必须配置调度程序 servlet 以支持多部分文件:

我将以下类添加到我的初始化程序中(扩展了WebApplicationInitializer):

dispatcher.setMultipartConfig(
        new MultipartConfigElement("/tmp", 25 * 1024 * 1024, 125 * 1024 * 1024, 1 * 1024 * 1024)
);

整个方法最终看起来像这样:

@Override
public void onStartup(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(ApplicationConfig.class, WebConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));

    //Spring security
    servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false, "/*");

    //Enable multipart support
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    dispatcher.setMultipartConfig(
            new MultipartConfigElement("/tmp", 25 * 1024 * 1024, 125 * 1024 * 1024, 1 * 1024 * 1024)
    );
}

关于java - Spring MVC : Bad request (parameter missing) on file upload even when required parameters are present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25109945/

相关文章:

java - 包含函数链表

For 表达式中的 Java ":"运算符

java - 使用循环查找均值和标准差

java - Spring 启动 : Cors policy missing 'access-control-allow-origin'

java - 如何为JtextArea添加不同的字符集支持?

java - Spring @Transactional + jdbcTemplate 调用Web服务

java - 在 Maven 中配置 JUnit 目录结构

java - 构造函数注入(inject)的空指针异常并与字段注入(inject)mockito一起使用

spring - 由 : java. lang.IllegalArgumentException 引起:无法解析值 'some.other.property' 中的占位符 "${some.other.property}"

html - 非常简单的 spring MVC 按钮点击