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

你可能感兴趣的文章
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理解决跨域问题(导致图片只能预览不能下载)
查看>>
Nginx代理访问提示ERR_CONTENT_LENGTH_MISMATCH
查看>>
Nginx代理配置详解
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
nginx优化日志拒绝特定404请求写入
查看>>
Nginx优化解析
查看>>
Nginx使用proxy_cache指令设置反向代理缓存静态资源
查看>>
Nginx做反向代理时访问端口被自动去除
查看>>
Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例
查看>>
Nginx入门简介和反向代理、负载均衡、动静分离理解
查看>>
nginx入门篇----nginx服务器基础配置
查看>>
vue中参数传不到后台去怎么办?
查看>>
nginx反向代理
查看>>
Nginx反向代理
查看>>
nginx反向代理、文件批量改名及统计ip访问量等精髓总结
查看>>