cURL command to upload file does not upload file

Saturnian :

So I have a Java API and I use cURL commands to send GET/POST requests to my API. I'm using Springboot in my Java API. My cURL command looks like this:

curl -X POST \
  https://localhost:8443/abc/code/v \
  -H 'Content-Type: multipart/form-data' \
  -F file=@/path/to/file.txt

and my Java Springboot method that receives this request looks like this:

@RequestMapping(method = RequestMethod.POST, value = "/{code}/{v}")
    public ResponseEntity<Object> uploadTemplate( //
            @ApiParam(value = "code desc") //
            @PathVariable final String code, //
            @ApiParam(value = "v desc") //
            @PathVariable final String v, //
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        //log.info("received [url: /tabc/" + code + "/" + v + ", method: POST");
        System.out.println("file: "+ file.getName());
}

which prints out "file: file"

It doesn't look like file.txt is uploaded to the server at all! What am I doing wrong? I've looked up on Google but it looks like I'm doing nearly everything.

Marco R. :

The MultipartFile.getName returns the name of the parameter in the multipart form.

To get the bytes of the MultipartFile use file.getBytes()

And if your MultipartFile is a text file you could use this utility method to retrieve a list of the lines in that text file:

    public List<String> read(MultipartFile file) throws IOException {
        InputStreamReader in = new InputStreamReader(file.getInputStream());
        try (BufferedReader reader = new BufferedReader(in)) {
            return reader.lines().collect(Collectors.toList());
        }
    }

Complete code on GitHub

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95994&siteId=1