在浏览器中显示pdf文件? [英] Display a pdf file in browser?

查看:98
本文介绍了在浏览器中显示pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用servlet在jsp中显示pdf文件

Possible Duplicate:
how to display a pdf file in jsp using servlet

我从数据库中检索一个pdf文件,并将其放入这样的文件中

I retrieve a pdf file from my database and put it in a file like this

String str="select * from files where name='Security.pdf';";
Statement stmt2= conn.createStatement  
                   (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt2.executeQuery(str);
while(rs.next())
{
 InputStream input = rs.getBinaryStream("content");
 //to create file
   File f=new File("c:/pdfile.pdf");
   OutputStream out=new FileOutputStream(f);
   byte buf[]=new byte[1024];
   int len;
   while((len=input.read(buf))>0)
   out.write(buf,0,len);
   out.close();
   input.close();
    System.out.println("\nFile is created..");
}

现在这是在服务器端.在我的客户端,每当用户单击链接时,说 在我的jsp页面中 a href = pdf(pdf是我的servlet名称),我应该在客户端的浏览器上显示从数据库检索到的文件.
我该怎么办?

Now this is at server end. In my client side, Whenever user clicks a link say a href=pdf(pdf is my servlet name) in my jsp page, I should display the file retrieved from database on client's Browser.
What should I do?

推荐答案

将响应的内容类型设置为pdf

Set your content-type of the response to pdf

response.setContentType("application/pdf");

然后将pdf内容写入响应对象

Then write the pdf contents into the response object

这篇关于在浏览器中显示pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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