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

Leo Le
  • 775
  • 3
  • 10
  • 29
  • Do you need your files to be sent to server in JSON format? Here is an example, how you supposed to be doing it: https://spring.io/guides/gs/uploading-files/ – mavarazy Aug 22 '15 at 10:13

1 Answers1

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
Community
  • 1
  • 1
mavarazy
  • 7,208
  • 1
  • 30
  • 56