Zuzu JH
Zuzu JH

Reputation: 627

JAX-RS POST file input stream is empty

I am trying to create a POST call that uploads file, here is my code:

@POST
@Path("/uploadfile") 
@Produces({"application/json","application/xml"}) //the default result is json
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Authenticated
public Response saveFile( @FormDataParam("file") InputStream inputStream,
                              @FormDataParam("file") FormDataContentDisposition cdh) throws CompanyNotFoundException, UserUnauthorizedException, IllegalAccessException, WasteInvalidException, ItemNotFoundException, IOException{
    System.out.println(inputStream.available());

    return Response.ok("success").build();
}

but for some reason the received inputStream has 0 bytes always (use inputStream.available() check). I did the testing using Postman. enter image description here

What am I missing please?

Upvotes: 0

Views: 867

Answers (1)

davidmerrick
davidmerrick

Reputation: 1037

That all looks right. Are you sure your assumption is correct about the stream being empty? Looking at the Javadoc:

The available method for class InputStream always returns 0.

You might try reading from it and seeing if there's data present.

byte[] bytes = IOUtils.toByteArray(inputStream);

Upvotes: 1

Related Questions