您的位置:首页 > 编程语言 > Java开发

Java客户端下载服务器端的文件

2009-02-26 17:55 337 查看
一个比较简单的Java客户端下载服务器端的文件的方法

// 下载服务器端的文件
 private void save(String filePath, HttpServletResponse response)
   throws FileNotFoundException, IOException {

  response.setContentType("APPLICATION/OCTET-STREAM");
  response.setHeader("Content-Disposition", "attachment; filename=/""
    + "tempStore.xls" + "/"");
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    filePath));
  BufferedOutputStream bos = new BufferedOutputStream(response
    .getOutputStream());

  try {
   byte[] buff = new byte[2 * 1024];
   int bytesRead;

   while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
   }
  } finally {
   if (bis != null) {
    bis.close();
   }
   if (bos != null) {
    bos.close();
   }
  }
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  服务器 java byte null string