天天看点

jsp文件下载另存为中文文件名

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<title>??妗d?杞戒?瀛?涓?杞?椤甸??lt;/title>
</head>
<!--
  *??浠跺??:down_center.jsp
  *瀹??板???????″?ㄤ?淇?瀛?????浠跺??涓烘??涓?浼??堕?村?藉????
                涓?杞芥?惰???ㄦ?挎??负涓?????浠跺??
  *??????棰?:???煎??ゆ????????寰?寮???
  *缂????堕??2006-9-5
  *浠g??缂???:????绔?  *??缁?淇??规?堕??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 = "涓?????妗d?杞?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>
           

?

?