Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android文件下载功能实现代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了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,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,len);
      //计算累计下载的长度
      length += len;
     }
     threadfile.close();
     is.close();
     System.out.println("线程"+(threadid+1) + "已下载完成");
    }
   }catch(Exception eX){System.out.println("线程"+(threadid+1) + "下载出错"+ eX);}
  }
  
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android文件下载功能实现代码全部内容,希望文章能够帮你解决Android文件下载功能实现代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:文件下载