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

你可能感兴趣的文章
OSG学习:纹理映射(六)——灯光
查看>>
OSG学习:纹理映射(四)——三维纹理映射
查看>>
OSG:从源码看Viewer::run() 一
查看>>
osi 负载均衡
查看>>
OSI七层模型与TCP/IP五层模型(转)
查看>>
OSI七层模型与TCP/IP四层与五层模型详解
查看>>
OSI七层模型的TCP/IP模型都有哪几层和他们的对应关系?
查看>>
OSI操作系统(NETBASE第八课)
查看>>
OSM数据如何下载使用(地图数据篇.11)
查看>>
OSPF 四种设备角色:IR、ABR、BR、ASBR
查看>>
OSPF 四种路由类型:Intra Area、Inter Area、第一、二类外部路由
查看>>
OSPF 学习
查看>>
OSPF 支持的网络类型:广播、NBMA、P2MP和P2P类型
查看>>
OSPF 概念型问题
查看>>
OSPF 的主要目的是什么?
查看>>
OSPF5种报文:Hello报文、DD报文、LSR报文、LSU报文和LSAck报文
查看>>
SQL Server 存储过程分页。
查看>>
OSPFv3:第三版OSPF除了支持IPv6,还有这些强大的特性!
查看>>
OSPF不能发现其他区域路由时,该怎么办?
查看>>
OSPF两个版本:OSPFv3与OSPFv2到底有啥区别?
查看>>