天天看点

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

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//)+"M"
def len
def hasRead=;
byte[] arr=new byte[]
def out=new FileOutputStream("IQIYIsetup_1001_5.3.21.2659.exe")
def lastResult=
while ((len=stream.read(arr))!=-){
    out.write(arr,,len)
    hasRead+=len
    def decimal = hasRead/total*+""

    if(!decimal.equals("100"))
    decimal=decimal.substring(,decimal.indexOf("."))

    if(lastResult.equals(Integer.parseInt(decimal))){
        lastResult++

        println "下载进度:"+ decimal+"%"
    }



}

stream.close()
out.close()
println("下载完成!")


控制台输出如下:
初始文件大小为:M
下载进度:%
下载进度:%
下载进度:%
下载进度:%
下载进度:%
.
.
.
.



           

继续阅读