Uploading a file to Jersey without using multipart

I am running a web service where I convert a file from one file format to another. The transformation logic is already working, but now I want to query this logic through Jersey. Whenever uploading files over Jersey is addressed in tutorials / questions, people describe how to do it using data with multiple forms. However, I just want to send and return one file and skip the overhead of sending multiple parts. (The webservice is started by another machine that I control, so there is no HTML form.)

My question is, how could I achieve something like the following:

@POST
@Path("{sessionId"}
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@PathParam("sessionId") String sessionId, 
                    @WhatToPutHere InputStream uploadedFileStream) {
  return BusinessLogic.convert(uploadedFile); // returns StreamingOutput - works!
}

      

How do I get uploadedFileStream

(This should be an annotation, I think it certainly isn't @WhatToPutHere

). I figured out how to directly return the file via StreamingOutput

.

Thanks for any help!

0


source to share


1 answer


You don't need to enter anything in the second parameter of the function; just leave it un-annoted. The only thing you need to do is "name" the resource:

The resource must have a URI like: someSite / someRESTEndPoint / myResourceId, so the function must be:

@POST
@Path("{myResourceId}")
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@PathParam("myResourceId") String myResourceId, 
                                               InputStream uploadedFileStream) {
    return BusinessLogic.convert(uploadedFileStream); 
}

      



If you want to use some sort of SessionID, I'd rather use the header parameter ... something like:

@POST
@Path("{myResourceId}")
@Consumes("image/png")
@Produces("application/pdf")
public Response put(@HeaderParam("sessionId") String sessionId,
                    @PathParam("myResourceId") String myResourceId, 
                                               InputStream uploadedFileStream) {
    return BusinessLogic.convert(uploadedFileStream); 
}

      

+2


source







All Articles