Skip to content

Commit 9f0a1af

Browse files
committed
* 修改插件下载403问题以及下载失败不能重新下载问题
1 parent 70e39e6 commit 9f0a1af

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/main/java/com/xwintop/xJavaFxTool/plugin/PluginManager.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.xwintop.xJavaFxTool.plugin;
22

3+
import cn.hutool.http.HttpStatus;
34
import cn.hutool.http.HttpUtil;
45
import com.alibaba.fastjson.JSON;
56
import com.xwintop.xJavaFxTool.model.PluginJarInfo;
67
import com.xwintop.xJavaFxTool.utils.Config;
78
import com.xwintop.xJavaFxTool.utils.Config.Keys;
9+
import com.xwintop.xJavaFxTool.utils.UserAgentUtils;
810
import com.xwintop.xJavaFxTool.utils.XJavaFxSystemUtil;
911
import java.io.File;
1012
import java.io.FileOutputStream;
@@ -212,15 +214,20 @@ public File downloadPlugin(
212214
this.currentProgressListener =
213215
(bytesRead, contentLength, done) -> onProgressUpdate.accept(contentLength, bytesRead);
214216

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--;
223223
}
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);
224231

225232
plugin.setIsDownload(true);
226233
plugin.setIsEnable(true);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.xwintop.xJavaFxTool.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @Description 用于i伪造请求的UserAgent
8+
* @Author LiFaXin
9+
* @Date 2020/5/14 17:24
10+
* @Version
11+
**/
12+
public class UserAgentUtils {
13+
14+
private static List<String> userAgents = new ArrayList<>(3);
15+
static{
16+
// Firefox
17+
userAgents.add("Mozilla/5.0 (Windows NT 6.1; rv:51.0) Gecko/20100101 Firefox/51.0");
18+
// Chrome
19+
userAgents.add("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.0 Safari/537.36");
20+
// IE
21+
userAgents.add("Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
22+
}
23+
24+
/**
25+
* 获取UserAgent
26+
* @param index
27+
* @return
28+
*/
29+
public static String getUserAgent(int index){
30+
index = index > userAgents.size()-1 ? userAgents.size()-1 : index;
31+
return userAgents.get(index);
32+
}
33+
34+
/**
35+
* 获取UserAgent配置数量
36+
* @return
37+
*/
38+
public static int getUserAgentSize(){
39+
return userAgents.size();
40+
}
41+
}

0 commit comments

Comments
 (0)