用JSP下载word文件(不会直接用IE打开)
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%
string filename = "";
if (request.getparameter("file") != null) {
filename = request.getparameter("file");
}
response.setcontenttype("application/msword");
response.setheader("content-disposition","attachment; filename="+filename);
bufferedinputstream bis = null;
bufferedoutputstream bos = null;
try {
bis = new bufferedinputstream(new fileinputstream(getservletcontext().getrealpath("" + 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(final ioexception e) {
system.out.println ( "出现ioexception." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return;
%>
下一篇: 数据库阿里连接池 druid配置详解