如何在jsp页面显示一个已有的word文档
我用的是ssh,要在jsp页面上显示一个word文档,具体怎么做,望高人指点一下方便的,麻烦上的代码,感谢不尽我的word文档的文件名是在数据库查出来的,从后来传过来的,...
我用的是ssh,要在jsp页面上显示一个word文档,具体怎么做,望高人指点一下
方便的,麻烦上的代码,感谢不尽
我的word文档的文件名是在数据库查出来的,从后来传过来的,想在前台直接显示的 展开
方便的,麻烦上的代码,感谢不尽
我的word文档的文件名是在数据库查出来的,从后来传过来的,想在前台直接显示的 展开
1个回答
展开全部
<%@page contentType="application/msword;charset=utf8"%>
<%@page import="java.io.*"%>
<%
String filename = "";
if (request.getParameter("docfilename") != null) {
filename = request.getParameter("docfilename");
}
File f = new File(filename);
if(!f.exists() || f.isDirectory()){
out.println("<mce:script language=\"JavaScript\"><!--
");
out.println("alert(\"文件未找到\");");
out.println("window.close();");
out.println("
// --></mce:script>");
out.close();
return;
}
response.setContentType("application/msword");
response.setHeader("Content-disposition","attachment; filename=mc.doc");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filename));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(FileNotFoundException fe){
System.out.println("文件没找到");
} catch(final IOException e) {
System.out.println ( "出现IOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
out.clear();
out = pageContext.pushBody();
}
return;
%>
<%@page import="java.io.*"%>
<%
String filename = "";
if (request.getParameter("docfilename") != null) {
filename = request.getParameter("docfilename");
}
File f = new File(filename);
if(!f.exists() || f.isDirectory()){
out.println("<mce:script language=\"JavaScript\"><!--
");
out.println("alert(\"文件未找到\");");
out.println("window.close();");
out.println("
// --></mce:script>");
out.close();
return;
}
response.setContentType("application/msword");
response.setHeader("Content-disposition","attachment; filename=mc.doc");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(filename));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
} catch(FileNotFoundException fe){
System.out.println("文件没找到");
} catch(final IOException e) {
System.out.println ( "出现IOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
out.clear();
out = pageContext.pushBody();
}
return;
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询