双重预防项目-国泰新华二开定制版
heheng
2025-11-19 247a4a1f10f233c89a4bd054dee3cb9b7d4a76f1
src/main/java/com/ruoyi/common/utils/ImageToBase64.java
@@ -22,10 +22,21 @@
@Slf4j
public class ImageToBase64 {
    public static String convertImageToBase64(String imageUrl) {
        try {
            log.info("【F】任务记录-处理数据imageUrl:" + imageUrl);
            String[] split = imageUrl.split(",");
            String URL1 = "";
            if (split.length > 1){
                URL1 = split[0].trim();
            }else if (split.length == 1){
                URL1 = imageUrl.trim();
            }
            // 创建URL对象
            URL url = new URL(imageUrl);
            URL url = new URL(URL1);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法和超时时间
@@ -51,7 +62,68 @@
            byteArrayOutputStream.close();
            // 将图片字节数组编码为 Base64 字符串
            // 将图片字节数组编码为 Base64 字符串
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String convertImageToBase64More(String imageUrl) {
        try {
            log.info("【F】任务记录-处理数据imageUrl:" + imageUrl);
            String[] split = imageUrl.split(",");
            String URL1 = split[0].trim();
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < split.length; i++) {
                String url1 = split[i].trim();
                // 跳过空字符串
                if (url1.isEmpty()) {
                    continue;
                }
                // 创建URL对象
                URL url = new URL(url1);
                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 字符串
                // 将图片字节数组编码为 Base64 字符串
                String base64 = Base64.getEncoder().encodeToString(imageBytes);
                if (result.length() > 0) {
                    result.append(",");
                }
                result.append(base64);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;