forked from golang/hotime
36 lines
602 B
Go
36 lines
602 B
Go
package download
|
|
|
|
import (
|
|
"bytes"
|
|
"go.hoteas.com/hotime"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
//下载文件
|
|
func Down(url, path, name string) bool {
|
|
|
|
os.MkdirAll(path, os.ModeDir)
|
|
if hotime.Substr(path, len(path)-1, len(path)) != "/" {
|
|
path = path + "/"
|
|
}
|
|
out, err := os.Create(path + name)
|
|
e := hotime.Error{}
|
|
if err != nil {
|
|
e.SetError(err)
|
|
return false
|
|
}
|
|
defer out.Close()
|
|
resp, err := http.Get(url)
|
|
defer resp.Body.Close()
|
|
pix, err := ioutil.ReadAll(resp.Body)
|
|
_, err = io.Copy(out, bytes.NewReader(pix))
|
|
if err != nil {
|
|
e.SetError(err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|