如何在浏览器中直接打开pdf/doc/excel文件
我在文件下载的部分 需要在具体文件名上加超链接 到可以直接在浏览器打开
另外下载时候 能不能也默认直接打开在浏览器 而不是显示对话框
请做个类似功能的JE友给个帮助 谢谢先
在struts2 + spring + hibernate中
问题补充:
引用
response.setHeader("Content-Disposition", "inline"+ "; filename=/"" + encodedShowName+"/"");
这个我试过 会跳出对话框放你选择 是否打开或者保持
我想要的是直接打开在浏览器
谢谢
问题补充:
我是这么写的
- < action name = "downloadKnowledge" class = "knowledgeAction" method = "downloadKnowledge" >
- <!-- 指定被下载资源的位置 -->
- < param name = "inputPath" > /upload/ </ param >
- < result name = "success" type = "stream" >
- < param name = "contentType" > ${contentType} </ param >
- < param name = "inputName" > targetFile </ param >
- < param name = "contentDisposition" > ${filename} </ param >
- < param name = "bufferSize" > 4096 </ param >
- </ result >
- </ action >
java代码
- /**
- * 下载Action
- * @return
- */
- public String downloadKnowledge() throws Exception{
- checkFormat(download);
- return SUCCESS;
- }
- /**
- * 检查格式,文件后缀
- * @param fileName
- */
- private void checkFormat(String fileName){
- String[] splitString = fileName.split("//." );
- int length = splitString.length;
- String format = null ;
- if (length > 1 ){
- format = splitString[length - 1 ];
- }
- DataFormat df = Enum.valueOf(DataFormat.class , format);
- switch (df){
- case xls:
- // contentType设定
- contentType = "application/vnd.ms-excel;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- case xlsx:
- // contentType设定
- contentType = "application/vnd.ms-excel;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- case pdf:
- // contentType设定
- contentType = "application/pdf;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- case doc:
- // contentType设定
- contentType = "application/msword;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- case docx:
- // contentType设定
- contentType = "application/msword;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- case txt:
- // contentType设定
- contentType = "text/plain;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- default :
- // contentType设定
- contentType = "text/plain;charset=utf-8" ;
- // attachment表示网页会出现保存、打开对话框
- filename = "inline; filename=" + fileName;
- break ;
- }
- }