博客
关于我
一个简单的 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/

你可能感兴趣的文章
OSPF技术连载10:OSPF 缺省路由
查看>>
OSPF技术连载11:OSPF 8种 LSA 类型,6000字总结!
查看>>
OSPF技术连载13:OSPF Hello 间隔和 Dead 间隔
查看>>
OSPF技术连载14:OSPF路由器唯一标识符——Router ID
查看>>
OSPF技术连载15:OSPF 数据包的类型、格式和邻居发现的过程
查看>>
OSPF技术连载16:DR和BDR选举机制,一篇文章搞定!
查看>>
OSPF技术连载17:优化OSPF网络性能利器——被动接口!
查看>>
OSPF技术连载18:OSPF网络类型:非广播、广播、点对多点、点对多点非广播、点对点
查看>>
OSPF技术连载19:深入解析OSPF特殊区域
查看>>
SQL Server 复制 订阅与发布
查看>>
OSPF技术连载20:OSPF 十大LSA类型,太详细了!
查看>>
OSPF技术连载21:OSPF虚链路,现代网络逻辑连接的利器!
查看>>
OSPF技术连载22:OSPF 路径选择 O > O IA > N1 > E1 > N2 > E2
查看>>
OSPF技术连载2:OSPF工作原理、建立邻接关系、路由计算
查看>>
OSPF技术连载5:OSPF 基本配置,含思科、华为、Junifer三厂商配置
查看>>
OSPF技术连载6:OSPF 多区域,近7000字,非常详细!
查看>>
OSPF技术连载7:什么是OSPF带宽?OSPF带宽参考值多少?
查看>>
OSPF技术连载8:OSPF认证:明文认证、MD5认证和SHA-HMAC验证
查看>>
OSPF故障排除技巧
查看>>
spring配置文件中<context:property-placeholder />的使用
查看>>