java直接打开pdf,doc,xls

jsp页面: 

Js代码 复制代码 收藏代码
  1. <a href=\'#\' onclick=onLine(\''+urls[i]+'\') >在线打开</a>  

   html页面超链接单击打开online函数

  

Js代码 复制代码 收藏代码
  1. var onLine = function(filename){   
  2.     var param="toolbar=yes,location=yes,menubar=yes,   
  3.     scrollbars=yes,resizable=yes";   
  4.     var path = "DocumentCabinetAction!openOnLine.action?        
  5.     filepath="+encodeURI(filename);   
  6.     window.open(path,'maxwindow',param);    
  7. };  

   online函数:打开一个新的窗口,path为后台函数,直接输出流到页面,在线打开。这个函数不能异步提交。 

  

Java代码 复制代码 收藏代码
  1. //type 为response 返回类型,filename 为要打开的文件名称   
  2.  if((filename.indexOf(".xls")>0) || (filename.indexOf(".xlsx")>0)){   
  3.      type = "application/vnd.ms-excel";     
  4.  }else if(filename.indexOf(".pdf")>0){   
  5.      type = "application/pdf";             
  6.  }else if((filename.indexOf(".doc")>0)  || (filename.indexOf(".docx")>0)){   
  7.      type = "application/msword";          
  8.  }else if(filename.indexOf(".txt")>0){    
  9.      type = "text/plain";                   
  10.  }else if(filename.indexOf(".ppt") >0){   
  11.      type = "application/ppt";              
  12.  }   
  13.   
  14.  //response设置返回内容type,outputStream输出   
  15.  response.setContentType(type);    
  16.  response.setHeader("Content-disposition","inline;filename="+URLEncoder.encode(filename, "utf-8"));   
  17.  //ftp读取文件,并放入到输出流中   
  18.  ftp.retrieveFile(new String(filename.getBytes("GBK"),"ISO-8859-1"), outputStream);   
  19.  outputStream.flush();   
  20.  outputStream.close();  

 

注意点:一般下载、在线打开不要使用ajax提交,ajax提交会有问题,页面预览的时候第一次还是会弹出下载/打开对话框,第二次就正常显示了

在线打开的条件是:电脑中有相应的打开软件,不然在线显示不了,要想直接靠浏览器打开,要使用插件,比如dsoframer.ocx,这个插件只能打开word,excel ,代码如下:

Js代码 复制代码 收藏代码
  1. <div id="div1" style="width:100%;height:90%;">   
  2.         <OBJECT id="FramerControl1" codeBase="dsoframer.ocx" style="margin:0px;top:0px;height:700px;width:100%" classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57">   
  3.              <PARAM NAME="_ExtentX" VALUE="16960">   
  4.              <PARAM NAME="_ExtentY" VALUE="13600">   
  5.              <PARAM NAME="BorderColor" VALUE="-2147483632">   
  6.              <PARAM NAME="BackColor" VALUE="-2147483643">   
  7.              <PARAM NAME="ForeColor" VALUE="-2147483640">   
  8.              <PARAM NAME="TitlebarColor" VALUE="-2147483635">   
  9.              <PARAM NAME="TitlebarTextColor" VALUE="-2147483634">   
  10.              <PARAM NAME="BorderStyle" VALUE="1">   
  11.              <PARAM NAME="Titlebar" VALUE="0">   
  12.              <PARAM NAME="Toolbars" VALUE="1">   
  13.              <PARAM NAME="Menubar" VALUE="0">   
  14.          </OBJECT>   
  15.     </div>   
Js代码 复制代码 收藏代码
  1. //在线打开word,excel path 为全路径,   
  2. getWord = function(path){   
  3.     if(path == null)   
  4.     {   
  5.         Ext.Msg.alert('提示''要打开的文件路径不存在!');   
  6.     }else if((path.indexOf(".xls") >0) || (path.indexOf(".doc")>0) ){   
  7.         document.getElementById('FramerControl1').Open(path);   
  8.         document.getElementById('FramerControl1').ProtectDoc(1,2,"satellite");   
  9.         //显示修订留痕   
  10.         document.getElementById('FramerControl1').ShowRevisions(0);   
  11.     }else{   
  12.         Ext.Msg.alert('提示''该格式的文件不能打开!');   
  13.     }   
  14. };  

posted on 2013-08-05 17:19  世界之大追梦者  阅读(1193)  评论(0编辑  收藏  举报

导航