package com.ruoyi.gysl.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class ImgBase64Extractor {

    public static void main(String[] args) throws IOException {
        String  imgUrl ="http://39.101.188.84:7071/profile/upload/2025/03/24/abc_20250324154129A006.jpg";

        URL url = new URL(imgUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        // 添加请求头
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        // 连接并获取响应
        connection.connect();

        // 检查响应码
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 连接成功，获取输入流
            InputStream inputStream = connection.getInputStream();

            // 读取输入流
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            inputStream.close();
            byte[] imageBytes = outputStream.toByteArray();
            System.out.println(imageBytes);
        } else {
            System.out.println("Error: Server returned HTTP response code: " + responseCode);

        }
    }

}
