android 文件下载 超简单

    public void downloadPlug(String downloadUrl,String savePath) {
        try {
            URL url = new URL(downloadUrl);
            //打开连接
            URLConnection conn = url.openConnection();
            //打开输入流
            InputStream is = conn.getInputStream();
            //获得长度
            int contentLength = conn.getContentLength();

            //下载后的文件名
            File downLoadFile = new File(savePath);
            if (downLoadFile.exists()) {
                downLoadFile.delete();
            }
            //创建字节流
            byte[] bs = new byte[1024];
            int len;
            OutputStream os = new FileOutputStream(downLoadFile);
            //写数据
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            //完成后关闭流
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/caoxinyu/p/6647779.html