Java 请求被拒绝,因为在springboot中找不到多部分边界

Java 请求被拒绝,因为在springboot中找不到多部分边界,java,web-services,file-upload,spring-boot,multifile-uploader,Java,Web Services,File Upload,Spring Boot,Multifile Uploader,当我尝试使用spring boot和带有postman chrome插件的WebService时 在postmancontent type=“multipart/form data”中,我得到了以下异常 HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart se

当我尝试使用spring boot和带有postman chrome插件的WebService时

在postman
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;
}

问题是您自己正在设置
内容类型
,让它为空。谷歌Chrome会帮你做的。多部分
内容类型
需要知道文件边界,当您删除
内容类型
时,邮递员将自动为您执行此操作

通过设置内容类型,“Postman-REST客户端”不适合执行post操作。您可以尝试使用“高级REST客户端”或其他


此外,从Spring 3.1 M2开始,收割台被消耗和生产所取代,请参见。您可以直接使用
products=MediaType.MULTIPART\u FORM\u DATA\u VALUE

我在向邮递员发出POST请求时遇到了同样的问题,后来我可以通过设置自定义内容类型并设置边界值来解决这个问题,就像这样

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


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

这对我很有效: 通过Postman将文件上载到SpringMVC后端Web应用程序:

后端:

邮递员:

当我使用邮递员向外部网络发送一个5.6M的文件时,我遇到了同样的问题。在我自己的计算机和本地测试环境中,同样的操作也成功了

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

我遇到了这个问题,因为我使用了基于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直接发送请求,没有添加配置

axios.post('/manage/product/upload.do',
      param
    )
希望这可以解决您的问题

较新版本的ARC(Advaced Rest client)还提供了文件上载选项:


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


谢谢。这对我在《邮递员》中的工作很有效。此外,来自tomeokin的回答有助于理解,Postman不适用于所有测试场景。当我删除内容类型时,我会得到“org.springframework.web.HttpMediaTypeNotSupportedException:content type'text/plain'notsupported”当我删除内容类型标头时,服务器不会收到数据参数(这是有效载荷)。也许邮递员从2016年起改变了处理它的方式太棒了!这对我来说很有效。ThanksI也面临着同样的问题,它在邮递员中的唯一工作就是不使用其他工具,如“AdvanceRESTClient”。我可以知道为什么吗???@Narendhran,我们现在可以从ARC上传文件,这将消除此问题。请检查:这是非常有用的答案。它解决了我的问题。使用高级REST客户端,我可以发送与我尝试使用Postman时相同的请求。Postman请求导致错误
org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为未找到多部分边界
HTTP 405
您是如何获得边界值的?API文档中建议了边界值。