在浏览器中直接打开PDF

如何在浏览器中直接打开pdf/doc/excel文件

我在文件下载的部分 需要在具体文件名上加超链接 到可以直接在浏览器打开
另外下载时候 能不能也默认直接打开在浏览器 而不是显示对话框
 
请做个类似功能的JE友给个帮助 谢谢先

在struts2 + spring + hibernate中
问题补充:

引用

response.setHeader("Content-Disposition", "inline"+ "; filename=/"" + encodedShowName+"/""); 



这个我试过 会跳出对话框放你选择 是否打开或者保持

我想要的是直接打开在浏览器

谢谢
问题补充:
我是这么写的

Xml代码   收藏代码
  1. < action   name = "downloadKnowledge"   class = "knowledgeAction"   method = "downloadKnowledge" >   
  2.     <!-- 指定被下载资源的位置 -->    
  3.        < param   name = "inputPath" > /upload/ </ param >    
  4.          
  5.     < result   name = "success"   type = "stream" >   
  6.         < param   name = "contentType" > ${contentType} </ param >   
  7.         < param   name = "inputName" > targetFile </ param >   
  8.         < param   name = "contentDisposition" > ${filename} </ param >   
  9.         < param   name = "bufferSize" > 4096 </ param >   
  10.     </ result >   
  11. </ action >   



java代码

Java代码   收藏代码
  1. /**  
  2.  * 下载Action  
  3.  * @return  
  4.  */   
  5. public  String downloadKnowledge()  throws  Exception{  
  6.     checkFormat(download);  
  7.     return  SUCCESS;  
  8. }  
  9.   
  10. /**  
  11.  * 检查格式,文件后缀  
  12.  * @param fileName  
  13.  */   
  14. private   void  checkFormat(String fileName){  
  15.       
  16.     String[] splitString = fileName.split("//." );  
  17.     int  length = splitString.length;  
  18.     String format = null ;  
  19.     if  (length >  1 ){  
  20.         format = splitString[length - 1 ];  
  21.     }  
  22.       
  23.     DataFormat df = Enum.valueOf(DataFormat.class , format);  
  24.     switch  (df){  
  25.         case  xls:  
  26.             // contentType设定       
  27.             contentType = "application/vnd.ms-excel;charset=utf-8" ;      
  28.             // attachment表示网页会出现保存、打开对话框       
  29.             filename = "inline; filename="  + fileName;      
  30.             break ;                
  31.         case  xlsx:  
  32.             // contentType设定       
  33.             contentType = "application/vnd.ms-excel;charset=utf-8" ;      
  34.             // attachment表示网页会出现保存、打开对话框       
  35.             filename = "inline; filename="  + fileName;      
  36.             break ;  
  37.         case  pdf:    
  38.             // contentType设定       
  39.             contentType = "application/pdf;charset=utf-8" ;      
  40.             // attachment表示网页会出现保存、打开对话框       
  41.             filename = "inline; filename="  + fileName;   
  42.             break ;  
  43.         case  doc:  
  44.             // contentType设定       
  45.             contentType = "application/msword;charset=utf-8" ;      
  46.             // attachment表示网页会出现保存、打开对话框       
  47.             filename = "inline; filename="  + fileName;      
  48.             break ;                
  49.         case  docx:  
  50.             // contentType设定       
  51.             contentType = "application/msword;charset=utf-8" ;      
  52.             // attachment表示网页会出现保存、打开对话框       
  53.             filename = "inline; filename="  + fileName;      
  54.             break ;  
  55.         case  txt:  
  56.             // contentType设定       
  57.             contentType = "text/plain;charset=utf-8" ;      
  58.             // attachment表示网页会出现保存、打开对话框       
  59.             filename = "inline; filename="  + fileName;      
  60.             break ;  
  61.         default :  
  62.             // contentType设定       
  63.             contentType = "text/plain;charset=utf-8" ;      
  64.             // attachment表示网页会出现保存、打开对话框       
  65.             filename = "inline; filename="  + fileName;    
  66.             break ;  
  67.     }     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值