实现Android从远程服务器下载文件到本地sd卡中。

 

File file = new File(newFilename);//如果目标文件已经存在,则删除。产生覆盖旧文件的效果if(file.exists()){    file.delete();}try {         // 构造URL           URL url = new URL(_urlStr);            // 打开连接            URLConnection con = url.openConnection();         //获得文件的长度         int contentLength = con.getContentLength();         System.out.println("长度 :"+contentLength);         // 输入流            InputStream is = con.getInputStream();           // 1K的数据缓冲            byte[] bs = new byte[1024];            // 读取到的数据长度            int len;            // 输出的文件流            OutputStream os = new FileOutputStream(newFilename);            // 开始读取            while ((len = is.read(bs)) != -1) {                os.write(bs, 0, len);            }           // 完毕,关闭所有链接            os.close();           is.close();            } catch (Exception e) {        e.printStackTrace();}


 

更多相关文章

  1. android ndk 调用第三方so
  2. Unbuntu下Android(安卓)studio报Unable to recreate missing deb
  3. 什么是AIDL
  4. Andorid UI设计技巧
  5. android 混淆文件project.properties和proguard-project.txt
  6. Android开发环境搭建和简单介绍
  7. Android(安卓)读取sdcard指定目录文件
  8. Android(安卓)Device Monitor无法查看data目录解决办法
  9. NPM 和webpack 的基础使用

随机推荐

  1. 【Android】 从头搭建视频播放器(3)——手
  2. Native层HIDL服务的获取原理-Android10.0
  3. Android WiFi接口理解
  4. Android getDimensionPixelSize, 代码中
  5. Android学习系列之三
  6. Android编译系统简要介绍和学习计划
  7. Android(安卓)网络通信——Volley
  8. android如何支持多屏幕
  9. 《Android 开发工程师面试指南》
  10. 分享两种最简单的Android打渠道包的方法