Return a file using Java Jersey

前端 未结 4 1060
孤城傲影
孤城傲影 2020-12-28 16:07

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 dat

相关标签:
4条回答
  • 2020-12-28 16:51

    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.

    0 讨论(0)
  • 2020-12-28 16:53

    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?

    0 讨论(0)
  • 2020-12-28 17:00

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

    0 讨论(0)
  • 2020-12-28 17:06

    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:

    • JDBC's getBinaryStream()

    • An example

    0 讨论(0)
提交回复
热议问题