spring-boot REST POST API 发送文件

krs*_*888 3 java rest spring multipartform-data spring-boot

我是 spring rest 的新手,并尝试创建一个 REST POST API,用户可以在其中将文件发送到服务器。

@RequestMapping(value = "/order", method = RequestMethod.POST)
public String create(@RequestParam("file") MultipartFile file) {        
        System.out.println("---------INSIDE ORDER----------");
        return "file succesfully received!";
}
Run Code Online (Sandbox Code Playgroud)

但是当我通过上传 order.txt 文件并选择表单数据(在邮递员中)调用此 API 时,我收到此错误

{
  "timestamp": 1474129488458,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.multipart.support.MissingServletRequestPartException",
  "message": "Required request part 'file' is not present",
  "path": "/order"
}
Run Code Online (Sandbox Code Playgroud)

Rav*_*iga 6

问题不在于您接受请求的代码。这与您的要求有关。

-d用于传递数据。您必须使用-F如下所示

curl -X POST localhost:8080/order -F "file=@cooltext.txt"
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅curl 手册的帖子部分