<%@ page contentType="text/html;charset=GBK" %>
<%
// 得到文件名字和路径
String filename = request.getParameter("filename");
String id=request.getParameter("id");
String filepath=pageContext.getServletContext().getRealPath("/NoticeFile");
// 设置响应头和下载保存的文件名
//String CONTENT_TYPE = "text/html; charset=GBK";
response.setContentType("application/x-msdownload");
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\"" +new String(filename.getBytes("GBK"),"ISO_8859_1") + "\"");
java.io.File file=new java.io.File(filepath +"\\"+ filename);
if(file==null || !file.exists()){
response.sendRedirect("downloaderror.jsp");
//response.setContentType("text/html; charset=GBK");
//String msg="文件不存在";
//RequestDispatcher rd=null;
//rd=request.getRequestDispatcher("/redirect.jsp");
//ServletRequest req=pageContext.getRequest();
//ServletResponse rps=pageContext.getResponse();
//req.setAttribute("msg",msg);
//rd.forward(req,rps);
return ;
}
// 打开指定文件的流信息
java.io.FileInputStream fileInputStream =new java.io.FileInputStream(file);
// 写出流信息
int i;
while ((i=fileInputStream.read()) != -1) {
response.getOutputStream().write(i);
}
fileInputStream.close();
response.getOutputStream().close();
out.clear();
out = pageContext.pushBody();
%>
posted on 2008-02-01 14:52
有猫相伴的日子 阅读(426)
评论(0) 编辑 收藏 所属分类:
web-demo