更新到1.0.14

整合网络请求到HttpTools类
server酱请求改成post
新增!version操作,获取本地和服务端版本号
This commit is contained in:
yutou 2020-12-25 10:51:11 +08:00
parent 7300b5e3b7
commit f53bc2d48d
8 changed files with 159 additions and 58 deletions

View File

@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ToolsApplication {
public static final String version="1.0.13";
public static final String version="1.0.14";
public static void main(String[] args) {
System.out.println("当前版本号:"+version);

View File

@ -1,17 +1,9 @@
package com.yutou.tools.bangumi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.utils.BiliBiliLiveTools;
import com.yutou.tools.utils.Tools;
import com.yutou.tools.utils.HttpTools;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class AnimationData {
@ -32,7 +24,7 @@ public class AnimationData {
title = title.substring(1, title.length() - 1);
System.out.println(String.format(animUrl, "" + index, title,type,team));
System.out.println(rss2jsonUrl + URLEncoder.encode(String.format(animUrl, "" + index, title,type,team), "UTF-8") +String.format( "&api_key=%s&count=500",rss2jsonApi_2));
String js=Tools.get(rss2jsonUrl+ URLEncoder.encode(String.format(animUrl,""+index,title,type,team),"UTF-8")+String.format( "&api_key=%s&count=500",rss2jsonApi_2));
String js= HttpTools.get(rss2jsonUrl+ URLEncoder.encode(String.format(animUrl,""+index,title,type,team),"UTF-8")+String.format( "&api_key=%s&count=500",rss2jsonApi_2));
JSONObject json=JSONObject.parseObject(js);
if(json.getString("status").equals("ok")){
return json.getJSONArray("items");
@ -61,7 +53,7 @@ public class AnimationData {
}
public static void initData(){
String url="https://share.dmhy.org/topics/advanced-search?team_id=0&sort_id=0&orderby=";
String data=Tools.get(url);
String data=HttpTools.get(url);
System.out.println(data);
}
public static void main(String[] args) {

View File

@ -1,6 +1,7 @@
package com.yutou.tools.home.nas;
import com.yutou.tools.nas.UpdateIp;
import com.yutou.tools.utils.HttpTools;
import com.yutou.tools.utils.RedisTools;
import com.yutou.tools.utils.Tools;
import org.springframework.stereotype.Controller;
@ -8,7 +9,6 @@ import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@ -19,7 +19,7 @@ public class ToolsController {
public String open_pc(HttpServletRequest request, String type) {
if (StringUtils.isEmpty(type)) {
if (Tools.checkWebLogin(request) == 1) {
Tools.get("http://" + UpdateIp.nas_ip + ":8000/tools/openpc.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor&type=nas");
HttpTools.get("http://" + UpdateIp.nas_ip + ":8000/tools/openpc.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor&type=nas");
}
} else {
if (type.equals("nas")) {

View File

@ -0,0 +1,104 @@
package com.yutou.tools.utils;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.utils.Interfaces.NetworkInterface;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Set;
public class HttpTools {
public static String get(String url){
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", getExtUa());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder();
String tmp;
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
}
reader.close();
connection.disconnect();
return str.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void post(final String url, final byte[] body, final NetworkInterface networkInterface) {
new Thread(new Runnable() {
@Override
public void run() {
String tmp;
StringBuilder str = new StringBuilder();
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(10 * 1000);
//connection.addRequestProperty("Connection", "keep-alive");
//connection.addRequestProperty("User-Agent", getExtUa());
//connection.addRequestProperty("content-type", "application/json");
connection.addRequestProperty("charset", "UTF-8");
OutputStream outputStream = connection.getOutputStream();
outputStream.write(body);
outputStream.flush();
outputStream.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
}
final String finalStr = str.toString();
// Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")");
if (networkInterface != null) {
try {
networkInterface.httpGetData(str.toString(), connection.getResponseCode());
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
private static String getExtUa() {
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36";
}
public String toUrlParams(JSONObject json) {
StringBuilder string = new StringBuilder();
Set<String> keys = json.keySet();
for (String key : keys) {
try {
string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
try {
string.append("&").append(URLEncoder.encode(key, "UTF-8")).append("=");
// string += "&" + key + "=";
} catch (Exception e1) {
string.append("&").append(key).append("=");
}
}
}
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString();
}
}

View File

@ -0,0 +1,16 @@
package com.yutou.tools.utils.Interfaces;
public interface NetworkInterface {
/**
* 请求成功
* @param data 请求参数
* @param state http状态
*/
void httpGetData(Object data, int state);
/**
* 请求异常
* @param e 异常
*/
void httpError(Exception e);
}

View File

@ -1,5 +1,6 @@
package com.yutou.tools.utils;
import com.yutou.tools.ToolsApplication;
import kotlin.coroutines.CoroutineContext;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactoryJvm;
@ -127,6 +128,9 @@ public class QQBotManager {
case "!ip":
RedisTools.Consumer.bot("getip");
break;
case "!version":
sendVersion();
break;
default:
if (msg.startsWith("!cmd")) {
RedisTools.Consumer.system("cmd", msg.replace("!cmd", ""));
@ -138,6 +142,14 @@ public class QQBotManager {
return ListeningStatus.LISTENING;
}
private void sendVersion() {
String localVersion= ToolsApplication.version;
String serverVersion=HttpTools.get("http://tools.yutou233.cn:8000/public/version.do?token=zIrsh9TUZP2lfRW753PannG49E7VJvor");
String msg="本地版本:"+localVersion+"\n"+"服务器版本:"+serverVersion;
QQBotManager.getInstance().sendMessage(msg);
Tools.sendServer("服务版本查询",msg);
}
@EventHandler
public void onMessage(@NotNull MessageEvent event) throws Exception {
// sendMessage("个人:"+event.getSender().getNick()+" 发来消息:"+event.getMessage().contentToString());

View File

@ -13,7 +13,6 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import javax.accessibility.AccessibleAction;
public class RedisTools {
private static boolean isNotInstallRedis = false;
@ -268,7 +267,7 @@ public class RedisTools {
public static void bot(String value) {
switch (value) {
case "getip":
JSONObject json = JSONObject.parseObject(Tools.get("https://api.asilu.com/ip/"));
JSONObject json = JSONObject.parseObject(HttpTools.get("https://api.asilu.com/ip/"));
String ip = json.getString("ip");
QQBotManager.getInstance().sendMessage("服务器IP:\n" + ip);
break;

View File

@ -19,6 +19,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.Date;
@ -89,13 +90,9 @@ public class Tools {
public static void sendServer(String title, String msg) {
try {
System.out.println("title=" + title + " msg=" + msg);
HttpURLConnection connection = (HttpURLConnection) new URL("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send?title="
+ URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
int i = inputStream.read();
inputStream.close();
connection.disconnect();
HttpTools.post("https://sctapi.ftqq.com/SCT2619Tpqu93OYtQCrK4LOZYEfr2irm.send",
("title="+URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(msg, "UTF-8")).getBytes(StandardCharsets.UTF_8),
null);
if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("nas")) {
String img = null;
msg = msg.replace("<br/>", "\n");
@ -148,12 +145,7 @@ public class Tools {
+ "&token=zIrsh9TUZP2lfRW753PannG49E7VJvor";
}
System.out.println(url);
connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
inputStream = connection.getInputStream();
i = inputStream.read();
inputStream.close();
connection.disconnect();
HttpTools.get(url);
}
} catch (Exception e) {
e.printStackTrace();
@ -241,25 +233,6 @@ public class Tools {
return 0;
}
public static String get(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder str = new StringBuilder();
String tmp;
while ((tmp = reader.readLine()) != null) {
str.append(tmp);
}
reader.close();
connection.disconnect();
return str.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 保存上传的文件
*
@ -293,7 +266,7 @@ public class Tools {
public static void download(String url, DownloadInterface downloadInterface) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
connection.disconnect();
new File("tmp").mkdirs();
File file = new File("tmp" + File.separator + url.trim().split("/")[url.trim().split("/").length - 1]);
@ -337,6 +310,7 @@ public class Tools {
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
}
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
@ -358,6 +332,7 @@ public class Tools {
}
return bytesToHexString(digest.digest());
}
private static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
@ -373,29 +348,32 @@ public class Tools {
}
return stringBuilder.toString();
}
public static String base64ToString(String base){
base=base.replace(" ","+");
public static String base64ToString(String base) {
base = base.replace(" ", "+");
try {
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
}catch (Exception e){
base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes()));
} catch (Exception e) {
try {
base=URLDecoder.decode(base,"UTF-8");
base=base.replace(" ","+");
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
base = URLDecoder.decode(base, "UTF-8");
base = base.replace(" ", "+");
base = new String(Base64.getDecoder().decode(base.replace("\r\n", "").getBytes()));
} catch (Exception e1) {
e1.printStackTrace();
}
}
return base;
}
/**
* 异常输出
*
* @param e 异常
* @return
*/
public static String getExceptionString(Exception e) {
StringWriter writer=new StringWriter();
PrintWriter printWriter=new PrintWriter(writer);
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.close();
return writer.toString();