实现下载提示框

  StringBuffer downLoadPath = new StringBuffer();

//获取项目中temp文件夹的路径
  String path = request.getSession().getServletContext().getRealPath("/") + "temp" + File.separator;

//获取需下载的文件名称
  String targetName =  AAA(l_bid_id,path);  
  System.out.println("说明书下载targetName" + targetName + "-----------------------------------------------------");
  java.io.BufferedInputStream bis = null;
  java.io.BufferedOutputStream bos = null;
  try {
  response.setContentType("text/html;charset=utf-8");
  request.setCharacterEncoding("UTF-8");
  String ctxPath = request.getSession().getServletContext().getRealPath("/") + "temp" + File.separator+targetName;
  downLoadPath.append(ctxPath);
  long fileLength = new File(downLoadPath.toString()).length();
  response.setContentType("application/octet-stream;");

//需下载的文件名中包含汉字
  response.setHeader("Content-Disposition", "attachment; filename=" + new String(targetName.getBytes("gbk"), "ISO8859-1"));
  response.setHeader("Content-Length", String.valueOf(fileLength));
  bis = new BufferedInputStream(new FileInputStream(downLoadPath.toString()));
  bos = new BufferedOutputStream(response.getOutputStream());
  byte[] buff = new byte[2048];
  int bytesRead;
  while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
   bos.write(buff, 0, bytesRead);
  }
  bos.flush();
  } catch (Exception ex) {
   ex.printStackTrace();
  } finally {
   try {
    if (bis != null)
     bis.close();
    if (bos != null){
     bos.flush();
     bos.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

猜你喜欢

转载自liuzhiqiang19890403.iteye.com/blog/2173776