Bläddra i källkod

后勤资源 代码优化

yangbq 2 månader sedan
förälder
incheckning
82b9fb4499

+ 53 - 49
project-common/src/main/java/com/project/common/utils/http/HttpUtils.java

@@ -9,10 +9,7 @@ import org.slf4j.LoggerFactory;
 
 import javax.net.ssl.*;
 import java.io.*;
-import java.net.ConnectException;
-import java.net.SocketTimeoutException;
-import java.net.URL;
-import java.net.URLConnection;
+import java.net.*;
 import java.nio.charset.StandardCharsets;
 import java.security.cert.X509Certificate;
 import java.util.Map;
@@ -220,60 +217,67 @@ public class HttpUtils {
     /**
      * 向指定 URL 发送POST方法的请求
      *
-     * @param url   发送请求的 URL
+     * @param apiUrl   发送请求的 URL
      * @param map 请求参数,请求参数应该是 requestBody 的形式。
      * @return 所代表远程资源的响应结果
      */
-    public static String sendJsonPost(String url, Map<String, Object> map) {
-        String param = JSON.toJSONString(map);
-        PrintWriter out = null;
-        BufferedReader in = null;
-        StringBuilder result = new StringBuilder();
+    public static String sendJsonPost(String apiUrl, Map<String, Object> map) {
+        String responseBody = "";
         try {
-            log.info("sendPost - {}", url);
-            URL realUrl = new URL(url);
-            URLConnection conn = realUrl.openConnection();
-            conn.setRequestProperty("accept", "*/*");
-            conn.setRequestProperty("connection", "Keep-Alive");
-            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
-            conn.setRequestProperty("Accept-Charset", "utf-8");
-            conn.setRequestProperty("content-type", "application/json");
-            conn.setDoOutput(true);
-            conn.setDoInput(true);
-            out = new PrintWriter(conn.getOutputStream());
-            out.print(param);
-            out.flush();
-            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
-            String line;
-            while ((line = in.readLine()) != null) {
-                result.append(line);
+            // 设置请求的 URL 地址
+            URL url = new URL(apiUrl);
+            // 打开连接
+            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+
+            // 设置请求方法为 POST
+            connection.setRequestMethod("POST");
+            // 设置请求头,指定 Content-Type 为 application/json
+            connection.setRequestProperty("Content-Type", "application/json; utf-8");
+            connection.setRequestProperty("Accept", "application/json");
+
+            // 允许输出和输入
+            connection.setDoOutput(true);
+            connection.setDoInput(true);
+
+            String jsonInputString = JSON.toJSONString(map);
+
+            // 将 JSON 数据写入请求体
+            try (OutputStream os = connection.getOutputStream();
+                 OutputStreamWriter writer = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
+                writer.write(jsonInputString);
             }
-            log.info("recv - {}", result);
-        } catch (ConnectException e) {
-            throw new ServiceException(e.getMessage());
-            //log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
-        } catch (SocketTimeoutException e) {
-            log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
-            throw new ServiceException(e.getMessage());
-        } catch (IOException e) {
-            log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
-            throw new ServiceException(e.getMessage());
+
+            // 获取响应码
+            int responseCode = connection.getResponseCode();
+
+            // 处理响应数据(这里可以读取响应流)
+            if (responseCode == HttpURLConnection.HTTP_OK) {
+                responseBody = readResponseStream(connection.getInputStream());
+            }
+
+            // 关闭连接
+            connection.disconnect();
+
         } catch (Exception e) {
-            log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
             throw new ServiceException(e.getMessage());
-        } finally {
-            try {
-                if (out != null) {
-                    out.close();
-                }
-                if (in != null) {
-                    in.close();
-                }
-            } catch (IOException ex) {
-                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
+        }
+        return responseBody;
+    }
+
+    /**
+     * 从输入流中读取响应内容
+     */
+    private static String readResponseStream(InputStream inputStream) {
+        StringBuilder response = new StringBuilder();
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                response.append(line);
             }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
-        return result.toString();
+        return response.toString();
     }
 
     /**

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 3 - 6
project-zcustom/src/main/java/com/project/zcustom/tools/LogisticsService.java


Vissa filer visades inte eftersom för många filer har ändrats