|
@ -0,0 +1,191 @@ |
|
|
|
|
|
// error debug
|
|
|
|
|
|
package file |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"errors" |
|
|
|
|
|
"io" |
|
|
|
|
|
"io/ioutil" |
|
|
|
|
|
"net/http" |
|
|
|
|
|
"os" |
|
|
|
|
|
|
|
|
|
|
|
//"os/exec"
|
|
|
|
|
|
"path" |
|
|
|
|
|
"path/filepath" |
|
|
|
|
|
|
|
|
|
|
|
//"runtime"
|
|
|
|
|
|
"log" |
|
|
|
|
|
"strings" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 文件下载 |
|
|
|
|
|
* param url 要下载的文件URL |
|
|
|
|
|
* param filename 文件保存路径(或文件保存名称) |
|
|
|
|
|
*/ |
|
|
|
|
|
func DownloadFile(url, filename string) error { |
|
|
|
|
|
|
|
|
|
|
|
if url == "" { |
|
|
|
|
|
return errors.New("文件下载地址为空") |
|
|
|
|
|
} |
|
|
|
|
|
//判断filename是路径还是文件名
|
|
|
|
|
|
fileIndex := strings.Index(filename, ".") |
|
|
|
|
|
|
|
|
|
|
|
var filePath string |
|
|
|
|
|
|
|
|
|
|
|
if fileIndex < 1 { //名称是路径
|
|
|
|
|
|
index := strings.Index(url, "?") |
|
|
|
|
|
if index > 0 { |
|
|
|
|
|
url1 := url[0:index] |
|
|
|
|
|
index2 := strings.LastIndex(url1, "/") |
|
|
|
|
|
//修改此处index2为index2+1,不然多了一个多余的/
|
|
|
|
|
|
fileName := url1[index2+1:] |
|
|
|
|
|
filePath = strings.TrimRight(filename, "/") + "/" + fileName |
|
|
|
|
|
} else { |
|
|
|
|
|
fileName := path.Base(url) //获取文件名带后缀
|
|
|
|
|
|
filePath = strings.TrimRight(filename, "/") + "/" + fileName |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
filePath = filename |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res, err := http.Get(url) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(filePath) |
|
|
|
|
|
|
|
|
|
|
|
CreatePath(dir) |
|
|
|
|
|
|
|
|
|
|
|
file, err := os.Create(filePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, copyerr := io.Copy(file, res.Body) |
|
|
|
|
|
if copyerr != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 获取远程接口信息 |
|
|
|
|
|
*/ |
|
|
|
|
|
func Geturl(url string) ([]byte, error) { |
|
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(url) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return []byte(""), err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return []byte(""), err |
|
|
|
|
|
} |
|
|
|
|
|
return body, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 文件是否存在 |
|
|
|
|
|
*/ |
|
|
|
|
|
func FileExists(path string) (bool, error) { |
|
|
|
|
|
_, err := os.Stat(path) |
|
|
|
|
|
if err == nil { |
|
|
|
|
|
return true, nil |
|
|
|
|
|
} |
|
|
|
|
|
if os.IsNotExist(err) { |
|
|
|
|
|
return false, nil |
|
|
|
|
|
} |
|
|
|
|
|
return false, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Copy a file
|
|
|
|
|
|
func Cpfile(originalFilePath, newFilePath string) error { |
|
|
|
|
|
// Open original file
|
|
|
|
|
|
originalFile, err := os.Open(originalFilePath) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer originalFile.Close() |
|
|
|
|
|
|
|
|
|
|
|
// Create new file
|
|
|
|
|
|
newFile, err := os.Create(newFilePath) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer newFile.Close() |
|
|
|
|
|
|
|
|
|
|
|
// Copy the bytes to destination from source
|
|
|
|
|
|
_, err = io.Copy(newFile, originalFile) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Commit the file contents
|
|
|
|
|
|
// Flushes memory to disk
|
|
|
|
|
|
err = newFile.Sync() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func WriteFile(filePath, data string) error { |
|
|
|
|
|
|
|
|
|
|
|
fileExist, err := FileExists(filePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var f *os.File |
|
|
|
|
|
|
|
|
|
|
|
if fileExist { //如果文件存在
|
|
|
|
|
|
f, err = os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) //打开文件
|
|
|
|
|
|
} else { |
|
|
|
|
|
f, err = os.Create(filePath) //创建文件
|
|
|
|
|
|
} |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_, err = io.WriteString(f, data) //写入文件(字符串)
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return err |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* read file |
|
|
|
|
|
*/ |
|
|
|
|
|
func ReadFile(path string) string { |
|
|
|
|
|
fi, err := os.Open(path) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return "" |
|
|
|
|
|
} |
|
|
|
|
|
defer fi.Close() |
|
|
|
|
|
fd, err := ioutil.ReadAll(fi) |
|
|
|
|
|
return string(fd) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
*读取文件内容(返回字符串) |
|
|
|
|
|
*/ |
|
|
|
|
|
func GetFileContent(FilePath string) string { |
|
|
|
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadFile(FilePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("ERROR", "read file error", FilePath, err) |
|
|
|
|
|
return "" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return string(buf) |
|
|
|
|
|
} |