hotime/dri/download/download.go

40 lines
618 B
Go
Raw Normal View History

2017-11-07 01:21:48 +00:00
package download
import (
2021-05-23 23:27:41 +00:00
. "../../common"
2017-11-07 01:21:48 +00:00
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
)
//下载文件
func Down(url, path, name string) bool {
os.MkdirAll(path, os.ModeDir)
2021-05-23 23:27:41 +00:00
if Substr(path, len(path)-1, len(path)) != "/" {
2017-11-07 01:21:48 +00:00
path = path + "/"
}
out, err := os.Create(path + name)
2021-05-23 23:27:41 +00:00
e := Error{}
2017-11-07 01:21:48 +00:00
if err != nil {
e.SetError(err)
return false
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
2017-12-01 07:33:19 +00:00
return false
}
2017-11-07 01:21:48 +00:00
defer resp.Body.Close()
2017-12-01 07:33:19 +00:00
2017-11-07 01:21:48 +00:00
pix, err := ioutil.ReadAll(resp.Body)
_, err = io.Copy(out, bytes.NewReader(pix))
if err != nil {
e.SetError(err)
return false
}
return true
}