在某些业务场景下,我们需要获取应用服务器传过来的url地址来判断文件是否是重复的,就可以对每一个url对应的文件计算md5,然后将md5进行对比,一样的话就证明该图片是已经存在的
package main
import (
"crypto/md5"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url_1 :="http://m10.music.126.net/20201011153043/e6ee3cceb079c8acb94a83485a3059e5/ymusic/5408/520f/545a/cbd0b59720ee5e9f4f8cc0f7615506b2.mp3"
url_2 :="http://qi0z0c1kx.hn-bkt.clouddn.com/5408_520f_545a_cbd0b59720ee5e9f4f8cc0f7615506b2.mp3"
resp_1, err := http.Get(url_1)
if err != nil {
fmt.Println("error http.Get")
}
resp_2, err := http.Get(url_2)
if err != nil {
fmt.Println(err)
}
defer resp_1.Body.Close()
defer resp_2.Body.Close()
bytes_1, err := ioutil.ReadAll(resp_1.Body)
if err != nil {
fmt.Println(err)
}
bytes_2, err := ioutil.ReadAll(resp_2.Body)
if err != nil {
fmt.Println(err)
}
sum_1 := md5.Sum(bytes_1)
sum_2 := md5.Sum(bytes_2)
s1:=fmt.Sprintf("%x",sum_1)
s2:=fmt.Sprintf("%x",sum_2)
fmt.Println(s1)
fmt.Println(s2)
}