81,094
社区成员
发帖
与我相关
我的任务
分享
<%@ page contentType="text/html; charset=gb2312"%><%@page import="java.io.*"%><%
String filename = "";
if (request.getParameter("filename") != null) {
filename = request.getParameter("filename");
}
String name = filename.substring(filename.lastIndexOf("/") + 1);
//response.setContentType("application/msword");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+name);
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 );
} catch(final IllegalStateException e) {
System.out.println ( "出现IllegalStateException." + e );
}finally {
if (bis != null)
bis.close();
if (bos != null)
{
bos.flush();
bos.close();
bos=null;
}
}
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>