Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a file using Java Jersey

Tags:

java

jersey

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 ?

like image 599
digy Avatar asked Oct 10 '10 14:10

digy


2 Answers

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?

like image 164
nishantkyal Avatar answered Nov 11 '22 03:11

nishantkyal


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

like image 26
digy Avatar answered Nov 11 '22 04:11

digy