网站开发与移动开发

博客园 首页 新随笔 管理

/**
  * 公共 download
  * @param response
  * @param path
  * @param name
  * @throws Exception
  */
 private void download(HttpServletResponse response, String path, String name)
   throws Exception {
  response.setContentType("application/xls;charset=UTF-8");
  response.setHeader("Content-disposition", "attachment;filename="
    + toUTF8(name));

  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  try {
   FileInputStream fis = new FileInputStream(path);
   bis = new BufferedInputStream(fis);
   bos = new BufferedOutputStream(response.getOutputStream());

   byte buff[] = new byte[2048 * 10];
   int bytesRead;
   while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
   }
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
   if (bis != null) {
    bis.close();
    bis = null;
   }
   if (bos != null) {
    bos.close();
    bos = null;
   }
   new File(path).delete();
  }
 }

posted on 2012-04-05 14:28  txf2004  阅读(126)  评论(0编辑  收藏  举报