下载器支持代理

This commit is contained in:
Yutousama 2022-10-13 23:28:04 +08:00
parent f0d61ffbb6
commit 1908c905ac
4 changed files with 18 additions and 8 deletions

View File

@ -69,7 +69,7 @@ public class AppController {
return "not message"; return "not message";
} }
if (json.containsKey("image")) { if (json.containsKey("image")) {
image = HttpTools.syncDownload(json.getString("image"), System.currentTimeMillis() + ".png"); image = HttpTools.syncDownload(json.getString("image"), System.currentTimeMillis() + ".png",true);
} }
if (image != null) { if (image != null) {
ret = QQBotManager.getInstance().sendMessage(image, json.getLong("qq"), json.getString("message")); ret = QQBotManager.getInstance().sendMessage(image, json.getLong("qq"), json.getString("message"));

View File

@ -88,7 +88,7 @@ public class Bangumi extends Model {
return; return;
} }
for (String img : imgs) { for (String img : imgs) {
File file = HttpTools.syncDownload(img.replace("http://", "https://"), key + ".jpg"); File file = HttpTools.syncDownload(img.replace("http://", "https://"), key + ".jpg",false);
files.add(file); files.add(file);
send(imgs.size(), qq, text); send(imgs.size(), qq, text);
} }

View File

@ -24,8 +24,8 @@ public class HttpTools {
return http_post(url, body, 0, null); return http_post(url, body, 0, null);
} }
public static File syncDownload(final String url, final String saveName) { public static File syncDownload(final String url, final String saveName,boolean isProxy) {
return new HttpTools().http_syncDownload(url, saveName); return new HttpTools().http_syncDownload(url, saveName,isProxy);
} }
public static String https_get(String url, Map<String, String> header) { public static String https_get(String url, Map<String, String> header) {
@ -158,7 +158,7 @@ public class HttpTools {
} }
public static void main(String[] args) { public static void main(String[] args) {
File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg"); File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg",false);
System.out.println("file.length() = " + file.length()); System.out.println("file.length() = " + file.length());
} }
@ -249,7 +249,7 @@ public class HttpTools {
}).start(); }).start();
} }
public synchronized File http_syncDownload(final String url, final String saveName) { public synchronized File http_syncDownload(final String url, final String saveName,boolean isProxy) {
if (StringUtils.isEmpty(url)) { if (StringUtils.isEmpty(url)) {
return null; return null;
} }
@ -259,8 +259,18 @@ public class HttpTools {
if (!savePath.exists()) { if (!savePath.exists()) {
savePath.mkdirs(); savePath.mkdirs();
} }
Proxy proxy = null;
if (isProxy) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
}
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName); Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); HttpURLConnection connection;
if(proxy==null) {
connection = (HttpURLConnection) new URL(url).openConnection();
}else{
connection = (HttpURLConnection) new URL(url).openConnection(proxy);
}
connection.addRequestProperty("User-Agent", getExtUa()); connection.addRequestProperty("User-Agent", getExtUa());
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());

View File

@ -30,7 +30,7 @@ public class IdeaTools {
} }
public static List<String> getIdeaList(String url) { public static List<String> getIdeaList(String url) {
File file = HttpTools.syncDownload(url, "idea.zip"); File file = HttpTools.syncDownload(url, "idea.zip",false);
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
try { try {
ZipFile zip = new ZipFile(file, Charset.forName("gbk")); ZipFile zip = new ZipFile(file, Charset.forName("gbk"));