Possible Duplicate:
how to display a pdf file in jsp using servlet
我从数据库中检索一个 pdf 文件并将其放入这样的文件中
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..");
}
现在这是在服务器端。在我的客户端,每当用户单击链接时说
a href=pdf(pdf 是我的 servlet 名称) 在我的 jsp 页面中,我应该在客户端的浏览器上显示从数据库检索的文件。
我应该做什么?
最佳答案
将响应的内容类型设置为 pdf
response.setContentType("application/pdf");
然后将pdf内容写入响应对象
关于java - 在浏览器中显示pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10345861/