加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy下载文件并实时显示下载进度

发布时间:2020-12-14 16:43:54 所属栏目:大数据 来源:网络整理
导读:def stream = new URL( "http://sw.bos.baidu.com/sw-search-sp/software/ae80092c654/IQIYIsetup_1001_5.3.21.2659.exe" ).openStream() def stream2 = new URL( "http://sw.bos.baidu.com/sw-search-sp/software/ae80092c654/IQIYIsetup_1001_5.3.21.2659.
def stream = new URL("http://sw.bos.baidu.com/sw-search-sp/software/ae80092c654/IQIYIsetup_1001_5.3.21.2659.exe").openStream() def stream2 = new URL("http://sw.bos.baidu.com/sw-search-sp/software/ae80092c654/IQIYIsetup_1001_5.3.21.2659.exe").openConnection() def total = stream2.getContentLength() println "初始文件大小为:"+(total/1024/1024)+"M" def len def hasRead=0; byte[] arr=new byte[1024] def out=new FileOutputStream("IQIYIsetup_1001_5.3.21.2659.exe") def lastResult=0 while ((len=stream.read(arr))!=-1){ out.write(arr,0,len) hasRead+=len def decimal = hasRead/total*100+"" if(!decimal.equals("100")) decimal=decimal.substring(0,decimal.indexOf(".")) if(lastResult.equals(Integer.parseInt(decimal))){ lastResult++ println "下载进度:"+ decimal+"%" } } stream.close() out.close() println("下载完成!") 控制台输出如下: 初始文件大小为:36.3428955078125M 下载进度:0% 下载进度:1% 下载进度:2% 下载进度:3% 下载进度:4% . . . . 

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读