日期:2014-05-16  浏览次数:20382 次

jsp文件下载另存为中文文件名
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<title>文档下载保存中转页面</title>
</head>
<!--
  *文件名:down_center.jsp
  *实现功能:服务器上保存的文件名为按上传时间命名的
                下载时自动替换为中文文件名
  *遗留问题:无格式判断功能,有待开发
  *编写时间:2006-9-5
  *代码编写:郑兆童
  *最终修改时间:2006-9-5
  *最终修改内容:无
-->
<body>
<%
String root = getServletContext().getRealPath("/");      //得到网站绝对路径
//String filepath = "upload_file\\lwfile\\";                        //设置文件存放的相对路径(windows)
String filepath = "upload_file/lwfile/";                             //设置文件存放的相对路径(linux)
String fileName = request.getParameter("filename");    //得到文件名
String myName = "中文文档下载.doc";                              //文件改名

out.print(root + filepath + fileName);

// 设置响应头和下载保存的文件名
response.reset();
//response.setContentType("application/octet-stream");       //windows
//response.addHeader("Content-Disposition", "filename=\"" + myName + "\"");      //windows
response.setContentType("application/octet-stream; charset=GBK");     //linux

response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\"");      //linux


//新建文件输入输出流
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>
?

?