Skip to content

Commit d5e2608

Browse files
committed
修复无法下载问题
1 parent 26b6184 commit d5e2608

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

main.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,33 +150,41 @@ func runInstaller(installerPath string, loader string, version string, loaderVer
150150

151151
// 下载文件的函数
152152
func downloadFile(url, filePath string) error {
153-
resp, err := http.Get(url)
154-
if err != nil {
155-
return fmt.Errorf("无法下载文件: %v", err)
156-
}
157-
defer resp.Body.Close()
153+
const maxRetries = 3
154+
var err error
155+
for i := 0; i < maxRetries; i++ {
156+
resp, err := http.Get(url)
157+
if err != nil {
158+
fmt.Printf("下载文件尝试 %d/%d 失败: %v\n", i+1, maxRetries, err)
159+
continue
160+
}
161+
defer resp.Body.Close()
158162

159-
if resp.StatusCode == http.StatusNotFound {
160-
fmt.Printf("文件未找到,跳过下载: %s\n", url)
161-
return nil
162-
}
163+
if resp.StatusCode == http.StatusNotFound {
164+
fmt.Printf("文件未找到,跳过下载: %s\n", url)
165+
return nil
166+
}
163167

164-
if resp.StatusCode != http.StatusOK {
165-
return fmt.Errorf("下载文件失败,状态码: %d", resp.StatusCode)
166-
}
168+
if resp.StatusCode != http.StatusOK {
169+
fmt.Printf("下载文件失败,状态码: %d\n", resp.StatusCode)
170+
continue
171+
}
167172

168-
file, err := os.Create(filePath)
169-
if err != nil {
170-
return fmt.Errorf("无法创建文件: %v", err)
171-
}
172-
defer file.Close()
173+
file, err := os.Create(filePath)
174+
if err != nil {
175+
return fmt.Errorf("无法创建文件: %v", err)
176+
}
177+
defer file.Close()
173178

174-
_, err = io.Copy(file, resp.Body)
175-
if err != nil {
176-
return fmt.Errorf("无法写入文件: %v", err)
177-
}
179+
_, err = io.Copy(file, resp.Body)
180+
if err != nil {
181+
fmt.Printf("写入文件尝试 %d/%d 失败: %v\n", i+1, maxRetries, err)
182+
continue
183+
}
178184

179-
return nil
185+
return nil
186+
}
187+
return fmt.Errorf("下载文件失败,经过 %d 次尝试: %v", maxRetries, err)
180188
}
181189

182190
// 从 JAR 文件中提取 version.json

0 commit comments

Comments
 (0)