日期:2014-05-16 浏览次数:20363 次
down.jsp代码如下:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page language="java" import="java.io.*,java.net.*" pageEncoding="gbk"%>
<html>
<head>
<title>test</title>
</head>
<body>
<%
response.setContentType("text/html");
javax.servlet.ServletOutputStream ou = response.getOutputStream();
String filepath=request.getRealPath("/page")+"/enter/sample/";
String filename=new String(URLDecoder.decode(request.getParameter("filename"),"UTF-8")).toString();
System.out.println("DownloadFile filepath:" + filepath);
System.out.println("DownloadFile filename:" + filename);
java.io.File file = new java.io.File(filepath + filename);
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + " 文件不存在!");
return;
}
// 读取文件流
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file);
// 下载文件
// 设置响应头和下载保存的文件名
if (filename != null && filename.length() > 0) {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");
if (fileInputStream != null) {
int filelen = fileInputStream.available();
//文件太大时内存不能一次读出,要循环
byte a[] = new byte[filelen];
fileInputStream.read(a);
ou.write(a);
}
fileInputStream.close();
ou.close();
out.clear();
out = pageContext.pushBody();
}
%>
</body>
</html>
?
test.html
<a href='down.jsp?filename=%E5%8F%97%E7%90%86%E5%91%8A%E7%9F%A5%E5%8D%95.doc'>受理告知单</a>
?
注意:中文文件名需要用urlencode进行转码