I have a repository with many big pdf files. I am allowing users to download pdf files with a servlet. I want functionality where as soon as I click on "View File", users should be able to see contents that are already downloaded (page-by-page).
String fileType = "application/pdf";
response.setContentType(fileType);
// Make sure to show the download dialog
response.setHeader("Content-disposition","inline; filename=JavaIn21Days.pdf");
URL url = new URL("http://portal.aauj.edu/e_books/teach_your_self_java_in_21_days.pdf");
BufferedInputStream bufferedInputStream = new BufferedInputStream(url.openStream());
byte[] buffer = new byte[4096];
int length;
while ((length = bufferedInputStream.read(buffer)) > 0){
out.write(buffer, 0, length);
out.flush();
}
in.close();
out.close();
As you can see I tried to make "Content-disposition" as "inline" and also I put out.flush()
in the loop instead of out side loop.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…