java下载时打开pdf文件

时间:2021-05-14 10:08:04

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).

我有一个包含许多重要pdf文件的存储库。我允许用户使用servlet下载pdf文件。我想要功能,只要我点击“查看文件”,用户就应该能够看到已经下载的内容(逐页)。

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.

正如你所看到的,我试图将“Content-disposition”设置为“inline”,并且我在循环中放出了.flush()而不是在循环中。

1 个解决方案

#1


0  

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.

我暂时使用PDF.js。如果快速响应非常重要,则单页面布局非常实用。只需轻轻一点就可以加载第一页。用户可以单击上一个和下一个,并且每次只加载一个页面,它的工作速度非常快。

for single page layout http://www.worldwidewhat.net/2011/08/render-pdf-files-with-html5/

单页面布局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.

Pdf.js是javascript技术并在浏览器上运行。因此,它不允许您访问HTML(显示PDF)所在的文件夹之外的任何文件(也将其视为绝对路径问题)。为了解决这个问题,我使用了jsp servlet。我的view.HTML页面变成了jsp页面。我创建了一个servlet来访问文件系统上的任何pdf。您可以在PDFJS.open(“url”)函数或DEAFULT_URL参数中提供servlet URL。您可以使用servletname?filePath = value将文件路径作为参数传递给servlet。在我问题的上面给出的代码中,我将从文件系统中读取数据,其余代码是相同的。

#1


0  

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.

我暂时使用PDF.js。如果快速响应非常重要,则单页面布局非常实用。只需轻轻一点就可以加载第一页。用户可以单击上一个和下一个,并且每次只加载一个页面,它的工作速度非常快。

for single page layout http://www.worldwidewhat.net/2011/08/render-pdf-files-with-html5/

单页面布局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.

Pdf.js是javascript技术并在浏览器上运行。因此,它不允许您访问HTML(显示PDF)所在的文件夹之外的任何文件(也将其视为绝对路径问题)。为了解决这个问题,我使用了jsp servlet。我的view.HTML页面变成了jsp页面。我创建了一个servlet来访问文件系统上的任何pdf。您可以在PDFJS.open(“url”)函数或DEAFULT_URL参数中提供servlet URL。您可以使用servletname?filePath = value将文件路径作为参数传递给servlet。在我问题的上面给出的代码中,我将从文件系统中读取数据,其余代码是相同的。