servlet_response下载文件

  通过response响应头content-disposition,来下载文件,如果文件有中文,就用url编码

 

 1         String path = this.getServletContext().getRealPath("/download/妞儿.jpg");
 2         String fileName = path.substring(path.lastIndexOf("\\") + 1);
 3         //如果fileName是中文名字,那么我就要用URL编码 URLEncoder.encode(s, enc);
 4         response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
 5         InputStream in = new FileInputStream(path);
 6         OutputStream out =  response.getOutputStream();
 7         int len;
 8         byte[] buffer = new byte[1024];
 9         while((len = in.read(buffer)) > 0){
10             out.write(buffer,0,len);
11         }
12         in.close();

 

posted @ 2013-12-25 13:50  哎呦喂,我的小祖宗╰つ  阅读(217)  评论(0编辑  收藏  举报