java直接打开pdf,doc,xls
jsp页面:
- <a href=\'#\' onclick=onLine(\''+urls[i]+'\') >在线打开</a>
 
html页面超链接单击打开online函数
- var onLine = function(filename){
 - var param="toolbar=yes,location=yes,menubar=yes,
 - scrollbars=yes,resizable=yes";
 - var path = "DocumentCabinetAction!openOnLine.action?
 - filepath="+encodeURI(filename);
 - window.open(path,'maxwindow',param);
 - };
 
online函数:打开一个新的窗口,path为后台函数,直接输出流到页面,在线打开。这个函数不能异步提交。
- //type 为response 返回类型,filename 为要打开的文件名称
 - if((filename.indexOf(".xls")>0) || (filename.indexOf(".xlsx")>0)){
 - type = "application/vnd.ms-excel";
 - }else if(filename.indexOf(".pdf")>0){
 - type = "application/pdf";
 - }else if((filename.indexOf(".doc")>0) || (filename.indexOf(".docx")>0)){
 - type = "application/msword";
 - }else if(filename.indexOf(".txt")>0){
 - type = "text/plain";
 - }else if(filename.indexOf(".ppt") >0){
 - type = "application/ppt";
 - }
 - //response设置返回内容type,outputStream输出
 - response.setContentType(type);
 - response.setHeader("Content-disposition","inline;filename="+URLEncoder.encode(filename, "utf-8"));
 - //ftp读取文件,并放入到输出流中
 - ftp.retrieveFile(new String(filename.getBytes("GBK"),"ISO-8859-1"), outputStream);
 - outputStream.flush();
 - outputStream.close();
 
注意点:一般下载、在线打开不要使用ajax提交,ajax提交会有问题,页面预览的时候第一次还是会弹出下载/打开对话框,第二次就正常显示了
在线打开的条件是:电脑中有相应的打开软件,不然在线显示不了,要想直接靠浏览器打开,要使用插件,比如dsoframer.ocx,这个插件只能打开word,excel ,代码如下:
- <div id="div1" style="width:100%;height:90%;">
 - <OBJECT id="FramerControl1" codeBase="dsoframer.ocx" style="margin:0px;top:0px;height:700px;width:100%" classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57">
 - <PARAM NAME="_ExtentX" VALUE="16960">
 - <PARAM NAME="_ExtentY" VALUE="13600">
 - <PARAM NAME="BorderColor" VALUE="-2147483632">
 - <PARAM NAME="BackColor" VALUE="-2147483643">
 - <PARAM NAME="ForeColor" VALUE="-2147483640">
 - <PARAM NAME="TitlebarColor" VALUE="-2147483635">
 - <PARAM NAME="TitlebarTextColor" VALUE="-2147483634">
 - <PARAM NAME="BorderStyle" VALUE="1">
 - <PARAM NAME="Titlebar" VALUE="0">
 - <PARAM NAME="Toolbars" VALUE="1">
 - <PARAM NAME="Menubar" VALUE="0">
 - </OBJECT>
 - </div>
 
- //在线打开word,excel path 为全路径,
 - getWord = function(path){
 - if(path == null)
 - {
 - Ext.Msg.alert('提示', '要打开的文件路径不存在!');
 - }else if((path.indexOf(".xls") >0) || (path.indexOf(".doc")>0) ){
 - document.getElementById('FramerControl1').Open(path);
 - document.getElementById('FramerControl1').ProtectDoc(1,2,"satellite");
 - //显示修订留痕
 - document.getElementById('FramerControl1').ShowRevisions(0);
 - }else{
 - Ext.Msg.alert('提示', '该格式的文件不能打开!');
 - }
 - };
 
                    
                
                
            
        
浙公网安备 33010602011771号