问题
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.
回答1:
I used PDF.js for time being. If quick response is very essential , single page layout is very use full. The very first page loaded whithin flick of a cecond. User can click on previous and next, and as only single page is loaded each time, it works very fast.
for single page layout http://www.worldwidewhat.net/2011/08/render-pdf-files-with-html5/
Pdf.js is javascript technology and runs on the browser. Hence it doesnot allow you to access any file outside the folder where your HTML(wich displays PDF) is residing(also found it as an absolute path issue). To solve this issue I used jsp servlet. My view.HTML page became jsp page.I made a servlet to access any pdf on file system. you can give servlet url in PDFJS.open("url") function or DEAFULT_URL parameter. You can pass file path as a paramter to the servlet with servletname?filePath=value. In the above given code in my question, instead I'll read data from file system and rest of the code is same.
来源:https://stackoverflow.com/questions/23221428/java-open-pdf-file-when-it-is-being-downloaded