大量的更新

This commit is contained in:
zeaslity
2025-02-26 17:44:03 +08:00
parent b8170e00d4
commit c751c21871
8 changed files with 323 additions and 42 deletions

View File

@@ -0,0 +1,43 @@
package op
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func DownloadFile(url string, path string) (bool, string) {
// 创建HTTP客户端
client := &http.Client{
Timeout: 5 * time.Second,
}
// 发送GET请求
resp, err := client.Get(url)
if err != nil {
return false, fmt.Sprintf("下载文件失败: %v", err)
}
defer resp.Body.Close()
// 检查响应状态码
if resp.StatusCode != http.StatusOK {
return false, fmt.Sprintf("下载文件失败HTTP状态码: %d", resp.StatusCode)
}
// 创建目标文件
out, err := os.Create(path)
if err != nil {
return false, fmt.Sprintf("创建文件失败: %v", err)
}
defer out.Close()
// 将响应内容写入文件
_, err = io.Copy(out, resp.Body)
if err != nil {
return false, fmt.Sprintf("写入文件失败: %v", err)
}
return true, fmt.Sprintf("文件下载成功: %s", path)
}