路泰机电科技体检——数据平台后端
zhaowenxuan
2025-06-12 493d39e60bae93a724448af7dd53e2ff10b927c8
src/main/java/com/example/utils/HttpClientUtils.java
@@ -97,7 +97,54 @@
    }
    public static String sendPost(String httpUrl, Map<String, Object> maps) {
    public static String sendPost(String httpUrl, Map<String, Object> maps,Map<String ,Object> headers) {
        try {
            // 1. 创建 URL 对象
            URL url = new URL(httpUrl);
            // 2. 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 3. 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 4. 设置 Content-Type 头部字段
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            if (headers != null && !headers.isEmpty()){
                for (Map.Entry<String, Object> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
                }
            }
            // 6. 向服务器发送数据
            String requestBody = JSONUtil.toJsonStr(maps);
            log.info(httpUrl+"入参:   "+requestBody);
            log.info("请求头 ->{}",JSONUtil.toJsonStr(headers));
//            String requestBody1 = maps.toString();
            byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
            connection.setDoOutput(true);
            try (OutputStream outputStream = connection.getOutputStream()) {
                outputStream.write(postData);
            }
            // 8. 获取响应数据
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                log.info("=====================================================");
                log.info(httpUrl+"出参");
                log.info(response.toString());
                connection.disconnect();
                return response.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String sendPost(String httpUrl, String json) {
        try {
            // 1. 创建 URL 对象
@@ -109,10 +156,10 @@
            // 4. 设置 Content-Type 头部字段
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            // 6. 向服务器发送数据
            String requestBody = JSONUtil.toJsonStr(maps);
            String requestBody = json;
            log.info(httpUrl+"入参:   "+requestBody);
//            String requestBody1 = maps.toString();
            byte[] postData = requestBody.getBytes("UTF-8");
            byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
            connection.setDoOutput(true);
            try (OutputStream outputStream = connection.getOutputStream()) {
                outputStream.write(postData);
@@ -185,28 +232,18 @@
        OutputStreamWriter writer = null;
        BufferedReader reader = null;
        StringBuilder response = new StringBuilder();
        try {
            // 创建 URL 对象
            log.info("请求地址 ->{}",httpUrl);
            URL url = new URL(httpUrl);
            // 打开连接
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头 Content-Type 为 application/x-www-form-urlencoded
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 如果需要 Authorization Header
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            // 设置允许输出
            connection.setDoOutput(true);
            // 构建表单数据
            StringBuilder postData = new StringBuilder();
            log.info("入参 ->{}", JSONUtil.toJsonStr(maps));
            for (Map.Entry<String, Object> entry : maps.entrySet()) {
                if (postData.length() > 0) {
                    postData.append("&");
@@ -215,23 +252,20 @@
                        .append("=")
                        .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
            }
            // 获取输出流并写入表单数据
            log.info("参数拼接 ->{}",postData);
            writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(postData.toString());
            writer.flush();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            log.info("返回 ->{}",response);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭流
            try {
                if (writer != null) writer.close();
                if (reader != null) reader.close();
@@ -250,60 +284,39 @@
        DataOutputStream outStream = null;
        BufferedReader reader = null;
        StringBuilder response = new StringBuilder();
        // 边界字符串
        String boundary = "----WebKitFormBoundary" + UUID.randomUUID().toString().replaceAll("-", "");
        String CRLF = "\r\n"; // 换行符
        String CRLF = "\r\n";
        try {
            // 创建 URL 对象
            log.info("请求地址 ->{}",httpUrl);
            URL url = new URL(httpUrl);
            // 打开连接
            connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头 Content-Type 为 multipart/form-data
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            // 如果需要 Authorization Header
            if (authorization != null && !authorization.isEmpty()) {
                connection.setRequestProperty("Authorization", authorization);
            }
            // 设置允许输出
            connection.setDoOutput(true);
            // 获取输出流
            log.info("入参 ->{}",maps);
            outStream = new DataOutputStream(connection.getOutputStream());
            // 遍历传入的表单数据,并按照 multipart/form-data 格式写入
            for (Map.Entry<String, Object> entry : maps.entrySet()) {
                outStream.writeBytes("--" + boundary + CRLF);
                outStream.writeBytes("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + CRLF);
                outStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + CRLF);
                outStream.writeBytes(CRLF);  // 这里是分隔符,空一行
                outStream.writeBytes(CRLF);
                outStream.writeBytes(entry.getValue().toString() + CRLF);
            }
            // 结束 multipart/form-data 发送数据部分
            outStream.writeBytes("--" + boundary + "--" + CRLF);
            // 刷新输出流
            outStream.flush();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            log.info("返回 ->{}",response);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 关闭流
            try {
                if (outStream != null) outStream.close();
                if (reader != null) reader.close();