下载时打开java pdf文件 [英] java open pdf file when it is being downloaded

查看:353
本文介绍了下载时打开java pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多大pdf文件的存储库.我允许用户使用servlet下载pdf文件.我想要一种功能,只要单击查看文件",用户就应该能够查看已下载的内容(逐页).

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();

如您所见,我试图将"Content-disposition"设置为"inline",并且还将out.flush()放入循环而不是外部循环.

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.

推荐答案

我暂时使用了 PDF.js .如果快速响应非常重要,则单页布局非常有用.第一页载入了ceced的声音.用户可以单击上一个和下一个,并且每次仅加载单个页面时,它的运行速度非常快.

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.

用于单页布局 http://www.worldwidewhat .net/2011/08/render-pdf-files-with-html5/

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

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.

这篇关于下载时打开java pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆