0
votes

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
This is a question and answer site. If you want someone to work on a project or create some code for you, try a programmer-for-hire site like vworker.ElGavilan
I dont want any one to work.I tried many things like response.setHeader("Content-disposition","inline; filename="abc.pdf") but I am not able to achieve it so I am just asking for help if anyone has done it before.pratik
If you have tried many things, please share them here. It shows effort, and also reduces chances of getting solutions you've already tried. Edit your question with the code you have tried and why it didn't work (post error logs or "it did nothing", or an explanation). If you leave your question like this it might get closed.Nunser
Surprisingly, I don't see a similar question on Stack Overflow. Are you approaching the problem correctly? Byte serving PDFs have been around for some time. Google: java servlet "byte serve" pdf.jww
I am looking for functionality like mozilla.github.io/pdf.js/web/viewer.html. But I am looking for faster process as with pdf.js also end users have to wait for some time to get entire pdf loaded.pratik

1 Answers

0
votes

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.