Package Main
Import (
"Flag"
"FMT"
"IO"
"Log"
"Net/http"
"OS"
"StrConv"
"Strings"
"Time"
"GITHUB.COM/CHEGGAAA/PB"
)
VAR (
URL = flag. String ("url", "", "the download file URL.")
)
Func Usage () {
Fmt. Printf ("Usage of%s:\n-url=\" http://www.xxx.com/file.exe\ ": The Download file url.\n", Os. Args[0])
Os. Exit (1)
}
Func Main () {
Flag. Parse ()
If OS. Args = = Nil | | *url = = "" {
Usage ()
}
UrlMap: = Strings. Split (*url, "/")
FileName: = Urlmap[len (UrlMap)-1]
If strings. Contains (fileName, "=") {
Splitname: = Strings. Split (fileName, "=")
FileName = Splitname[len (splitname)-1]
}
RESP, err: = http. Get (*url)
If err! = Nil {
Log. Fatal (ERR)
}
If resp. StatusCode = = http. Statusok {
Log. Printf ("[INFO] Downloading: [%s]", FileName)
Fmt. Print ("\ n")
Downfile, err: = OS. Create (FileName)
If err! = Nil {
Log. Fatal (ERR)
}
I, _: = StrConv. Atoi (resp. Header.get ("Content-length"))
Sourcesiz: = Int64 (i)
Source: = resp. Body
Bar: = PB. New (int (sourcesiz)). Setunits (PB. U_bytes). Setrefreshrate (Time.millisecond * 10)
Bar. Start ()
Bar. Showspeed = True
Bar. Showfinaltime = True
Bar. Setmaxwidth (80)
Writer: = Io. Multiwriter (Downfile, bar)
Io. Copy (writer, source)
Bar. Finish ()
Fmt. Print ("\ n")
Log. Printf ("[INFO] [%s] Download succeeded.", FileName)
} else {
Fmt. Print ("\ n")
Log. Printf ("[ERROR] [%s] download failed,%s.", FileName, resp. Status)
}
}
This article is from the "Codyguo" blog, make sure to keep this source http://codyguo.blog.51cto.com/10440635/1701368
HTTP download with progress bar under black window