当前位置: 代码迷 >> Java Web开发 >> java 在线打开word文件直接成为下载word文件
  详细解决方案

java 在线打开word文件直接成为下载word文件

热度:45   发布时间:2016-04-16 22:16:53.0
java 在线打开word文件直接变成下载word文件
如题,我想用户在点击链接的时候,直接打开doc文件而不是下载到本地,但是每次都是下载到本地,求大神指导!
后台代码
public String fileDownload(HttpServletRequest request, HttpServletResponse response) throws IOException{
String fileName = request.getParameter("fileName");   //显示的文件名
String fileName = request.getParameter("filePath");  //文件存放路径
response.setContentType("application/msword;charset=UTF-8");
response.setHeader("Content-Disposition", "inline;filename=" + new String(fileName.getBytes("GB2312"), 

"ISO-8859-1"));
BufferedOutputStream bos = null;
FileInputStream fis = null;
try{
File file = new File(fileRealPath);
if(!file.exists()){
throw new Exception("找不到指定文件:"+fileRealPath);
}
// 开始下载
fis = new FileInputStream(file);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] b = new byte[8192];
int data = 0;
while ((data = fis.read(b)) != -1){
bos.write(b, 0, data);
}
// 刷新流
bos.flush();
}catch (Exception e){
throw e;
}finally{
if (bos != null)
bos.close();
if (fis != null)
fis.close();
}
}
页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>立项受理</title>
<script type="text/javascript">
function downloadFile(filePath,fileName){
var time = new Date().getTime();
var id = "attachmentForm_"+time;
var form = "<form  target='uploadFrame' method='post' id='"+id+"' name='"+id+"' 

action='/fileUtils/fileDownload.do'>"
     + "<input type='hidden' name='filePath' value='"+filePath+"'/>"
             + "<input type='hidden' name='fileName' value='"+fileName+"'/></form>";  
   $("body").append(form);
   $("#"+id).submit();
   $("#"+id).remove();
}
</script>
</head>
<body>
<form action="" method="post">
<a href="javascript:;" onclick="downloadFile('/2012/11111.doc','11111.doc')">11111.doc</a>
</form>
<iframe style="display:none;" name="uploadFrame" src=""></iframe>
</body>
</html>
谷歌浏览器总是提示如下信息
resource interprected as document but transferred with MIME type application/msword
------解决方案--------------------
怀疑是浏览器设置的问题。其他浏览器也是这样么?
------解决方案--------------------
这是浏览器行为你不能控制的吧。。
有些浏览器还能设置直接在浏览器里打开,还是用某种工具下载呢,这么多浏览器你怎么管?
------解决方案--------------------
打开只能用activex控件,单靠页面是不行的。早期的ie还可以。
------解决方案--------------------
友情帮顶,学习了。
------解决方案--------------------
楼主你把服务器上的文件下到哪了?fileRealPath路径又是哪?下到本地不应该用JS打开文件吗
  相关解决方案