0

I'm construct an uploading multiple images API by Spring Restful. This is my API method:

@RequestMapping(value = GiftURI.TEST_UPLOAD, method = RequestMethod.POST) public @ResponseBody String testUploadMultipleFiles(@RequestBody TestUploadImageReq request) throws BusinessException { String fileName = "test"; String filePath = "/Users/Dona/Desktop/"; System.out.println("My name is " + request.getMyName()); for (int i = 0; i < request.getImages().length; i++){ if (!request.getImages()[i].isEmpty()) { try { File newfile = new File(filePath+fileName+i+".jpg"); FileUtils.writeByteArrayToFile(newfile, request.getImages()[i].getBytes()); } catch (Exception e) { return "You failed to upload cause: " + e.toString(); } } } return "Finished"; } 

This is my TestUploadImageReq model:

public class TestUploadImageReq implements Serializable { private static final long serialVersionUID = 5284757625916162700L; public TestUploadImageReq(){} private MultipartFile[] images; private String myName; public String getMyName() { return myName; } public void setMyName(String myName) { this.myName = myName; } public MultipartFile[] getImages() { return images; } public void setImages(MultipartFile[] images) { this.images = images; } } 

However, when I used CURL command to test this API, I received the error "The request sent by the client was syntactically incorrect.". This is my CURL command:

curl -i -X POST -H "Content-Type:application/json" http://localhost:9190/rest/gift/test_upload -d '{"myName":"Dona", "image":"@/Users/Dona/Desktop/mcdona2.jpg"}' 

How can I test my API by curl command to upload multiple images to the server ? Thanks

1
  • Do you need your files to be sent to server in JSON format? Here is an example, how you supposed to be doing it: spring.io/guides/gs/uploading-files Commented Aug 22, 2015 at 10:13

1 Answer 1

3

Why standard way, does not work for you? http://spring.io/guides/gs/uploading-files

@Controller public class FileUploadController { @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file){ ... 

And you should be able to test it with

curl -F "userid=1" -F "filecomment=This is an image file" -F "image=@/home/user1/Desktop/test.jpg" localhost:8080/upload 

Using curl to upload POST data with files

If you need more meta, you can add Cookies to your upload Controller

@Controller public class FileUploadWithMetaController { @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String handleFileUpload( @RequestParam("name") String name, @RequestParam("file") MultipartFile file, @CookieValue(value = "META", defaultValue = "no") String metaCookie){ ... 

And CURL will look something like this:

curl --cookie "META=Yes" -F "userid=1" -F "filecomment=This is an image file" -F "image=@/home/user1/Desktop/test.jpg" localhost:8080/upload 
Sign up to request clarification or add additional context in comments.

6 Comments

Your suggest is ok. However, what if the TestUploadImageReq has a lot of attributes. Is Using RequestParam a good idea ?
You can use cookies to specify extra attributes, if you want.
Could you give an example about using cookies in this case, pls ? Thanks @mavarazy
Using RequestBody, I can pass objects to TestUploadImageReq model. How can I do it when using RequestParameter ? @mavarazy
You can create TestUploadImageReq with provided parameters
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.