天天看點

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
下載下傳進度:%
下載下傳進度:%
下載下傳進度:%
下載下傳進度:%
下載下傳進度:%
.
.
.
.



           

繼續閱讀