Android中怎么实现文件下载功能

今天就跟大家聊聊有关Android中怎么实现文件下载功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

建昌网站建设公司成都创新互联,建昌网站设计制作,有大型网站制作公司丰富经验。已为建昌近1000家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的建昌做网站的公司定做!

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中怎么实现文件下载功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。

网站名称:Android中怎么实现文件下载功能
本文网址:https://www.cdcxhl.com/article46/jcjchg.html

成都网站建设公司_创新互联,为您提供Google关键词优化面包屑导航动态网站网站排名服务器托管

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司