下载文件

// 得到文件名字和路径

 String fileName = (String)request.getAttribute("fileName");
 String filepath = (String)request.getAttribute("urlpath")+"\\"+ fileName;


 // 设置响应头和下载保存的文件名
 response.setContentType("APPLICATION/OCTET-STREAM");
 //response.setContentType("text/html; charset=utf-8");
 response.setHeader("Content-Disposition","attachment; filename=\""+fileName+ "\"");
 //response.setHeader("Content-Disposition","attachment; filename=读文件.txt");
    int n = 0;
 java.io.FileInputStream  fis = null;
    java.io.PrintWriter oo=null;
 try
 {
  fis  = new java.io.FileInputStream(filepath);
  oo =new java.io.PrintWriter(response.getWriter());
  while ((n=fis.read())!=-1)
  {
   oo.write(n);
  }
 }
 catch (Exception e)
 {
    e.printStackTrace() ;
 }
   finally
   {
  if (!(fis==null))
 {
    oo.close();
    oo = null;
    fis.close();
    fis = null;
  }
  System.gc();
   }

你可能感兴趣的:(java,html,OO)