文件下载

来源:互联网 发布:淘宝商家发快递的成本 编辑:程序博客网 时间:2024/04/30 13:01

  ServletOutputStream out = appContext.getResponse()
    .getOutputStream();
  appContext.getResponse().setContentType("application/excel");
  
  appContext.getResponse().setHeader("Content-disposition",
    "attachment; " + "filename=temp.xls");

  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  try {
   FileInputStream fin = new FileInputStream(
     fileName);
   bis = new BufferedInputStream(fin);
   bos = new BufferedOutputStream(out);
   byte buff[] = new byte[2048 * 8];
   int bytesRead;
   while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
    bos.write(buff, 0, bytesRead);
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (bis != null)
    bis.close();
   if (bos != null)
    bos.close();
  }  
    } 

原创粉丝点击