实现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文件访问权限问题
  2. Android的string-array数据源简单使用
  3. Android数据库工具ORM-greenDAO学习
  4. Android中SQLite数据库存储方式 .(转)
  5. Android jni调用第三方so库和.h文件
  6. android studio的Gradle一直在sync的办法,不停的下载各种文件

随机推荐

  1. Android那些疑惑(2)-LayoutInflater中inf
  2. Android(Java):onChange效果实现
  3. Android(安卓)向菜单按钮说再见
  4. Android之SharedPreferences轻量数据存储
  5. Android(安卓)Intent 大全 截止到Level 8
  6. IndicatorViewPager (笔记)
  7. android实现双击事件的监听
  8. Port SDL/TinySDGL to android with nati
  9. Android(安卓)View之组合控件实例(一)
  10. Android(安卓)中如何自己通过代码绘图