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

标签 java spring web-services httprequest multipart

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

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())));
    }
}

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

这是我设置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);

我的 Controller 方法声明是

String index(@RequestParam("file") MultipartFile 上传文件)

服务器报错

[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。

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

最佳答案

验证您是否有这些元素:

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

关于java - Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分 'file' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39705337/

相关文章:

具有参数化类型的 Java getConstructor(types)

java - 运行时未找到方面

xml - W3 和 xmlsoap.org 架构之间有什么区别?

web-services - SharePoint 2013 中的自定义 Web 服务与模拟

java - 未在 Micronaut 项目中创建 jOOQ DSLContext Bean

java - 获取时间戳的部分秒差异

java - 使用 Spring 在 AspectJ 类中检索 bean

java - 如何使用 spring 集成手动确认 rabbitmq 消息

java - SpringBoot @WebMvcTest 安全问题

iphone - 在 iPhone 上使用 Objective-C 使用 WCF Web 服务