aomar
aomar

Reputation: 510

fast java Input\Output Stream

i have a servlet that copies a pdf file to the client using response output stream

private boolean copyStreamToStream(InputStream in, OutputStream target) {

    logger.info("start copy file to stream");
    try {

        byte[] buffer = new byte[1024 * 8];
        int len = in.read(buffer);
        while (len != -1) {
            target.write(buffer, 0, len);
            len = in.read(buffer);
        }
        in.close();
        target.flush();
        target.close();
        logger.info("end copy file to stream");

    } catch (Exception ex) {

        logger.error("Error: ", ex);
        return false;
    }
    return true;

}

the InputStream for the pdf file on disk and OutputStream for response.getOutputStream()

the problem is that the PDF file is a big file and it takes a very long time to load it on client is there is any way to speed it up???

Upvotes: 0

Views: 1517

Answers (1)

Bitmap
Bitmap

Reputation: 12538

Send the file for download instead of passing it back as direct response object.

// Set the headers.
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);

// Send the file for download.
OutputStream out = response.getOutputStream(  );

Edited.

Upvotes: 1

Related Questions