Spring REST Upload file as binary

2019-07-21 02:20发布

问题:

I'm using spring. I want to implement rest controller to upload file to server. I found a lot examples like this:

public ResponseEntity doSomething(@PathVariable String paramOne, @RequestParam(required = false, name="file") List<MultipartFile> attachments
                                            ) throws IOException {
//Some logic here
}

Then I test it with postman, I create a request of type "form-data", add pram name "file", select type file, and select file. And it works ok.

It creates a post request as multipart request. But for some reasons I don't want to use multipart post request. So I want to upload file by select in postman type "binary". So my questions:

  1. Can spring somehow map this kind of request, so I get input file as param in my handler method? (I know that I can get HttpServletRequest and get InputStream from it, but is there a better way?)

  2. With this approach I get only input stream. What is a good way to pass file name?

  3. What are main disadvantages of this approach in general?