Spring框架的org.springframework.web.multipart.support.MissingServletRequestPartException异常,表示需要的请求参数'file'不存在。

3

我正在尝试使用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 collection是一个ArrayList,它包含文件。

以下是我的设置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":"Required request part 'file' is not present","path":"/upload1"}

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

我对web服务还不太熟悉,可能会犯一些愚蠢的错误。请帮我解决这个问题,谢谢!


在变量名中不应使用 $Java 语言规范 §3.8:*$ 符号只应用于机械生成的源代码或者极少数情况下用于访问遗留系统上的预先存在的名称。* - Andreas
你确定 parts 不是空的吗? - Andreas
当我打印零件时,它显示"[org.apache.http.entity.mime.FormBodyPart@1fb3ebeb]",所以我认为它不是空的。 - Sarthak Jain
我已经将变量名从i$改为i。结果显示相同。 - Sarthak Jain
2个回答

3

请验证是否拥有以下物品:

@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.

-1

spring.io 上有一个非常好的例子 上传文件


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接