mitao
2025-01-03 9ca1188c5a951ea2f4b94876098798c8cd64784c
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
package com.sinata.system.utils;
 
import org.apache.commons.io.IOUtils;
 
import java.io.IOException;
import java.net.URL;
import java.util.Base64;
 
public class ImageToBase64 {
    /**
     * 网络图片转Base64
     *
     * @param imageUrl
     * @return
     * @throws IOException
     */
    public static String convertImageToBase64(String imageUrl) {
        try {
            return Base64.getEncoder().encodeToString(IOUtils.toByteArray(new URL(imageUrl)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
 
    public static void main(String[] args) {
        String imageUrl = "https://ja-medical-service.oss-cn-chengdu.aliyuncs.com/medical/tmp_8d18f8502484db9141a627d807dccb7c20250103015938.png";
        String base64Image = convertImageToBase64(imageUrl);
        System.out.println("Base64 Image: " + base64Image);
    }
}