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

Moh*_*ved 44 java web-services file-upload multifile-uploader spring-boot

正如我正在尝试使用带有postman chrome附加组件的spring boot和webservices.

在邮递员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
Run Code Online (Sandbox Code Playgroud)

在Controller中我指定了以下代码

@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.";
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

de.*_*.ru 76

问题是你自己设置Content-Type,让它为空.谷歌Chrome将为您完成.multipart Content-Type需要知道文件边界,当你删除Content-Type时,Postman会自动为你做.

  • 当我删除内容类型时,我收到此“org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型‘文本/纯文本’” (4认同)
  • 谢谢.这对Po​​stman来说很有用.此外,tomeokin的回答有助于感觉,Postman并不适合所有测试场景. (2认同)
  • 当我删除 Content-Type 标头时,服务器不会收到数据参数(即有效负载)。 (2认同)

小智 16

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

样品


gor*_*anz 11

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

后端: 端点控制器定义

邮差: 标题设置 POST Body设置