Code
jsp文件代码:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<%@ page import="com.jfcgf.hrms.util.Tools" %>
<title>文档下载保存中转页面</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
String path = Tools.convertNull(request.getParameter("path")); //文件在web应用下的相对路径
String fileName = Tools.convertNull(request.getParameter("filename"));//文件名
String root = getServletContext().getRealPath("/"); //得到网站绝对路径
//String filepath = "upload/tra_dutam/";
String filepath = Tools.replaceAll(path,"/","\\\\");
filepath = filepath+"\\\\";
String myName = fileName;
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("application/x-msdownload"); //windows
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");
//新建文件输入输出流
OutputStream output = null;
FileInputStream fis = null;
try{
//新建File对象
File f = new File(root + filepath + fileName);
//新建文件输入输出流对象
output = response.getOutputStream();
fis = new FileInputStream(f);
//设置每次写入缓存大小
byte[] b = new byte[(int)f.length()];
//out.print(f.length());
//把输出流写入客户端
int i = 0;
while((i = fis.read(b)) > 0){
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(fis != null){
fis.close();
fis = null;
}
if(output != null){
output.close();
output = null;
}
}
%>
</body>
</html>
文章出处:http://www.diybl.com/course/3_program/java/javajs/2008315/104694.html
jsp文件代码:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<%@ page import="com.jfcgf.hrms.util.Tools" %>
<title>文档下载保存中转页面</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
String path = Tools.convertNull(request.getParameter("path")); //文件在web应用下的相对路径
String fileName = Tools.convertNull(request.getParameter("filename"));//文件名
String root = getServletContext().getRealPath("/"); //得到网站绝对路径
//String filepath = "upload/tra_dutam/";
String filepath = Tools.replaceAll(path,"/","\\\\");
filepath = filepath+"\\\\";
String myName = fileName;
// 设置响应头和下载保存的文件名
response.reset();
response.setContentType("application/x-msdownload"); //windows
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");
//新建文件输入输出流
OutputStream output = null;
FileInputStream fis = null;
try{
//新建File对象
File f = new File(root + filepath + fileName);
//新建文件输入输出流对象
output = response.getOutputStream();
fis = new FileInputStream(f);
//设置每次写入缓存大小
byte[] b = new byte[(int)f.length()];
//out.print(f.length());
//把输出流写入客户端
int i = 0;
while((i = fis.read(b)) > 0){
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(fis != null){
fis.close();
fis = null;
}
if(output != null){
output.close();
output = null;
}
}
%>
</body>
</html>
文章出处:http://www.diybl.com/course/3_program/java/javajs/2008315/104694.html