首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

在JSP中怎么实现“网页直接浏览PDF和WORD文件”

2012-04-04 
在JSP中如何实现“网页直接浏览PDF和WORD文件”在JSP中如何实现“网页直接浏览PDF和WORD文件”[解决办法]这是

在JSP中如何实现“网页直接浏览PDF和WORD文件”
在JSP中如何实现“网页直接浏览PDF和WORD文件”

[解决办法]
这是我的代码,供参考
public ActionForward read(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub


String filePath=(String)request.getParameter("url");

File f = new File(filePath);

String filename = f.getName(); 

if(!f.exists()){
response.sendError(404,"没有您要打开的文件");
return null;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;

response.reset();//非常重要
//在线打开方式
URL u = new URL("file://"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "online;filename=" + new String(filename.getBytes(), "ISO-8859-1"));//保证显示中文;
//文件名应该编码成UTF-8
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
br.close();
out.close();
return null;

如果能帮上你,就帮我点击www.55find.cn,提高一下点击率,谢谢!

热点排行