|
1 | 1 | package com.xwintop.xJavaFxTool.plugin; |
2 | 2 |
|
| 3 | +import cn.hutool.http.HttpStatus; |
3 | 4 | import cn.hutool.http.HttpUtil; |
4 | 5 | import com.alibaba.fastjson.JSON; |
5 | 6 | import com.xwintop.xJavaFxTool.model.PluginJarInfo; |
6 | 7 | import com.xwintop.xJavaFxTool.utils.Config; |
7 | 8 | import com.xwintop.xJavaFxTool.utils.Config.Keys; |
| 9 | +import com.xwintop.xJavaFxTool.utils.UserAgentUtils; |
8 | 10 | import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil; |
9 | 11 | import java.io.File; |
10 | 12 | import java.io.FileOutputStream; |
@@ -212,15 +214,20 @@ public File downloadPlugin( |
212 | 214 | this.currentProgressListener = |
213 | 215 | (bytesRead, contentLength, done) -> onProgressUpdate.accept(contentLength, bytesRead); |
214 | 216 |
|
215 | | - try ( |
216 | | - Response response = pluginDownloader |
217 | | - .newCall(new Builder().url(pluginJarInfo.getDownloadUrl()).build()) |
218 | | - .execute(); |
219 | | - InputStream inputStream = response.body().byteStream(); |
220 | | - FileOutputStream outputStream = new FileOutputStream(file) |
221 | | - ) { |
222 | | - IOUtils.copy(inputStream, outputStream); |
| 217 | + // 插件下载 |
| 218 | + int retryTimes = UserAgentUtils.getUserAgentSize()-1; |
| 219 | + Response response = null; |
| 220 | + while (retryTimes > 0 && (response== null || response.code() != HttpStatus.HTTP_OK)){ |
| 221 | + response = pluginDownloader.newCall(new Builder().header("User-Agent", UserAgentUtils.getUserAgent(retryTimes)).url(pluginJarInfo.getDownloadUrl()).build()).execute(); |
| 222 | + retryTimes--; |
223 | 223 | } |
| 224 | + // 下载仍未成功,需要抛出异常,提示下载失败 |
| 225 | + if(response== null || response.code() != HttpStatus.HTTP_OK){ |
| 226 | + throw new IOException("插件下载失败 " + pluginJarInfo.getJarName()); |
| 227 | + } |
| 228 | + InputStream inputStream = response.body().byteStream(); |
| 229 | + FileOutputStream outputStream = new FileOutputStream(file); |
| 230 | + IOUtils.copy(inputStream, outputStream); |
224 | 231 |
|
225 | 232 | plugin.setIsDownload(true); |
226 | 233 | plugin.setIsEnable(true); |
|
0 commit comments