Silk0vsky
Silk0vsky

Reputation: 1042

How to upload octet-stream file to spring-mvc application?

Usually for file upload I've used multipart/form-data & it worked fine. But now there is a requirement for my server to be able to accept files application/octet-stream.

On the server side I have:

@ResponseBody
@RequestMapping(path = "/mock",
        consumes = { MediaType.APPLICATION_OCTET_STREAM_VALUE },
        method = RequestMethod.POST)
public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) {
    return ResponseEntity.accepted().build();
}

And I'm trying to test it with curl:

curl -v -H "Content-Type:application/octet-stream" \
  --data-binary @/home/user/Desktop/test.txt http://localhost:9090/mock

As a result a have:

org.springframework.web.multipart.MultipartException: Current request is not a multipart request

I've noticed that "file" part is not specified in my curl command but expected on the server side. It's not clear where to move next & what is broken test command or server or both.

Upvotes: 4

Views: 6948

Answers (2)

Sven Döring
Sven Döring

Reputation: 4378

As you don't have a multipart form-data message you can't use a MultipartFile.

You now have two possibilties.

Either send a multipart message with curl:
(Have a look at the form data called file.)

curl -F "file=@/home/user/Desktop/test.txt" http://localhost:9090/mock

Or change the controller:

@ResponseBody
@RequestMapping(path = "/mock",
        consumes = { MediaType.APPLICATION_OCTET_STREAM_VALUE },
        method = RequestMethod.POST)
public ResponseEntity handleFileUpload(final HttpServletRequest request) {
    // request.getInputStream() will contain the content of the posted file
    return ResponseEntity.accepted().build();
}

Upvotes: 5

hanshenrik
hanshenrik

Reputation: 21513

to upload files in the multipart/form-data-format, use -F or --form

curl -F "file=@/home/user/Desktop/test.txt" http://localhost:9090/mock

but that will probably send the file as Content-Type: text/plain , not Content-Type: application/octet-stream, as we can see in a netcat server:

$ nc -l 9090
POST /mock HTTP/1.1
Host: 127.0.0.1:9090
User-Agent: curl/7.66.0
Accept: */*
Content-Length: 190
Content-Type: multipart/form-data; boundary=------------------------465160a3b9f7de24

--------------------------465160a3b9f7de24
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

lol

--------------------------465160a3b9f7de24--

in order to force curl to send it as application/octet-stream , do -F "file=@/home/user/Desktop/test.txt;type=application/octet-stream"

which gives us:

$ nc -l 9090
POST /mock HTTP/1.1
Host: 127.0.0.1:9090
User-Agent: curl/7.66.0
Accept: */*
Content-Length: 204
Content-Type: multipart/form-data; boundary=------------------------444e61e5527966d1

--------------------------444e61e5527966d1
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream

lol

--------------------------444e61e5527966d1--

Upvotes: 1

Related Questions