您的位置:首页 > 编程语言 > Java开发

JavaWeb项目下中文名文件的下载

2011-08-15 21:24 120 查看
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

  String finame = "劳斯莱斯.jpg";

  finame = URLEncoder.encode(finame, "utf-8");

  response.setHeader("content-disposition", "attachment;filename="+ finame);

  // getServletContext().getRealPath(相对于web应用的路径);此方法获得相对于web的绝对路径

  /*

   * String path = getServletContext().getRealPath("/a.jpg");

   *

   * InputStream in = new FileInputStream(path);

   */

  // InputStream in =super.getServletContext().getResourceAsStream(相对于web应用的路径);此方法返回读取相对于该路径文件的IO流

  InputStream in = super.getServletContext()

    .getResourceAsStream("/a.jpg");

  OutputStream out = response.getOutputStream();

  int len;

  byte[] buf = new byte[1024];

  while ((len = in.read(buf)) > 0) {

   out.write(buf, 0, len);

  }

  out.close();

  in.close();

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