Android 下载文件 获取 下载信息

1.获取下载文件大小、下载开始时间、下载结束时间、下载平均速率、下载时延、下载结果等。

 1 //开始时间,放在循环外,求解的usedTime就是总时间  
 2         long downloadStartTime = new Date().getTime();
 3         long startTime = System.currentTimeMillis();
 4         try {
 5         URL url = new URL(address);
 6         HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 7             conn.setConnectTimeout(4*1000);
 8             conn.setRequestMethod("GET");
 9             conn.setDoInput(true);
10             conn.setRequestProperty(
11                     "Accept",
12                     "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +  
13                     "application/x-shockwave-flash, application/xaml+xml, " +  
14                     "application/vnd.ms-xpsdocument, application/x-ms-xbap, " +  
15                     "application/x-ms-application, application/vnd.ms-excel, " +  
16                     "application/vnd.ms-powerpoint, application/msword, */*");  
17             conn.setRequestProperty("Accept-Language", "zh-CN");  
18             conn.setRequestProperty("Charset", "UTF-8");  
19             //设置浏览器类型和版本、操作系统,使用语言等信息  
20             conn.setRequestProperty(  
21                     "User-Agent",  
22                     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; " +  
23                     ".NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; " +  
24                     ".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");  
25             //设置为长连接  
26             conn.setRequestProperty("Connection", "Keep-Alive");
27             LogUtil.i("======>getResponseCode:" + conn.getResponseCode());
28             fileSize = conn.getContentLength();
29             LogUtil.i("======>将要下载文件的大小为:" + fileSize);
30             InputStream is = conn.getInputStream();
31             if (fileSize <= 0) fileSize = 4508876;
32             if (is == null) throw new RuntimeException("stream is null");
33             OutputStream outputstream = null;
34             byte[] buffer = new byte[1024 * 1];
35             if(FileUtils.isExist(Config.SD_CARD_ROOT + fileName)){
36                 //文件存在,进行删除操作
37                 FileUtils.deleteFile(Config.SD_CARD_ROOT + fileName);
38             }
39             
40             FileUtils.createFolder(FileUtils.getSDCardBasePath() 
41                     + Config.SD_CARD_ROOT);
42             File file = FileUtils.getTxtFile(FileUtils.getSDCardBasePath() 
43                     + Config.SD_CARD_ROOT, fileName);
44             outputstream = new FileOutputStream(file);
45             int readbuffer = 0;
46             do
47             {
48                 readbuffer = is.read(buffer);
49                 //下载完成
50                 if(readbuffer == -1){
51                     break;
52                 }
53                 //将输入流中的内容先输入到buffer中缓存,然后用输出流写到文件中
54                 outputstream.write(buffer , 0, readbuffer);
55                 downloadSize += readbuffer;
56                 LogUtil.i("下载大小: = " + downloadSize);
57                 // 计算下载URL网页内容百分比
58                 downloadPercent = (int) (downloadSize * 100 / fileSize);
59                 LogUtil.i("下载百分比: = " + downloadPercent + "%"); 
60                 curTime = System.currentTimeMillis();
61                 LogUtil.i("当前时间: = " + curTime);
62                 usedTime = (int) (curTime - startTime) / 1000;
63                 LogUtil.i("使用时间: " + usedTime);
64                 if(usedTime==0)usedTime = 1000;
65                 downloadSpeed = (downloadSize / usedTime) / 1000;
66                 LogUtil.i("下载速度: " + downloadSpeed);
67             } while (true);
68             outputstream.flush();
69             //记录指标
70             long endTime = new Date().getTime();
71             taskMap.put("下载开始时间", DateUtils
72                     .convertDateToLongString(new Date(downloadStartTime)));
73             long downLoadDelay = (endTime - downloadStartTime);
74             taskMap.put("下载结束时间", DateUtils.convertDateToLongString(new Date(endTime)));
75             taskMap.put("下载时延", downLoadDelay + "");
76             taskMap.put("下载结果", "0");
77             taskMap.put("下载文件大小", (fileSize / 1024) + "KB");
78             taskMap.put("平均下载速度", fileSize / downLoadDelay + "KB/s");
79             //发送完成信息
80             84             is.close();
85           //下载完成
86         } catch (Exception e) {
87             Message msg = new Message();
88             msg.what = ActionCode.TEST_ERROR;
89             msg.obj = itemViewsMap;
90             handler.sendMessageDelayed(msg, 3000);
91             //下载失败,防止is关闭导致的异常
92 
93             e.printStackTrace();
94         }

 

posted @ 2015-09-06 16:56  晕菜一员  阅读(687)  评论(0)    收藏  举报