From 1908c905acfaa1ec9c687c0cfa31acfd04153965 Mon Sep 17 00:00:00 2001 From: Yutousama <583819556@qq.com> Date: Thu, 13 Oct 2022 23:28:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=99=A8=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qqbot/Controllers/AppController.java | 2 +- .../yutou/qqbot/models/Commands/Bangumi.java | 2 +- .../java/com/yutou/qqbot/utlis/HttpTools.java | 20 ++++++++++++++----- .../java/com/yutou/qqbot/utlis/IdeaTools.java | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/yutou/qqbot/Controllers/AppController.java b/src/main/java/com/yutou/qqbot/Controllers/AppController.java index c733677..7ce89b4 100644 --- a/src/main/java/com/yutou/qqbot/Controllers/AppController.java +++ b/src/main/java/com/yutou/qqbot/Controllers/AppController.java @@ -69,7 +69,7 @@ public class AppController { return "not message"; } 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) { ret = QQBotManager.getInstance().sendMessage(image, json.getLong("qq"), json.getString("message")); diff --git a/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java index 55a2c03..807964f 100644 --- a/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java +++ b/src/main/java/com/yutou/qqbot/models/Commands/Bangumi.java @@ -88,7 +88,7 @@ public class Bangumi extends Model { return; } 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); send(imgs.size(), qq, text); } diff --git a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java index 87b329c..4e57641 100644 --- a/src/main/java/com/yutou/qqbot/utlis/HttpTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/HttpTools.java @@ -24,8 +24,8 @@ public class HttpTools { return http_post(url, body, 0, null); } - public static File syncDownload(final String url, final String saveName) { - return new HttpTools().http_syncDownload(url, saveName); + public static File syncDownload(final String url, final String saveName,boolean isProxy) { + return new HttpTools().http_syncDownload(url, saveName,isProxy); } public static String https_get(String url, Map header) { @@ -158,7 +158,7 @@ public class HttpTools { } 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()); } @@ -249,7 +249,7 @@ public class HttpTools { }).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)) { return null; } @@ -259,8 +259,18 @@ public class HttpTools { if (!savePath.exists()) { 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); - 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()); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); diff --git a/src/main/java/com/yutou/qqbot/utlis/IdeaTools.java b/src/main/java/com/yutou/qqbot/utlis/IdeaTools.java index 0037af9..49eb013 100644 --- a/src/main/java/com/yutou/qqbot/utlis/IdeaTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/IdeaTools.java @@ -30,7 +30,7 @@ public class IdeaTools { } public static List getIdeaList(String url) { - File file = HttpTools.syncDownload(url, "idea.zip"); + File file = HttpTools.syncDownload(url, "idea.zip",false); List list = new ArrayList<>(); try { ZipFile zip = new ZipFile(file, Charset.forName("gbk"));