這是將PDF文件輸出到瀏覽器的一段代碼,它可以更快嗎?
這是在Java servlet中實現的。有更快的方式輸出PDF文件嗎?
private ByteArrayOutputStream getByteArrayOutputStreamFromFile(File file) throws Exception {
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new ByteArrayOutputStream();
byte[] byteContent = new byte[1024 * 1024];
int len = 0;
while ((len = bis.read(byteContent)) != -1) {
bos.write(byteContent, 0, len);
}
return bos;
} catch (Exception ex) {
throw ex;
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
爲什麼你寫的一個直接'ByteArrayOutputStream'而不是響應的OutputStream的? 1M的緩衝區可能不會提高網絡的性能。 – 2011-04-08 07:03:44