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