Android中如何实现文件下载

本文介绍如何在Android应用中使用URLConnection实现文件下载功能。包括获取文件长度、选择存储路径及完成下载流程的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.itivy.com/android/archive/2011/6/28/android-file-download.html

我们在开发中经常需要从服务器下载文件,下载的内容可能有交换的信息,缓存的图片,程序更新包等。我们使用URLConnection来实现下载。先看几行代码:

1
2
3
4
5
6
7
String urlDownload = "" ;
URL url = new URL(urlDownload );   
// 打开连接   
URLConnection con = url.openConnection();
// 输入流   
InputStream is = con.getInputStream();
如上面的代码所示,指定一个下载的目标链接,我们上面指定了一个图片地址。然后构建一个URL对象,调用该对象的openConnection方法来建立一个数据通路(连接)。代码的最后一行使用 con.getInputStream,拿到一个输入流对象,通过这个流对象我们就可以读取到这个文件的内容了。下面要做的,就是读取这个流,将流写入我们的本地文件。不过在这之前,我们还要说下这个方法:
1
2
3
<SPAN xmlns= "http://www.w3.org/1999/xhtml" >//获得文件的长度
int contentLength = con.getContentLength();
System. out .println( "长度 :" +contentLength)</SPAN>
获得文件长度的方法。ContentLength是不很熟啊。它是http协议里描述头(head)部分的描述属性之一。实际这里是发送了一个http请求,分析了返回(response)里数据内容。

我们常常会把文件下载到手机的存储卡里,所以还会用到获得存储卡路径的方法:

1
2
3
4
5
6
7
8
<SPAN xmlns= "http://www.w3.org/1999/xhtml" >// 获得存储卡路径,构成 保存文件的目标路径
String dirName = "" ;
dirName = Environment.getExternalStorageDirectory()+ "/MyDownload/" ;
File f = new File(dirName);
if (!f.exists())
{
     f.mkdir();
}</SPAN>
Environment.getExternalStorageDirectory() 方法会返回一个字符串,指示了存储卡的路径。我们拼接字符串出一个准备存放下载文件的文件夹。并先判断文件夹是是否存在,如果不存在,则新建一个文件夹。

做完了上面的准备后,基本就能实现下载了。我们看看主要的完整代码。

下载前的准备工作:

1
2
3
4
5
6
7
8
9
10
11
12
//要下载的文件路径
String urlDownload = "" ;
urlDownload = "http: //www.baidu.com/img/baidu_sylogo1.gif%22;
// 获得存储卡路径,构成 保存文件的目标路径
String dirName = "" ;
dirName = Environment.getExternalStorageDirectory()+ "/MyDownload/" ;
File f = new File(dirName);
if (!f.exists())
{
     f.mkdir();
}
下载的操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//准备拼接新的文件名(保存在存储卡后的文件名)
String newFilename = _urlStr.substring(_urlStr.lastIndexOf( "/" )+1);
newFilename = _dirName + newFilename;
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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值