使用高级 rest 客户端的 Spring Boot 简单多部分文件上传 (Chrome)

SpringBoot simple multipart file upload with Advanced rest client (Chrome)

我想上传一张图片到文件系统。所以我正在使用 多部分文件上传 spring 引导。 而且我正在使用 Advance Rest Client(Chrome) 工具 POST 多部分文件。但是即使我没有指定任何内容类型,我也会遇到错误org.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界。

Here my rest controller code,

@RestController
public class StringController {
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file){

    String fileName = null;
    if (!file.isEmpty()) {
        try {
            fileName = file.getOriginalFilename();
            byte[] bytes = file.getBytes();
            BufferedOutputStream buffStream = 
                    new BufferedOutputStream(new FileOutputStream(new File("F:/" + fileName)));
            buffStream.write(bytes);
            buffStream.close();
            return "You have successfully uploaded " + fileName;
        } catch (Exception e) {
            return "You failed to upload " + fileName + ": " + e.getMessage();
        }
    } else {
        return "Unable to upload. File is empty.";
    }
  }
}

Screenshot (Advance rest client tool)

Error

{ "timestamp": 1490678908517, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.web.multipart.MultipartException", "message": "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", "path": "/upload" }

问题出在您对提前休息客户的请求中。它在上传 postman.The 图片时工作正常。试试邮递员你会得到它。

您在客户请求 header 的值 边界 中丢失了。 在 PostMan header "Content-Type" 中构造如下:

Content-Type : multipart/form-data;boundary="12312313132132"