简体   繁体   English

Spring org.springframework.web.multipart.support.MissingServletRequestPartException,所需的请求部分“文件”不存在

[英]Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not present

I am trying to send a file to the controller by using it in FormBodyPart, rather than directly sending the file to it.我试图通过在 FormBodyPart 中使用它来将文件发送到控制器,而不是直接将文件发送给它。 Here is the code for making a collection of files这是制作文件集合的代码

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 is an arraylist, which would contain the files.零件集合是一个数组列表,其中将包含文件。

Here is my code for setting Http Entity这是我设置 Http 实体的代码

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

My method declaration of controller is我的控制器方法声明是

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

I am getting an error from the server stating我收到来自服务器的错误说明

[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"} [400] {"timestamp":1474898550131,"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"必需的请求部分“文件”不存在","path":"/upload1"}

My dispatcher.xml already contains bean of multipartResolver.我的 dispatcher.xml 已经包含了 multipartResolver 的 bean。

I am fairly new to web services and might be doing some kind of silly mistake.我对网络服务相当陌生,可能会犯一些愚蠢的错误。 Please help me out,thanks in advance请帮助我,提前致谢

Verify if you have this items:验证您是否有以下物品:

@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;
}

and in the applications.properties :并在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.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“图像”不存在] - [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present] MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot 如何修复 Spring 引导分段上传中的“所需请求部分不存在” - How to fix "Required request part is not present" in a Spring Boot multipart upload React Spring 使用多部分表单数据启动应用程序 - 所需的请求部分“文件”不存在 - React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present org.springframework.web.multipart.MultipartException:当前请求不是多部分请求 spring boot - org.springframework.web.multipart.MultipartException: Current request is not a multipart request spring boot Spring 文件上传 - “所需的请求部分不存在” - Spring File Upload - 'Required request part is not present' Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present jQuery Ajax文件upload:org.springframework.web.bind.MissingServletRequestParameterException:必需的字符串参数&#39;upload&#39;不存在 - jQuery Ajax file upload:org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'upload' is not present 当前请求的类型不是[org.springframework.web.multipart.MultipartHttpServletRequest] - Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM