package com.fs.app.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class imgTest {

    private static final String ARK_API = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
    private static final String IMAGE_URL = "https://cos.his.cdwjyyh.com/fs/20250526/a17565cb7ba74aefb27e27ac91f802f2.jpg";
    public  void main(String[] args) {
        try {

            String apiKey = "208d3549-8dc9-4ef6-b3fa-5aa358f1ab20";
            String requestBody = String.format(
                    "{" +
                            "\"model\": \"doubao-1-5-thinking-vision-pro-250428\"," +
                            "\"messages\": [{" +
                            "\"role\": \"user\"," +
                            "\"content\": [" +
                            "{" +
                            "\"type\": \"image_url\"," +
                            "\"image_url\": {\"url\": \""+IMAGE_URL+"\"}" +
                            "}," +
                            "{" +
                            "\"type\": \"text\"," +
                            "\"text\": \"解析一下这张图片\"" +
                            "}" +
                            "]" +
                            "}]" +
                            "}"
            );

            // 发送请求
            String response = sendHttpRequest(apiKey, requestBody);
            System.out.println("API响应: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    private static String sendHttpRequest(String apiKey, String requestBody) throws IOException {
        HttpURLConnection connection = null;
        try {
            // 配置连接
            URL url = new URL(ARK_API);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", "Bearer " + apiKey);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setDoOutput(true);
            connection.setConnectTimeout(10000); // 10秒连接超时
            connection.setReadTimeout(30000);   // 30秒读取超时

            // 发送请求体
            try (OutputStream os = connection.getOutputStream()) {
                os.write(requestBody.getBytes(StandardCharsets.UTF_8));
            }

            // 处理响应
            int status = connection.getResponseCode();
            if (status == HttpURLConnection.HTTP_OK) {
                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        response.append(line);
                    }
                    return response.toString();
                }
            } else {
                // 读取错误流
                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
                    StringBuilder errorResponse = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        errorResponse.append(line);
                    }
                    throw new IOException("HTTP错误 " + status + ": " + errorResponse.toString());
                }
            }
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}
