该请求被拒绝,因为没有在springboot中找到多部分边界

问题描述:

因为我正在尝试使用Spring引导和Web服务与邮递员Chrome附加组件。该请求被拒绝,因为没有在springboot中找到多部分边界

在邮递员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 

在控制器I指定下面的代码

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

我也面临同样的问题,而且它在邮递员工作中的唯一工作就是不工作ith其他工具,如“高级休息客户端”。我可以知道为什么吗? – Narendhran

问题是不是在你的代码.problem。你在多部分请求中没有得到任何有限的边界。

+1

如何获取? –

+0

其服务器错误 500是用于服务器 没有boundry集时,服务器接收图像 –

问题是你自己设置Content-Type,让它变成空白。谷歌浏览器会为你做。多部分内容类型需要知道文件边界,并且当您删除内容类型时,邮差会自动为您执行此操作。

+2

谢谢。这在Postman中适用于我。此外,来自tomeokin的回答有助于说明邮差不适合所有的测试场景。 –

+0

谢谢解决我的问题 – erhun

+0

谢谢解决我的问题了... –

的“邮差 - REST客户端”是不适合做后行动设置content-type.You可以尝试使用“高级REST客户端”或其他。

此外,头被消耗更换和生产,因为春季3.1 M2,见https://spring.io/blog/2011/06/13/spring-3-1-m2-spring-mvc-enhancements。你可以直接使用produces = MediaType.MULTIPART_FORM_DATA_VALUE

我有同样的问题,同时使从邮差POST请求,后来我可以通过设置自定义的内容类型与随之像这样设定的边界值解决问题。

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

postman

中未经检查邮差内容类型和邮递员自动检测基于在运行时你输入的内容类型。

Sample

+0

为我工作,谢谢! – mangotang

这为我工作: 上传通过邮差文件,到用SpringMVC后端web应用:

后端: Endpoint controller definition

邮差: Headers setup POST Body setup