天天看點

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>
           

?

?