日期:2014-05-17  浏览次数:20716 次

在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,提高一下点击率,谢谢!