response.setContentType("application/x-msdownload"); response.setHeader("Content-disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"), "iso8859-1")); bis = new java.io.BufferedInputStream(new java.io.FileInputStream(config.getServletContext().getRealPath(path + filename))); bos = new java.io.BufferedOutputStream(response.getOutputStream());
<%@ page
language="java"
contentType="text/html; charset=GBK"
pageEncoding="GBK"
%><TITLE>download.jsp</TITLE>
</HEAD>
<BODY>
<%
java.io.BufferedInputStream bis=null;
java.io.BufferedOutputStream bos=null;
try
{
String filename = new String(request.getParameter("filename").getBytes("ISO8859_1"));
String path = new String(request.getParameter("path").getBytes("ISO8859_1"));;
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"), "iso8859-1")); bis = new java.io.BufferedInputStream(new java.io.FileInputStream(config.getServletContext().getRealPath(path + filename)));
bos = new java.io.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(Exception e)
{
e.printStackTrace();
}
finally
{
if (bis != null)
{
bis.close();
}
if (bos != null)
{
bos.close();
}
}
%>
</BODY>
</HTML>test.jsp:
<BODY>
<A href="download.jsp?filename=新建.rar&path=/">测试</A>
</BODY>
在test.jsp点击"测试",即可下载"新建.rar".