0

我有一个使用响应输出流将 pdf 文件复制到客户端的 servlet

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;

}

磁盘上 pdf 文件的 InputStream 和 response.getOutputStream() 的 OutputStream

问题是PDF文件是一个大文件,在客户端加载它需要很长时间。有什么办法可以加快速度???

4

1 回答 1

1

发送文件以供下载,而不是将其作为直接响应对象传回。

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

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

已编辑。

于 2012-04-30T11:27:33.453 回答