欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android基于HttpUrlConnection类的文件下载实例代码

程序员文章站 2023-12-18 17:50:52
废话不多说了,直接给大家贴代码了,具体代码如所示: /** * get方法的文件下载 *

* 特别说明 android...

废话不多说了,直接给大家贴代码了,具体代码如所示:

/**
   * get方法的文件下载
   * <p>
   * 特别说明 android中的progressbar是google唯一的做了处理的可以在子线程中更新ui的控件
   *
   * @param path
   */
  private void httpdown(final string path) {
    new thread() {
      @override
      public void run() {
        url url;
        httpurlconnection connection;
        try {
          //统一资源
          url = new url(path);
          //打开链接
          connection = (httpurlconnection) url.openconnection();
          //设置链接超时
          connection.setconnecttimeout(4000);
          //设置允许得到服务器的输入流,默认为true可以不用设置
          connection.setdoinput(true);
          //设置允许向服务器写入数据,一般get方法不会设置,大多用在post方法,默认为false
          connection.setdooutput(true);//此处只是为了方法说明
          //设置请求方法
          connection.setrequestmethod("get");
          //设置请求的字符编码
          connection.setrequestproperty("charset", "utf-8");
          //设置connection打开链接资源
          connection.connect();
          //得到链接地址中的file路径
          string urlfilepath = connection.geturl().getfile();
          //得到url地址总文件名 file的separatorchar参数表示文件分离符
          string filename = urlfilepath.substring(urlfilepath.lastindexof(file.separatorchar) + 1);
          //创建一个文件对象用于存储下载的文件 此次的getfilesdir()方法只有在继承至context类的类中
          // 可以直接调用其他类中必须通过context对象才能调用,得到的是内部存储中此应用包名下的文件路径
          //如果使用外部存储的话需要添加文件读写权限,5.0以上的系统需要动态获取权限 此处不在不做过多说明。
          file file = new file(getfilesdir(), filename);
          //创建一个文件输出流
          fileoutputstream outputstream = new fileoutputstream(file);
          //得到链接的响应码 200为成功
          int responsecode = connection.getresponsecode();
          if (responsecode == httpurlconnection.http_ok) {
            //得到服务器响应的输入流
            inputstream inputstream = connection.getinputstream();
            //获取请求的内容总长度
            int contentlength = connection.getcontentlength();
            //设置progressbar的max
            mpb.setmax(contentlength);
            //创建缓冲输入流对象,相对于inputstream效率要高一些
            bufferedinputstream bfi = new bufferedinputstream(inputstream);
            //此处的len表示每次循环读取的内容长度
            int len;
            //已经读取的总长度
            int totle = 0;
            //bytes是用于存储每次读取出来的内容
            byte[] bytes = new byte[1024];
            while ((len = bfi.read(bytes)) != -1) {
              //每次读取完了都将len累加在totle里
              totle += len;
              //每次读取的都更新一次progressbar
              mpb.setprogress(totle);
              //通过文件输出流写入从服务器中读取的数据
              outputstream.write(bytes, 0, len);
            }
            //关闭打开的流对象
            outputstream.close();
            inputstream.close();
            bfi.close();
            runonuithread(new runnable() {
              @override
              public void run() {
                toast.maketext(mainactivity.this, "下载完成!", toast.length_short).show();
              }
            });
          }
        } catch (exception e) {
          e.printstacktrace();
        }
      }
    }.start();
  }

总结

以上所述是小编给大家介绍的android基于httpurlconnection类的文件下载实例代码,希望对大家有所帮助

上一篇:

下一篇: