双重预防项目-国泰新华二开定制版
heheng
2 天以前 8e3dd5851dcedef5b21858dc1cfd5d11f1965ce6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.ruoyi.common.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.doublePrevention.entity.CJReport.PreventCJReportRiskAnaUnit;
import com.ruoyi.doublePrevention.entity.ZDReport.HandlerZDReportParam;
import com.ruoyi.doublePrevention.entity.ZDReport.PreventZDReportRiskAnaUnit;
import com.ruoyi.doublePrevention.enums.SyncEnum;
import lombok.extern.slf4j.Slf4j;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
 
@Slf4j
public class ImageToBase64 {
 
    public static String convertImageToBase64(String imageUrl) {
        try {
            // 创建URL对象
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
            // 设置请求方法和超时时间
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoInput(true);
 
            // 获取输入流
            InputStream inputStream = connection.getInputStream();
 
            // 读取图片字节数据
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
 
            // 关闭流
            inputStream.close();
            byteArrayOutputStream.close();
 
            // 将图片字节数组编码为 Base64 字符串
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
 
    public static void main(String[] args) {
        pushFourColorPicture();
    }
 
    public static String convertImageToBase64local(String imagePath) throws IOException {
        File file = new File(imagePath);
        FileInputStream imageInFile = new FileInputStream(file);
 
        // 读取文件为字节数组
        byte[] imageData = new byte[(int) file.length()];
        imageInFile.read(imageData);
 
        // 使用 Base64 编码
        return Base64.getEncoder().encodeToString(imageData);
    }
 
 
    public static void convertBase64ToImage(String base64Data, String outputPath) throws IOException {
        // 去除 Base64 数据的前缀(如果有)
        if (base64Data.startsWith("data:image")) {
            base64Data = base64Data.split(",")[1];
        }
 
        // 解码 Base64 数据为字节数组
        byte[] imageBytes = Base64.getDecoder().decode(base64Data);
 
        // 写入字节数组到图片文件
        try (FileOutputStream imageOutFile = new FileOutputStream(outputPath)) {
            imageOutFile.write(imageBytes);
        }
    }
 
    public static void pushFourColorPicture() {
        HttpURLConnection con = null;
        BufferedReader buffer = null;
        int responseCode = 200;
 
            StringBuffer unitResultBuffer = null;
            //上报数据
            try {
 
                URL url = new URL( "http://218.31.50.112:8087/api/wwyt/t_qyfxfbxx_List");
                con = (HttpURLConnection) url.openConnection();
 
                // 设置请求方式和参数
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setUseCaches(false);
                con.setRequestProperty("token", "030B6744-EECB-4732-B679-280373E7E575");
                con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
 
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("ID", "mn25kwer-8956-34hj-3sdl-dl980lcfdc8a");
                jsonObject.put("IMG", convertImageToBase64local("D:\\1751424343390.png"));
                jsonObject.put("CREATE_TIME", "20220501000000");
                jsonObject.put("UPDATE_TIME", "20250701000000");
                jsonObject.put("DELETE_MARK", "0");
                jsonObject.put("CREATE_BY", "李康");
                jsonObject.put("UPDATE_BY","李康");
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(jsonObject);
 
                // 发送请求体
                try (OutputStream os = con.getOutputStream()) {
                    os.write(JSON.toJSONString(jsonArray).getBytes(StandardCharsets.UTF_8));
                }
                responseCode = con.getResponseCode();
                //本段日志,测试成功后,可注释掉
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    //得到响应流
                    InputStream inputStream = con.getInputStream();
                    //将响应流转换成字符串
                    unitResultBuffer = new StringBuffer();
                    String line;
                    buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                    while ((line = buffer.readLine()) != null) {
                        unitResultBuffer.append(line);
                    }
                    System.out.println("上传四色图:" + unitResultBuffer);
                    
                }
 
            } catch (Exception e) {
                e.printStackTrace();
            }
 
 
    }
 
 
}