打开java web Application中生成的Pdf文件 [英] Open a Pdf File generated in java web Application

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

问题描述

我正在我的网络应用程序中成功生成报告。我需要在客户端提供此文件并且我已完成此操作:

I am generating a report in my web application successfully. I need this file to be available at client side and I have done this:

 String serverHomeDir = System.getenv("CATALINA_HOME");
        String reportDestination = serverHomeDir + "/Reports/" + user + "_" + church + "_" + currdate + ".pdf";
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);

目录上可用的Pdf文件可以成功打开。但是,无法打开客户端上的One下载。它给出了这个错误:

The Pdf File Available on the Directory can be Opened Successfully. However the One downloading on Client Side cannot be Opened. It is giving this Error:

我在做什么错误或不做?

What am I doing Wrong or not doing?

推荐答案

您必须将文件作为字节数组附加到您的响应中:

You have to attach your file as a byte array to your response:

 String serverHomeDir = System.getenv("CATALINA_HOME");
 String reportDestination = serverHomeDir + "/Reports/" + user + "_" + church + "_" + currdate + ".pdf";

 FileInputStream fis = new FileInputStream(new File(reportDestination));

 // Fast way to copy a bytearray from InputStream to OutputStream
 org.apache.commons.io.IOUtils.copy(fis, response.getOutputStream());
 response.setContentType("application/pdf");
 response.setHeader("Content-Disposition", "attachment; filename=" + reportDestination);
 response.flushBuffer();

这篇关于打开java web Application中生成的Pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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