用response输出Pdf文件流后,页面显示:无法加载pdf文件

这是feel的柑桔 2017-12-12 06:11:55
1、情况是这样子的:读取一个本地的pdf文件,然后再页面上显示这个pdf文件,就相当于pdf预览的功能。
遇到的问题是:通过在标题头的设置,从页面上下载的pdf无法让pdf阅读器读取显示,并且在页面查看就显示:"无法加载Pdf文件"。
下载后的图片:

页面展示时的图片:
2、代码是这样子的:
String filePath=SealAction.getPdfPath(); //pdf文件的绝对路径
File outFile = new File(filePath);
if (!outFile.exists()) {
this.setMsg("文件不存在");
return EMPTY;
} else {
response.setContentLength((int)outFile.length());
response.setCharacterEncoding("utf-8");
response.setHeader("Content-type", "application/pdf;charset=utf-8");
String filename = outFile.getName();
response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes(),"utf-8"));
FileInputStream fli = null;
OutputStream out = null;
BufferedReader in = null;
Writer oWriter = null;
//以流的方式响应会页面
try {
fli = new FileInputStream(outFile);
out = response.getOutputStream();

in = new BufferedReader(new InputStreamReader(fli,"UTF-8"));
oWriter = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));

char[] b = new char[1024];
int i = 0;
while((i = in.read(b))!= -1)
{
oWriter.write(b, 0, i);
}

in.close();
oWriter.flush();
oWriter.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
if(in != null){
in.close();
in = null;
}if(oWriter != null){
oWriter.close();
oWriter = null;
}
}catch(Exception e){
//不作任何处理
}
}
}


3、总的来说:问题是如何能在页面上显示出pdf呢? 哪里错了呢?
...全文
6094 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_wing啊 2021-02-11
  • 打赏
  • 举报
回复 1
引用 1 楼 我自横刀香甜笑 的回复:
public void findPdf() throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/pdf"); FileInputStream in = new FileInputStream(new File(“c:/测试.pdf”)); OutputStream out = response.getOutputStream(); byte[] b = new byte[512]; while ((in.read(b))!=-1) { out.write(b); } out.flush(); in.close(); out.close(); }
我有一个类似预览pdf的功能,response响应本地pdf文件流后,页面全是乱码,看你的加了个 response.setContentType("application/pdf"); 解决了问题。
Vue.pad 2019-12-18
  • 打赏
  • 举报
回复
楼主,我跟你差不多导出pdf文件之后用2345看图王打开出现格式出现错误 response.setHeader("content-Type", "application/pdf"); // 下载文件的默认名称 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String fileName = "Classmate-" + new Date().getTime() + ".pdf"; response.setHeader("Content-Disposition", "attachment;filename="+ fileName); //设置中文 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); //创建一个document对象 Document document = new Document(); PdfWriter.getInstance(document, response.getOutputStream()); document.open(); document.addTitle("同学信息表"); //表头 PdfPTable tableS = new PdfPTable(6); tableS.addCell(new Paragraph("姓名",FontChinese)); tableS.addCell(new Paragraph("性别",FontChinese)); tableS.addCell(new Paragraph("电话",FontChinese)); tableS.addCell(new Paragraph("家庭地址",FontChinese)); tableS.addCell(new Paragraph("出生年月",FontChinese)); tableS.addCell(new Paragraph("个人喜好",FontChinese)); document.add(tableS);
  • 打赏
  • 举报
回复
引用 2 楼 zzm9876789 的回复:
我刚写的,你看看能不能帮到你 http://blog.csdn.net/zzm9876789/article/details/78800476
好的,我都看了一下,你发的那些都是教如何创建pdf文件的,不太符合我题意,但是我还是感谢的你回答。
  • 打赏
  • 举报
回复
引用 1 楼 lye2000000_super 的回复:
public void findPdf() throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/pdf"); FileInputStream in = new FileInputStream(new File(“c:/测试.pdf”)); OutputStream out = response.getOutputStream(); byte[] b = new byte[512]; while ((in.read(b))!=-1) { out.write(b); } out.flush(); in.close(); out.close(); }
搞来搞去,发现你这样写的是最简单的,可行
zzm_fengye 2017-12-14
  • 打赏
  • 举报
回复
我刚写的,你看看能不能帮到你 http://blog.csdn.net/zzm9876789/article/details/78800476
  • 打赏
  • 举报
回复
public void findPdf() throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/pdf"); FileInputStream in = new FileInputStream(new File(“c:/测试.pdf”)); OutputStream out = response.getOutputStream(); byte[] b = new byte[512]; while ((in.read(b))!=-1) { out.write(b); } out.flush(); in.close(); out.close(); }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧