日期:2014-05-17 浏览次数:20766 次
<%@page pageEncoding="UTF-8"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%
String path=null;
try{
String name = request.getParameter("name");
String newname = request.getParameter("fileName");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/x-msdownload");
path = newname;
out.clear(); //添加
out = pageContext.pushBody();//添加
InputStream is = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] bs = new byte[1024];
int len = 0;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
os.flush();
}
os.close();
is.close();
}catch(Exception e){
e.printStackTrace();
}
%>
request.setCharacterEncoding("UTF-8"); //防止获取中文乱码
String path=null;
try{
String name = request.getParameter("name");
String newname = request.getParameter("fileName");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
response.setContentType("application/x-msdownload");
path = application.getRealPath("/")+newname;
out.clear(); //添加
out = pageContext.pushBody();//添加
InputStream is = new FileInputStream(path);
OutputStream os = response.getOutputStream();
byte[] bs = new byte[1024];
int len = 0;
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
os.flush();
}
os.close();
is.close();
}catch(Exception e){
e.printStackTrace();
}