[zt]http://mornstar.blog.sohu.com/12594838.html
JSP下载指定服务器目录的文件,支持中文路径
1 <%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%> 2 <%! 3 public String convert(String s) { 4 try { 5 return new String(s.getBytes("ISO-8859-1"), "GB2312"); 6 } catch (Exception e) { 7 return null; 8 } 9 } 10 public String getFileName(String fileName) { 11 return fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); 12 } 13 %> 14 <% 15 response.reset(); 16 response.setContentType("application/x-download"); 17 String filepath = convert(request.getParameter("filepath")); 18 String filenamedisplay = getFileName(filepath); 19 filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8"); 20 response.addHeader("Content-Disposition", "attachment;filename=" + filenamedisplay); 21 OutputStream output = null; 22 FileInputStream fis = null; 23 try { 24 output = response.getOutputStream(); 25 fis = new FileInputStream(filepath); 26 byte[] b = new byte[1024]; 27 int i = 0; 28 while((i = fis.read(b)) > 0) { 29 output.write(b, 0, i); 30 } 31 output.flush(); 32 } catch(Exception e) { 33 System.out.println("Error!"); 34 e.printStackTrace(); 35 } finally { 36 if(fis != null) { 37 fis.close(); 38 fis = null; 39 } 40 if(output != null) { 41 output.close(); 42 output = null; 43 } 44 } 45 %>