在adobe reader中客户端的java open pdf文件

时间:2014-04-16 14:01:51

标签: java file pdf client adobe

在Java Web Application中,我必须在客户端机器中打开PDF文件,该文件也可以在共享中使用而无需复制到客户端。它应该在客户端的adobe reader中打开。

1 个答案:

答案 0 :(得分:0)

如果文件位于客户端,如果他双击该文件,它将在Adobe阅读器中打开: - )

但是服务器无法打开位于客户端的文件。如果Web应用程序需要打开,则该文件需要存在于服务器可读位置。您可以编写一个servlet来打开PDF文件。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream servletOutputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {

    // Public user guide
    String fileName = "MyFile.pdf";


    bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));

    byte[] data = new byte[bufferedInputStream.available()];
    bufferedInputStream.read(data);

    response.setContentType("application/pdf");
    servletOutputStream = response.getOutputStream();
    bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
    bufferedOutputStream.write(data);

} catch (Exception e) {
    LOG.error(e);
} finally {

    if (bufferedInputStream != null) {
        bufferedInputStream.close();
    }

    if (bufferedOutputStream != null) {
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }

    if (servletOutputStream != null) {
        servletOutputStream.flush();
        servletOutputStream.close();
    }
}