加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA网页下载文档

(2014-12-25 14:56:02)
标签:

文档

网页

线程

路径

文件名

下载最好新写一个方法new一个新线程去调用,在请求中调用,因为如果请求去调用该方法,会增加耗时。

 

 private HttpServletResponse download(String path,
   HttpServletResponse response) {
  try {
   // path是指欲下载的文件的路径。
   File file = new File(path);
   // 取得文件名。
   String filename = file.getName();
   // 以流的形式下载文件。
   InputStream fis = new BufferedInputStream(new FileInputStream(path));
   byte[] buffer = new byte[fis.available()];
   fis.read(buffer);
   fis.close();
   // 清空response
   response.reset();
   // 设置response的Header
   response.addHeader("Content-Disposition", "attachment;filename="
     + new String(filename.getBytes()));
   response.addHeader("Content-Length", "" + file.length());
   OutputStream toClient = new BufferedOutputStream(response
     .getOutputStream());
   response.setContentType("application/x-download");
   toClient.write(buffer);
   toClient.flush();
   toClient.close();
   file.delete();
  } catch (IOException ex) {
   ex.printStackTrace();
  }
  return response;
 }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有