1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| package main
import ( "crypto/md5" "encoding/hex" "flag" "fmt" "io" "net/http" "os" "strconv" "time" )
var ( url = "被下载文件链接" data = make(map[string]int) logFile = flag.String("logFile", os.Getenv("HOME")+"/"+"network-card-test.log", "日志文件路径") )
func main() { flag.Parse() for { handle() } }
func handle() { md5Str, err := download(url) if err != nil { log(err.Error()) return }
defer func() { count := data[md5Str] data[md5Str] = count + 1 }()
count, found := data[md5Str] if !found && len(data) > 0 { log("文件MD5异常:" + md5Str + ", count=" + strconv.Itoa(count)) } else { log("文件MD5:" + md5Str + ", count=" + strconv.Itoa(count)) } }
func download(url string) (string, error) { var ( err error resp *http.Response contentLen int64 downloadLen int ) if resp, err = http.Get(url); err != nil { return "", err }
if contentLen, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64); err != nil { return "", err }
defer func() { _ = resp.Body.Close() fmt.Print("\n") }()
h := md5.New() for { buf := make([]byte, 1024) c, e := resp.Body.Read(buf) if e != nil && e != io.EOF { return "", e } downloadLen += c
printProgress(downloadLen, contentLen)
h.Write(buf[0:c])
if e != nil && e == io.EOF { break } } return hex.EncodeToString(h.Sum(nil)), nil }
func printProgress(downloadLen int, contentLen int64) { fmt.Printf("\r") fmt.Printf("Downloading... %d of %d", downloadLen, contentLen) }
func log(content string) { content = time.Now().Format("2006-01-02 15:04:05") + ": " + content
fmt.Println(content)
file, err := os.OpenFile(*logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) if err != nil { fmt.Println(err.Error()) return } defer func() { _ = file.Close() }()
_, err = file.WriteString(content + "\n") if err != nil { fmt.Println(err.Error()) } }
|