当前位置: 移动技术网 > IT编程>移动开发>Android > Android文件下载功能实现代码

Android文件下载功能实现代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

火灭中文小说网,墨鲤抄,户外广告网

本文实例为大家分享了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);}
  }
  
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网