您的位置:首页 > Web前端 > JavaScript

下载服务器上的文件的jsp

2012-12-14 15:25 260 查看
<%@ page import="java.io.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String filepath = new String(request.getAttribute("redirectUrl")
.toString().getBytes("ISO-8859-1"), "UTF-8");
System.out.println("============================" + filepath);
if (filepath != null) {
OutputStream os = null;
FileInputStream fis = null;
try {
String file = filepath;
if (!(new File(file)).exists()) {
System.out.println("没有文件");
return;
}
String filename = file
.substring(file.lastIndexOf("\\") + 1);
System.out.println("文件名为:" + filename);
os = response.getOutputStream();
response.setHeader("content-disposition",
"attachment;filename="
+ new String(filename.getBytes("GBK"),
"ISO-8859-1"));
response.setContentType("application/octet-stream");//八进制流 与文件类型无关
byte temp[] = new byte[1024];
fis = new FileInputStream(file);
int n = 0;
while ((n = fis.read(temp)) != -1) {
os.write(temp, 0, n);
}
} catch (Exception e) {
out.print("出错了");
} finally {
if (os != null)
os.close();
if (fis != null)
fis.close();
}
out.clear();
out = pageContext.pushBody();
}
%>


用来下载服务器上的文件,支持中文文件名,路径

来源于百度来的某个文章

稍许更改



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: