3
votes

I am trying to upload file through Intellij IDEA REST Client. I choose "File to upload (multipart/form-data)" and choose file to send. What is parameter name of this file? In my Sprint Controller i use this code

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

I also tried different names, such as "file", "fileToSend", "File to send" for @RequestParam, but Spring always cant find MultipartFile paramater.

3
Can you add your error log? - Rana_S
Did you find the solution? - nyxz
@nyxz No, i just found another rest client (for Google Chrome) in which i can set a parameter name of multipart file. I still don't know how set it in Intellij Rest Client. - hluhovskyi
I have the same problem - Juru
Ran into the same problem. Using a debugger, it appears that the name is actually the filename itself. Since there is no way for the controller to know this in advance, it can't be handled. - gregturn

3 Answers

9
votes

I use the following code which works for me:

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) {

For more information, please refer to https://www.jetbrains.com/help/ruby/exploring-http-syntax.html#use-multipart-form-data

1
votes

If you wish to do multipart file uploads inside IntelliJ IDEA's REST Client, please upvote this bug report => https://youtrack.jetbrains.com/issue/WEB-20197

1
votes

File upload is not allowed due to security concern, not for application running on local machine. This solution worked for me. Its based on the comment by vincent.

See below:

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

###