How to close InputStream which fed into Response(jax.rs)

Siva R
@GET
@Path("/{id}/content")
@Produces({ "application/octet-stream" })
public Response getDocumentContentById(@PathParam("id") String docId) {

    InputStream is = getDocumentStream(); // some method which gives stream
    ResponseBuilder responseBuilder = Response.ok(is);
    responseBuilder.header("Content-Disposition", "attachment; filename=" + fileName);
    return responseBuilder.build();

}

Here how can I close the InputStream is ? If something(jax.rs) closes automatically. Please give me some information. Thank you.

Donal Fellows

When you're wanting to stream a custom response, the most reliable way I've found is to return an object that contains the InputStream (or which can obtain the stream in some other way at some point), and to define a MessageBodyWriter provider that will do the actual streaming at the right time.

For example, this code is part of Apache Taverna, and it streams back the zipped contents of a directory. All that the main code needs to do to use it is to return a ZipStream as the response (which can be packaged in a Response or not) and to ensure that it is dealing with returning the application/zip content type. The final point to note is that since this is dealing with CXF, you need to manually register the provider; unlike with Glassfish, they are not automatically picked up. This is a good thing in sophisticated scenarios, but it does mean that you need to do the registration.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Iterator of InputStream: How to close the InputStreams?

From Dev

Why is it good to close() an inputstream?

From Dev

finally { if (inputStream != null) { inputStream.close();

From Dev

finally { if (inputStream != null) { inputStream.close();

From Dev

How to convert inputsource to inputstream which will feed to stringreader as the parameter?

From Dev

How to close merged branches which were not close during merge?

From Dev

How to close a activity which has been moveTaskToBack

From Dev

How can a command with arguments be fed to Bash?

From Dev

how to predict second class which is more close to test data

From Dev

How to safely close a THREAD which has a infinite loop in it

From Dev

How to detect which close button the user has pressed with multiple tabs

From Dev

How to close IDLE window which is opened by Tkinter.Tk()

From Dev

How to fix html tags(which is missing the <open> & <close> tags) with HTMLAgilityPack

From Dev

How can I run a command which will survive terminal close?

From Dev

how to close a webview which was opened by javascript in android programming

From Dev

Does InputStream.close() do anything?

From Dev

Java interrupt inputstream without close() method

From Dev

How to update an element's placeholder attribute based on a dynamically fed id?

From Dev

How to escape an apostrophe (') from an xml fed string value

From Dev

How to use props to hold user fed input (via textbox) in ReactJS?

From Dev

Not able to read InputStream from RS232 Bluetooth module

From Dev

Most Pythonic way to find which of a set of inputs, when fed to a function, produces the smallest output

From Dev

How to convert ArchiveEntry to InputStream?

From Dev

How to empty an inputstream?

From Dev

How to get copy of InputStream?

From Dev

How to copy InputStream to AsynchronousFileChannel

From Dev

How to convert InputStream to int

From Dev

How to mock InputStream and ByteString

From Dev

How is InputStream managed in memory?

Related Related

  1. 1

    Iterator of InputStream: How to close the InputStreams?

  2. 2

    Why is it good to close() an inputstream?

  3. 3

    finally { if (inputStream != null) { inputStream.close();

  4. 4

    finally { if (inputStream != null) { inputStream.close();

  5. 5

    How to convert inputsource to inputstream which will feed to stringreader as the parameter?

  6. 6

    How to close merged branches which were not close during merge?

  7. 7

    How to close a activity which has been moveTaskToBack

  8. 8

    How can a command with arguments be fed to Bash?

  9. 9

    how to predict second class which is more close to test data

  10. 10

    How to safely close a THREAD which has a infinite loop in it

  11. 11

    How to detect which close button the user has pressed with multiple tabs

  12. 12

    How to close IDLE window which is opened by Tkinter.Tk()

  13. 13

    How to fix html tags(which is missing the <open> & <close> tags) with HTMLAgilityPack

  14. 14

    How can I run a command which will survive terminal close?

  15. 15

    how to close a webview which was opened by javascript in android programming

  16. 16

    Does InputStream.close() do anything?

  17. 17

    Java interrupt inputstream without close() method

  18. 18

    How to update an element's placeholder attribute based on a dynamically fed id?

  19. 19

    How to escape an apostrophe (') from an xml fed string value

  20. 20

    How to use props to hold user fed input (via textbox) in ReactJS?

  21. 21

    Not able to read InputStream from RS232 Bluetooth module

  22. 22

    Most Pythonic way to find which of a set of inputs, when fed to a function, produces the smallest output

  23. 23

    How to convert ArchiveEntry to InputStream?

  24. 24

    How to empty an inputstream?

  25. 25

    How to get copy of InputStream?

  26. 26

    How to copy InputStream to AsynchronousFileChannel

  27. 27

    How to convert InputStream to int

  28. 28

    How to mock InputStream and ByteString

  29. 29

    How is InputStream managed in memory?

HotTag

Archive