添加能耗采集定时任务

This commit is contained in:
chy
2026-03-13 09:58:32 +08:00
parent fde5cfe3b2
commit 21fe5196be
6 changed files with 358 additions and 4 deletions

View File

@@ -7,6 +7,18 @@
<artifactId>lidee-tool</artifactId>
<version>${lidee.version}</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>
<artifactId>tool-common</artifactId>
<packaging>jar</packaging>

View File

@@ -10,8 +10,12 @@ import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
@@ -21,6 +25,9 @@ import java.util.Map;
*/
public class HttpUtils {
private static final int CONNECT_TIMEOUT = 5000; // 连接超时(毫秒)
private static final int READ_TIMEOUT = 10000; // 读取超时(毫秒)
@SuppressWarnings("unchecked")
public static String replaceUrlQuery(String url, String key, String value) {
UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset());
@@ -122,5 +129,203 @@ public class HttpUtils {
return null;
}
/**
* 发送 HTTP GET 请求
*
* @param urlStr 请求地址
* @param headers 请求头(可为 null
* @return 响应字符串
*/
public static String doGet(String urlStr, Map<String, String> headers) throws IOException {
HttpURLConnection conn = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setDoInput(true);
// 设置请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} else {
throw new IOException("GET request failed, response code: " + responseCode);
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
conn.disconnect();
}
}
}
/**
* 发送 HTTP POST 请求application/x-www-form-urlencoded
*
* @param urlStr 请求地址
* @param params 表单参数(可为 null
* @param headers 请求头(可为 null
* @return 响应字符串
*/
public static String doPost(String urlStr, Map<String, String> params, Map<String, String> headers)
throws IOException {
HttpURLConnection conn = null;
BufferedWriter writer = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
// 写入表单参数
if (params != null && !params.isEmpty()) {
StringBuilder paramStr = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (paramStr.length() > 0) {
paramStr.append("&");
}
paramStr.append(entry.getKey())
.append("=")
.append(java.net.URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
}
writer = new BufferedWriter(
new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
writer.write(paramStr.toString());
writer.flush();
}
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} else {
throw new IOException("POST request failed, response code: " + responseCode);
}
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignored) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
conn.disconnect();
}
}
}
/**
* 发送 JSON 格式的 POST 请求
*
* @param urlStr 请求地址
* @param json 请求体 JSON 字符串
* @param headers 请求头(可为 null
* @return 响应字符串
*/
public static String doPostJson(String urlStr, String json, Map<String, String> headers)
throws IOException {
HttpURLConnection conn = null;
BufferedWriter writer = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置默认 Content-Type
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// 设置自定义请求头
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
// 写入 JSON 请求体
writer = new BufferedWriter(
new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
writer.write(json);
writer.flush();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} else {
throw new IOException("POST JSON request failed, response code: " + responseCode);
}
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignored) {
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
if (conn != null) {
conn.disconnect();
}
}
}
}