您的位置:首页 > 运维架构 > Tomcat

解决Tomcat下载中文名字文件乱码

2006-07-19 22:56 741 查看
 
String file="我.rar";

 file=URLDecoder.decode(file,"UTF-8");
 
 String fileName=file;

 file= application.getRealPath("/")+File.separator+file;
 
 fileName=java.net.URLEncoder.encode(fileName, "UTF-8").replace('+',' ');
 response.reset();
 response.setContentType("application/x-download");
 response.setHeader("Content-Disposition", "attachment;filename=" +fileName);
 
 BufferedOutputStream bos=null;
 BufferedInputStream bis=null;
 
  try{
        bis = new BufferedInputStream(new java.io.FileInputStream(file));
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[1024];
        int bytesRead;
        while(-1 != (bytesRead = bis.read(buff, 0, buff.length))){
            bos.write(buff,0,bytesRead);
        }
    }catch(Exception ex){
   
    }finally{

   bis.close();
      bos.close();
  }
  response.flushBuffer();
     bis=null;
  bos=null;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息