博客
关于我
一个简单的 java 文件流下载函数
阅读量:700 次
发布时间:2019-03-17

本文共 2725 字,大约阅读时间需要 9 分钟。

一个简单的 Java 流下载函数

import java.io.BufferedInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;

以下是一个简单的 Java 代码示例,用于通过网络 URL 下载文件到本地指定路径。

程序逻辑依赖于 Java 的 IO 流机制,通过 BufferedInputStream 从网络流读取数据并写入本地文件中。

public static void main(String[] args)  {    try    {      SaveURL("c:/temp/004.pdf", "http://download.localhost/%e5%b0%81%e9%9d%a2.pdf");    }    catch (IOException e)    {      e.printStackTrace();    }  }

功能说明:

public static void SaveURL(final String strFileName, final String strURL) throws MalformedURLException, IOException  {    BufferedInputStream bufferedInputStream = null;    FileOutputStream fileOutputStream = null;    int count = -1;    try    {      bufferedInputStream = new BufferedInputStream(new URL(strURL).openStream());      fileOutputStream = new FileOutputStream(strFileName);      // 读取流数据      final int buffer = 1024;      final byte data[] = new byte[buffer];      while ((count = bufferedInputStream.read(data, 0, buffer)) != -1)      {        fileOutputStream.write(data, 0, count);      }    }    catch (Exception e)    {    }    finally    {      if (bufferedInputStream != null)      {        bufferedInputStream.close();      }      if (fileOutputStream != null)      {        fileOutputStream.close();      }    }  }

另一种实现方法是通过 Apache HttpClient 的 HttpComponents 库:

public Boolean getFile(final String strRemoteFile, String strLocalFile)  {    Boolean result = null;    try    {      CloseableHttpClient httpclient = HttpClients.createDefault();      HttpGet httpget = new HttpGet(strRemoteFile);      HttpResponse response = httpclient.execute(httpget);      HttpEntity entity = response.getEntity();      InputStream inputStream = entity.getContent();      File file = new File(strLocalFile);      try      {        FileOutputStream fileOutputStream = new FileOutputStream(file);        int l = -1;        byte[] tmp = new byte[1024];        while ((l = inputStream.read(tmp)) != -1)        {          fileOutputStream.write(tmp, 0, l);        }        fileOutputStream.flush();        fileOutputStream.close();        result = true;      }      catch (IOException e)      {        e.printStackTrace();        result = false;      }      finally      {        inputStream.close();      }      httpclient.close();    }    catch (IOException e)    {      e.printStackTrace();      result = false;    }    return result;  }

上述代码实现了文件从网络 URL 下载到本地文件系统的功能。第一种实现直接使用 Java 的 IO 流机制,而第二种实现则基于 Apache HttpClient 的 HttpComponents 库,提供了更全面的 HTTP 请求和状态管理功能。

需要注意的是,在实际应用中,建议对异常进行合理捕获和处理,并根据具体需求选择合适的下载策略。

转载地址:http://zhvez.baihongyu.com/

你可能感兴趣的文章
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>