Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分'file'不存在

时间:2016-09-26 14:18:39

标签: java spring web-services httprequest multipart

我试图通过在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())));
    }
}

零件集合是一个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”: “所需的请求部分'文件'不存在”,“path”:“/ upload1”}

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

我对网络服务相当新,可能会犯一些愚蠢的错误。请提前帮助我

2 个答案:

答案 0 :(得分: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;
}

pplications.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 :(得分:0)

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