Android文件下载功能实现代码

作者:yl007 时间:2023-08-14 00:58:55 

本文实例为大家分享了Android文件下载功能的具体代码,供大家参考,具体内容如下

1.普通单线程下载文件:

直接使用URLConnection.openStream()打开网络输入流,然后将流写入到文件中!


public static void downLoad(String path,Context context)throws Exception
{
URL url = new URL(path);
InputStream is = url.openStream();
//截取最后的文件名
String end = path.substring(path.lastIndexOf("."));
//打开手机对应的输出流,输出到文件中
OutputStream os = context.openFileOutput("Cache_"+System.currentTimeMillis()+end, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len = 0;
//从输入六中读取数据,读到缓冲区中
while((len = is.read(buffer)) > 0)
{
 os.write(buffer,0,len);
}
//关闭输入输出流
is.close();
os.close();
}

2.普通多线程下载:

步骤:

  • 获取网络连接

  • 本地磁盘创建相同大小的空文件

  • 计算每条线程需从文件哪个部分开始下载,结束

  • 依次创建,启动多条线程来下载网络资源的指定部分


public class Downloader {
//添加@Test标记是表示该方法是Junit测试的方法,就可以直接运行该方法了
 @Test
 public void download() throws Exception
 {
  //设置URL的地址和下载后的文件名
  String filename = "meitu.exe";
  String path = "http://10.13.20.32:8080/Test/XiuXiu_Green.exe";
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setConnectTimeout(5000);
  conn.setRequestMethod("GET");
  //获得需要下载的文件的长度(大小)
  int filelength = conn.getContentLength();
  System.out.println("要下载的文件长度"+filelength);
  //生成一个大小相同的本地文件
  RandomAccessFile file = new RandomAccessFile(filename, "rwd");
  file.setLength(filelength);
  file.close();
  conn.disconnect();
  //设置有多少条线程下载
  int threadsize = 3;
  //计算每个线程下载的量
  int threadlength = filelength % 3 == 0 ? filelength/3:filelength+1;
  for(int i = 0;i < threadsize;i++)
  {
   //设置每条线程从哪个位置开始下载
   int startposition = i * threadlength;
   //从文件的什么位置开始写入数据
   RandomAccessFile threadfile = new RandomAccessFile(filename, "rwd");
   threadfile.seek(startposition);
   //启动三条线程分别从startposition位置开始下载文件
   new DownLoadThread(i,startposition,threadfile,threadlength,path).start();
  }
  int quit = System.in.read();
  while('q' != quit)
  {
   Thread.sleep(2000);
  }
 }

private class DownLoadThread extends Thread {
 private int threadid;
 private int startposition;
 private RandomAccessFile threadfile;
 private int threadlength;
 private String path;
 public DownLoadThread(int threadid, int startposition,
   RandomAccessFile threadfile, int threadlength, String path) {
  this.threadid = threadid;
  this.startposition = startposition;
  this.threadfile = threadfile;
  this.threadlength = threadlength;
  this.path = path;
 }
 public DownLoadThread() {}
 @Override
 public void run() {
  try
  {
   URL url = new URL(path);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setConnectTimeout(5000);
   conn.setRequestMethod("GET");
   //指定从什么位置开始下载
   conn.setRequestProperty("Range", "bytes="+startposition+"-");
   //System.out.println(conn.getResponseCode());
   if(conn.getResponseCode() == 206)
   {
    InputStream is = conn.getInputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    int length = 0;
    while(length < threadlength && (len = is.read(buffer)) != -1)
    {
     threadfile.write(buffer,0,len);
     //计算累计下载的长度
     length += len;
    }
    threadfile.close();
    is.close();
    System.out.println("线程"+(threadid+1) + "已下载完成");
   }
  }catch(Exception ex){System.out.println("线程"+(threadid+1) + "下载出错"+ ex);}
 }

}
}
标签:Android,文件下载
0
投稿

猜你喜欢

  • spring mvc中@RequestBody注解的作用说明

    2022-04-07 14:15:02
  • Android提高之多级树形菜单的实现方法

    2021-08-27 08:26:48
  • Spring Cloud Alibaba Nacos 入门详解

    2022-02-23 12:29:09
  • java使用TimerTask定时器获取指定网络数据

    2022-08-14 10:52:20
  • 初识Java基础之数据类型与运算符

    2021-10-13 12:20:32
  • 单点登录的三种方式和JWT的介绍与使用

    2023-05-19 22:10:59
  • C#堆排序实现方法

    2022-03-17 02:48:46
  • SpringBoot配置Profile实现多环境支持

    2023-07-29 21:53:20
  • 使用springboot整合RateLimiter限流过程

    2022-09-12 21:42:48
  • java对象和json的来回转换知识点总结

    2023-08-01 23:35:57
  • C#实现为一张大尺寸图片创建缩略图的方法

    2021-08-03 21:07:15
  • spring强行注入和引用实例解析

    2021-11-23 23:26:19
  • C#简单实现SNMP的方法

    2021-11-25 18:42:38
  • C#实现窗体淡入淡出效果的方法总结

    2021-05-28 08:23:37
  • Java使用substring()截取(提取)子字符串

    2023-09-29 17:02:04
  • java 启动exe程序,传递参数和获取参数操作

    2023-09-11 04:30:47
  • springboot相关面试题汇总详解

    2023-10-06 17:16:11
  • spring中向一个单例bean中注入非单例bean的方法详解

    2022-07-19 13:14:18
  • Java全面详细讲解逻辑控制的使用

    2021-10-16 12:48:42
  • android viewpaper实例探讨

    2023-03-21 12:47:55
  • asp之家 软件编程 m.aspxhome.com