Intellij IDEA REST Client 文件上传

hlu*_*kyi 2 rest spring intellij-idea

我正在尝试通过 Intellij IDEA REST 客户端上传文件。我选择“要上传的文件(多部分/表单数据)”并选择要发送的文件。这个文件的参数名是什么?在我的 Sprint 控制器中,我使用此代码

@RequestMapping(method = RequestMethod.POST, value = "/{id}/photo")
public void setCover(@PathVariable int id,
                     @RequestParam MultipartFile file) {
    System.out.println(file.getName());
}
Run Code Online (Sandbox Code Playgroud)

我还为@RequestParam 尝试了不同的名称,例如“file”、“fileToSend”、“File to send”,但 Spring 始终找不到 MultipartFile 参数。

vin*_*ent 7

我使用以下对我有用的代码:

POST http://localhost:9003/goods/add HTTP/1.1
Content-Type: multipart/form-data; boundary=boundary

--boundary
Content-Disposition: form-data; name="file"; filename="file.csv"

// The 'input.txt' file will be uploaded
< /Users/xing/Desktop/file.csv
--boundary
Content-Disposition: form-data; name="advertType"

1
--boundary

// The method in the Spring controller
public Xxxx add(@RequestParam("file") MultipartFile file, Long advertType) {
Run Code Online (Sandbox Code Playgroud)

更多信息请参考https://www.jetbrains.com/help/ruby/exploring-http-syntax.html#use-multipart-form-data


小智 7

出于安全考虑,不允许上传文件,不适用于在本地计算机上运行的应用程序。这个解决方案对我有用。它基于文森特的评论。

见下文:

POST http://localhost:8080/api/test HTTP/1.1
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="param1"; filename="myfile.csv"
Content-Type: application/csv

 // below will the path to the file (i.e. myfile.csv)
< C:/users/user/data/sample/myfile.csv
--WebAppBoundary
Content-Disposition: form-data; name="param2"

 // value of param2
test
--WebAppBoundary

###
Run Code Online (Sandbox Code Playgroud)