【问题标题】:The request was rejected because no multipart boundary was found in springboot请求被拒绝,因为在 spring boot 中没有找到 multipart 边界
【发布时间】:2016-06-30 13:08:51
【问题描述】:

我正在尝试使用带有邮递员 Chrome 插件的 Spring Boot 和 Web 服务。

在邮递员content-type="multipart/form-data" 中,我收到以下异常。

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; 
nested exception is java.io.IOException: 
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

在控制器中我指定了以下代码

@ResponseBody
@RequestMapping(value = "/file", headers = "Content-Type= multipart/form-data", method = RequestMethod.POST)

public String upload(@RequestParam("name") String name,
        @RequestParam(value = "file", required = true) MultipartFile file)
//@RequestParam ()CommonsMultipartFile[] fileUpload
{
    // @RequestMapping(value="/newDocument", , method = RequestMethod.POST)
    if (!file.isEmpty()) {
        try {
            byte[] fileContent = file.getBytes();
            fileSystemHandler.create(123, fileContent, name);
            return "You successfully uploaded " + name + "!";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

这里我指定文件处理程序代码

public String create(int jonId, byte[] fileContent, String name) {
    String status = "Created file...";
    try {
        String path = env.getProperty("file.uploadPath") + name;
        File newFile = new File(path);
        newFile.createNewFile();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile));
        stream.write(fileContent);
        stream.close();
    } catch (IOException ex) {
        status = "Failed to create file...";
        Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return status;
}

【问题讨论】:

  • 我也面临同样的问题,它只能在邮递员中工作,不能与“高级休息客户端”等其他工具一起使用。我可以知道为什么吗??
  • @Narendhran,我们现在可以从 ARC 上传文件,这将消除这个问题。请检查:stackoverflow.com/a/59342761/3519504

标签: java web-services file-upload spring-boot multifile-uploader


【解决方案1】:

问题是你自己设置Content-Type,让它为空。谷歌浏览器会为你做这件事。多部分Content-Type 需要知道文件边界,当您删除Content-Type 时,Postman 会自动为您完成。

【讨论】:

  • 谢谢。这在邮递员中对我有用。此外,tomeokin 的回答有助于理解 Postman 并不适合所有测试场景。
  • 当我删除内容类型时,我得到这个“org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain' not supported”
  • 当我删除 Content-Type 标头时,服务器没有收到数据参数(即有效负载)。
  • 也许 Postman 自 2016 年以来改变了处理方式
  • 谢谢!它解决了我的问题!
【解决方案2】:

取消选中 Postman 中的内容类型,postman 会根据您在运行时输入的内容自动检测内容类型。

【讨论】:

    【解决方案3】:

    这对我有用: 通过 Postman 上传文件到 SpringMVC 后端 webapp:

    后端:

    邮递员:

    【讨论】:

      【解决方案4】:

      我在从 Postman 发出 POST 请求时遇到了同样的问题,后来我可以通过设置一个自定义的 Content-Type 来解决这个问题,并像这样设置边界值。

      我认为人们可能会遇到类似的问题,因此,我正在分享我的解决方案。

      【讨论】:

      • 你是怎么得到边界值的?
      • API 文档中建议的边界值。
      【解决方案5】:

      听说你可以在邮递员中做到这一点:

      【讨论】:

        【解决方案6】:

        “Postman - REST 客户端”不适合通过设置 content-type 来进行 post 操作。您可以尝试使用“高级 REST 客户端”或其他方式。

        此外,自 Spring 3.1 M2 起,标头已被消耗和生成替换,请参阅 https://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements。并且可以直接使用produces = MediaType.MULTIPART_FORM_DATA_VALUE

        【讨论】:

        • 这是一个非常有用的答案。它解决了我的问题。使用 Advanced REST 客户端,我可以发送与 Postman 相同的请求。 Postman 请求导致错误 org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was foundHTTP 405
        【解决方案7】:

        我遇到了这个问题,因为我使用了基于 axios 编写的 request.js
        我已经在 request.js 中设置了 defaults.headers

        import axios from 'axios'
        const request = axios.create({
          baseURL: '', 
          timeout: 15000 
        })
        service.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
        

        这是我解决这个问题的方法
        而不是

        request.post('/manage/product/upload.do',
              param,config
            )
        

        我用axios直接发送请求,没有加config

        axios.post('/manage/product/upload.do',
              param
            )
        

        希望这可以解决您的问题

        【讨论】:

          【解决方案8】:

          较新版本的 ARC(Advaded Rest 客户端)也提供文件上传选项:

          【讨论】:

            【解决方案9】:

            当我使用邮递员将 5.6M 的文件发送到外部网络时,我遇到了同样的问题。同样的动作在我自己的电脑和本地测试环境上也成功了。

            在检查了所有服务器配置和 HTTP 标头后,我发现原因是 Postman 在模拟对外部 HTTP 请求的请求时可能会遇到一些问题。 最后,我在 chrome HTML 页面上成功完成了 sendfile 请求。 仅供参考:)

            【讨论】:

              【解决方案10】:

              您可以尝试使用以下简单代码,它应该可以工作。我在 Advanced REST Client 上进行了测试,下面附上的截图将有助于配置。

              package com.example.demo;
              
              import java.io.FileOutputStream;
              import java.io.IOException;
              
              import org.springframework.web.bind.annotation.PostMapping;
              import org.springframework.web.bind.annotation.RequestParam;
              import org.springframework.web.bind.annotation.RestController;
              import org.springframework.web.multipart.MultipartFile;
              
              @RestController
              public class ImageUploadController {
              
                  @PostMapping("image-upload")
                  public String uploadImage(@RequestParam MultipartFile myFile) throws IOException {
                      
                      byte[] bytes = myFile.getBytes();
              
                      FileOutputStream fileOutputStream = new FileOutputStream("image.jpg");
              
                      fileOutputStream.write(bytes);
                      fileOutputStream.close();
                      
                      return "Image uploaded successfully..!";
              
                  }
              }
              

              您可以在项目的以下位置找到上传的图片。

              另外请注意,如果您的控制器应该在 @SpringBootApplication 包的包中。您可以参考下图。

              【讨论】:

                猜你喜欢
                • 2013-07-02
                • 2019-03-18
                • 1970-01-01
                • 2019-06-22
                • 1970-01-01
                • 2020-08-27
                • 2020-11-21
                • 1970-01-01
                • 2013-05-10
                相关资源
                最近更新 更多