23

I am using the Java Jersey to implement a REST service. One thing my service should provide is a file download option. These files are quite big and are constructed from data from db.

Currently I am fetching all data from the db and saving it to a file and returning a

Response.ok().entity(new FileInputStream(file)).build();

Is there a way how I can start serving the file without fully downloading the data from db, but as the data comes from db append it to the Output stream ?

digy
  • 2,663
  • 3
  • 19
  • 14
  • See http://stackoverflow.com/questions/3496209/input-and-output-binary-streams-using-jersey, this an answer that is also useful for this. – centic Nov 13 '14 at 13:52

4 Answers4

9

How about

File fileToSend = getFile();
return Response.ok(fileToSend, "application/zip").build();

The media type can be set to match the file being sent.

This looks pretty straight forward but more importantly, do java experts reading this see a performance problem with the solution?

nishantkyal
  • 814
  • 1
  • 10
  • 8
9

Solved the problem by using the answer from Input and Output binary streams using JERSEY?

Community
  • 1
  • 1
digy
  • 2,663
  • 3
  • 19
  • 14
0

So you want to get a stream from your database ?

I'm not sure it's even possible.

But you don't have to write a temp file to send your data. Instead of writing into the file, you could use a ByteArrayInputStream and put your data there with a byte array.

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • Yes, I want something like new InputStream(new MyDBWrapper()) where MyDBWrapper would perform the data fetching and formating. I am not sure I can use ByteInputStream as the data is quite big - can get more then 1 GB. – digy Oct 10 '10 at 14:29
0

depends on the type of underlying database connection/driver, if you have access to the JDBC layer (e.g. using Hibernate) it should be possible to stream data using the JDBC Streaming API, then take the Streams from the ResultSet and pass them into Jersey's Response Builder. I have not done this myself though..

check here:

fasseg
  • 17,504
  • 8
  • 62
  • 73