【问题标题】:Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not presentSpring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分“文件”不存在
【发布时间】:2016-09-26 14:18:38
【问题描述】:

我试图通过在 FormBodyPart 中使用它来将文件发送到控制器,而不是直接将文件发送给它。这是制作文件集合的代码

private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType)
        throws ClassificationException {
    if (inputFile == null) {
        throw new ClassificationException("Null input file provided");
    }
    if (!inputFile.exists()) {
        throw new ClassificationException("Input file not found: " + inputFile.getAbsolutePath());
    }
    if (fileType != null) {

        String charset = "UTF-8";
        parts.add(new FormBodyPart("file", new FileBody(inputFile, fileType, charset)));

    } else {
        parts.add(new FormBodyPart("file", new FileBody(inputFile, inputFile.getName())));
    }
}

Parts 集合是一个数组列表,其中包含文件。

这是我设置 Http Entity 的代码

HttpPost httppost = new HttpPost("http://localhost:9000/upload1");
            MultipartEntity reqEntity1 = new MultipartEntity();
            FormBodyPart part1;
            for (Iterator i$ = parts.iterator(); i$.hasNext(); reqEntity1.addPart(part1)) {
                part1 = (FormBodyPart) i$.next();
                System.out.println(part1.getHeader());
            }

            httppost.setEntity(reqEntity1);
            HttpResponse response = httpclient.execute(httppost);
            System.out.println(response);

我的控制器方法声明是

String index(@RequestParam("file") MultipartFile uploadfile)

我收到来自服务器的错误提示

[400] {"timestamp":1474898550131,"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message": "所需的请求部分'文件'不存在","path":"/upload1"}

我的 dispatcher.xml 已经包含了 multipartResolver 的 bean。

我对 Web 服务还很陌生,可能会犯一些愚蠢的错误。请帮帮我,在此先感谢

【问题讨论】:

  • 你不应该在变量名中使用$Java Language Specification §3.8$ 符号应仅用于机械生成的源代码中,或者很少用于访问旧系统上预先存在的名称。
  • 您确定parts 不为空吗?
  • 当我打印部件时,它显示“[org.apache.http.entity.mime.FormBodyPart@1fb3ebeb]”,所以我不认为它是空的。
  • 我确实将变量的名称从 i$ 更改为 i。它显示相同的结果。

标签: java spring web-services httprequest multipart


【解决方案1】:

验证您是否有这些物品:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipart = new CommonsMultipartResolver();
    multipart.setMaxUploadSize(3 * 1024 * 1024);
    return multipart;
}

@Bean
@Order(0)
public MultipartFilter multipartFilter() {
    MultipartFilter multipartFilter = new MultipartFilter();
    multipartFilter.setMultipartResolverBeanName("multipartResolver");
    return multipartFilter;
}

applications.properties:

# MULTIPART (MultipartProperties)
spring.http.multipart.enabled=true 
# Enable support of multi-part uploads.
# spring.http.multipart.file-size-threshold=3 # Threshold after which files will be written to disk. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.location= /
# Intermediate location of uploaded files.
spring.http.multipart.max-file-size=10MB
# Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.max-request-size=10MB
# Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.resolve-lazily=false 
# Whether to resolve the multipart request lazily at the time of file or parameter access.

【讨论】:

    【解决方案2】:

    spring.ioUploading Files中有一个很好的例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2019-09-08
      • 2017-10-09
      • 2020-10-22
      • 2019-07-11
      相关资源
      最近更新 更多