From 09e6dc3fb7266620fafb5e341808a8eb36e080a1 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期六, 13 十二月 2025 22:51:52 +0800
Subject: [PATCH] feat:增加企业微信消息提醒

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java |  205 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 195 insertions(+), 10 deletions(-)

diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
index edbf719..3cc8d9f 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
@@ -1,16 +1,10 @@
 package com.ruoyi.common.utils.http;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
-import java.net.ConnectException;
-import java.net.SocketTimeoutException;
-import java.net.URL;
-import java.net.URLConnection;
+import java.io.*;
+import java.net.*;
 import java.nio.charset.StandardCharsets;
 import java.security.cert.X509Certificate;
+import java.util.Map;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
@@ -31,6 +25,45 @@
 public class HttpUtils
 {
     private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
+
+    public static final int DEFAULT_READ_TIMEOUT = 60000;
+    private static final String BOUNDARY_PREFIX = "----WebKitFormBoundary";
+    private static final String LINE_END = "\r\n";
+    private static final String TWO_HYPHENS = "--";
+
+
+    /**
+     * 鐢熸垚鍞竴鐨刡oundary
+     */
+    private static String generateBoundary() {
+        return BOUNDARY_PREFIX + System.currentTimeMillis();
+    }
+
+    /**
+     * 鏍规嵁鏂囦欢鍚嶈幏鍙朇ontent-Type
+     */
+    private static String getContentType(String fileName) {
+        if (fileName == null) {
+            return "application/octet-stream";
+        }
+
+        String extension = fileName.toLowerCase();
+        if (extension.endsWith(".jpg") || extension.endsWith(".jpeg")) {
+            return "image/jpeg";
+        } else if (extension.endsWith(".png")) {
+            return "image/png";
+        } else if (extension.endsWith(".gif")) {
+            return "image/gif";
+        } else if (extension.endsWith(".pdf")) {
+            return "application/pdf";
+        } else if (extension.endsWith(".doc")) {
+            return "application/msword";
+        } else if (extension.endsWith(".docx")) {
+            return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
+        } else {
+            return "application/octet-stream";
+        }
+    }
 
     /**
      * 鍚戞寚瀹� URL 鍙戦�丟ET鏂规硶鐨勮姹�
@@ -75,7 +108,7 @@
             URLConnection connection = realUrl.openConnection();
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
-            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
+//            connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
             connection.connect();
             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
             String line;
@@ -148,6 +181,12 @@
             log.info("sendPost - {}", url);
             URL realUrl = new URL(url);
             URLConnection conn = realUrl.openConnection();
+
+            //璁剧疆杩炴帴瓒呮椂鏃堕棿
+//            conn.setConnectTimeout(DEFAULT_READ_TIMEOUT);
+//            //璁剧疆璇诲彇瓒呮椂鏃堕棿
+//            conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
+
             conn.setRequestProperty("accept", "*/*");
             conn.setRequestProperty("connection", "Keep-Alive");
             conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
@@ -291,4 +330,150 @@
             return true;
         }
     }
+
+    /**
+     * 鍙戦�佹枃浠朵笂浼犺姹�
+     *
+     * @param url 璇锋眰URL
+     * @param params 璇锋眰鍙傛暟锛堝寘鍚枃浠舵祦锛�
+     * @return 鍝嶅簲鍐呭
+     */
+    public static String postFile(String url, Map<String, Object> params) {
+        return postFile(url, params, null);
+    }
+
+    /**
+     * 鍙戦�佹枃浠朵笂浼犺姹傦紙鏀寔鑷畾涔夋枃浠跺悕锛�
+     * 鏀寔HTTP鍜孒TTPS锛孒TTPS浼氳嚜鍔ㄥ拷鐣SL璇佷功楠岃瘉
+     *
+     * @param url 璇锋眰URL
+     * @param params 璇锋眰鍙傛暟锛堝寘鍚枃浠舵祦锛�
+     * @param fileName 鏂囦欢鍚嶏紙鍙�夛紝濡傛灉涓簄ull鍒欎娇鐢ㄥ弬鏁伴敭鍚嶏級
+     * @return 鍝嶅簲鍐呭
+     */
+    public static String postFile(String url, Map<String, Object> params, String fileName) {
+        HttpURLConnection connection = null;
+        try {
+            // 鐢熸垚鍞竴鐨刡oundary
+            String boundary = generateBoundary();
+
+            log.info("寮�濮嬫枃浠朵笂浼� - URL: {}, fileName: {}, boundary: {}", url, fileName, boundary);
+            log.info("鍙傛暟鏁伴噺: {}", params.size());
+            for (Map.Entry<String, Object> entry : params.entrySet()) {
+                log.info("鍙傛暟: {} = {}", entry.getKey(), entry.getValue() instanceof InputStream ? "InputStream" : entry.getValue());
+            }
+
+            // 鍒涘缓杩炴帴
+            URL requestUrl = new URL(url);
+            connection = (HttpURLConnection) requestUrl.openConnection();
+            
+            // 濡傛灉鏄疕TTPS璇锋眰锛岄厤缃甋SL淇′换鎵�鏈夎瘉涔�
+            if (connection instanceof HttpsURLConnection) {
+                HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
+                SSLContext sc = SSLContext.getInstance("TLS");
+                sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
+                httpsConnection.setSSLSocketFactory(sc.getSocketFactory());
+                httpsConnection.setHostnameVerifier(new TrustAnyHostnameVerifier());
+                log.info("閰嶇疆HTTPS杩炴帴锛屽拷鐣SL璇佷功楠岃瘉");
+            }
+
+            
+            // 璁剧疆璇锋眰灞炴��
+            connection.setRequestMethod("POST");
+            connection.setDoOutput(true);
+            connection.setDoInput(true);
+            connection.setUseCaches(false);
+            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
+            connection.setRequestProperty("Accept", "application/json");
+            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
+            connection.setRequestProperty("Connection", "Keep-Alive");
+            connection.setRequestProperty("Accept-Charset", "utf-8");
+            connection.setConnectTimeout(30000);
+            connection.setReadTimeout(60000);
+
+            // 鏋勫缓璇锋眰浣�
+            try (OutputStream outputStream = connection.getOutputStream();
+                 PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true)) {
+
+                // 鍐欏叆鏅�氬弬鏁�
+                for (Map.Entry<String, Object> entry : params.entrySet()) {
+                    Object value = entry.getValue();
+                    if (!(value instanceof InputStream)) {
+                        writer.append(TWO_HYPHENS).append(boundary).append(LINE_END);
+                        writer.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"").append(LINE_END);
+                        writer.append("Content-Type: text/plain; charset=UTF-8").append(LINE_END);
+                        writer.append(LINE_END);
+                        writer.append(value.toString()).append(LINE_END);
+                        writer.flush();
+                    }
+                }
+
+                // 鍐欏叆鏂囦欢鍙傛暟
+                for (Map.Entry<String, Object> entry : params.entrySet()) {
+                    Object value = entry.getValue();
+                    if (value instanceof InputStream) {
+                        // 纭畾鏂囦欢鍚�
+                        String actualFileName = fileName != null ? fileName : entry.getKey();
+
+                        writer.append(TWO_HYPHENS).append(boundary).append(LINE_END);
+                        writer.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"; filename=\"").append(actualFileName).append("\"").append(LINE_END);
+                        writer.append("Content-Type: ").append(getContentType(actualFileName)).append(LINE_END);
+                        writer.append(LINE_END);
+                        writer.flush();
+
+                        // 鍐欏叆鏂囦欢鍐呭
+                        try (InputStream inputStream = (InputStream) value) {
+                            byte[] buffer = new byte[4096];
+                            int bytesRead;
+                            while ((bytesRead = inputStream.read(buffer)) != -1) {
+                                outputStream.write(buffer, 0, bytesRead);
+                            }
+                            outputStream.flush();
+                        }
+
+                        writer.append(LINE_END);
+                        writer.flush();
+                    }
+                }
+
+                // 鍐欏叆缁撴潫鏍囪
+                writer.append(TWO_HYPHENS).append(boundary).append(TWO_HYPHENS).append(LINE_END);
+                writer.flush();
+            }
+
+            // 鑾峰彇鍝嶅簲
+            int responseCode = connection.getResponseCode();
+            log.info("鏂囦欢涓婁紶鍝嶅簲鐮侊細{}", responseCode);
+
+            // 璇诲彇鍝嶅簲鍐呭
+            StringBuilder response = new StringBuilder();
+            try (BufferedReader reader = new BufferedReader(
+                    new InputStreamReader(
+                            responseCode == HttpURLConnection.HTTP_OK ?
+                                    connection.getInputStream() :
+                                    connection.getErrorStream(),
+                            StandardCharsets.UTF_8))) {
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    response.append(line);
+                }
+            }
+
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                log.error("鏂囦欢涓婁紶澶辫触锛屽搷搴旂爜锛歿}锛屽搷搴斿唴瀹癸細{}", responseCode, response.toString());
+                throw new RuntimeException("鏂囦欢涓婁紶澶辫触锛屽搷搴旂爜: " + responseCode + "锛屽搷搴斿唴瀹�: " + response.toString());
+            }
+
+            log.info("鏂囦欢涓婁紶鎴愬姛锛屽搷搴旓細{}", response.toString());
+            return response.toString();
+
+        } catch (Exception e) {
+            log.error("鏂囦欢涓婁紶寮傚父锛歿}", e.getMessage(), e);
+            throw new RuntimeException("鏂囦欢涓婁紶澶辫触: " + e.getMessage(), e);
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.1