Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

jsp - java open pdf file when it is being downloaded

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...