java对接chatgpt的图片生成接口
pupublic static void ChatGPTImgClient() throws IOException { // ChatGPT API endpoint String apiUrl = "https://api.openai.com/v1/images/generations"; // Your API key/ String apiKey = "API_KEY"; // Request payload (prompt提示词,n生成几张,图片大小) String payload = "{\n" + " \"prompt\": \"There is a plate of spaghetti on the wooden table with basil leaves and small tomatoes, forks and a mat next to it, warm light, spaghetti at the golden point, depth of field, created by Carl Wamer, high-definition image\",\n" + " \"n\": 4,\n" + " \"size\": \"1024x1024\"\n" + "}\n"; // Create connection URL url = new URL(apiUrl); HttpURLConnection connection; // Proxy configuration (if using a proxy) String proxyHost = "127.0.0.1";//代理本机地址 int proxyPort = 7890;//代理端口 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); connection = (HttpURLConnection) url.openConnection(proxy); // Set connection properties connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + apiKey); connection.setDoOutput(true); // Set connection and read timeouts (in milliseconds) int connectionTimeout = 20000; // 20 seconds int readTimeout = 20000; // 20 seconds connection.setConnectTimeout(connectionTimeout); connection.setReadTimeout(readTimeout); try { // Send request DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(payload); outputStream.flush(); outputStream.close(); // Check response code int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // Get response and process the image InputStream inputStream = connection.getInputStream(); // TODO: Process the image data as needed // Print image to browser String contentType = connection.getContentType(); String contentDisposition = connection.getHeaderField("Content-Disposition"); System.out.println("Content-Type: " + contentType); System.out.println("Content-Disposition: " + contentDisposition); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // Print image data to browser System.out.write(buffer, 0, bytesRead); } inputStream.close(); System.out.println("Image generated successfully!"); } else { // Handle error response System.out.println("Error response code: " + responseCode); } } catch (IOException e) { if (e instanceof SocketTimeoutException) { // Handle read timeout System.out.println("Read timeout occurred!"); } else { // Handle other IO exceptions System.out.println("IO exception occurred: " + e.getMessage()); } } finally { // Disconnect and close connection connection.disconnect(); } }
打印结果:
Content-Type: application/json
Content-Disposition: null
{
"created": 1684397276,
"data": [
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-BaFP8YjDVDJIerp1fmUaE2aX/user-RkoKhRkPLrVRsW0JENNXbK0K/img-bJ3wC2jucT4eeSSuiFr5yBuk.png?st=2023-05-18T07%3A07%3A56Z&se=2023-05-18T09%3A07%3A56Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-05-17T20%3A34%3A52Z&ske=2023-05-18T20%3A34%3A52Z&sks=b&skv=2021-08-06&sig=%2BfoKU5kvX3QypSJTnAUFGvNgjhJjIEUjX1d42lPVKXo%3D"
},
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-BaFP8YjDVDJIerp1fmUaE2aX/user-RkoKhRkPLrVRsW0JENNXbK0K/img-S1173RJGqjOYY4Y2q6VJ1xu9.png?st=2023-05-18T07%3A07%3A56Z&se=2023-05-18T09%3A07%3A56Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-05-17T20%3A34%3A52Z&ske=2023-05-18T20%3A34%3A52Z&sks=b&skv=2021-08-06&sig=lytdSKh/2mn7gEU5VedLCWcirmirJKI94JVlr8oY2No%3D"
},
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-BaFP8YjDVDJIerp1fmUaE2aX/user-RkoKhRkPLrVRsW0JENNXbK0K/img-o5iDCNP5SEci6hs98j4uROb1.png?st=2023-05-18T07%3A07%3A56Z&se=2023-05-18T09%3A07%3A56Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-05-17T20%3A34%3A52Z&ske=2023-05-18T20%3A34%3A52Z&sks=b&skv=2021-08-06&sig=ucy3oxEiQgNjncMJ%2Bwpw2af0JjT9aaVOXxymSds1iGU%3D"
},
{
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-BaFP8YjDVDJIerp1fmUaE2aX/user-RkoKhRkPLrVRsW0JENNXbK0K/img-OwWSvkrgFbhMiNFm7wR0vTd1.png?st=2023-05-18T07%3A07%3A56Z&se=2023-05-18T09%3A07%3A56Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-05-17T20%3A34%3A52Z&ske=2023-05-18T20%3A34%3A52Z&sks=b&skv=2021-08-06&sig=dR0lzhthIhDULsrm4rBbEyjtS10MF8W4pUk4Vi/qvx4%3D"
}
]
}
Image generated successfully!
注意:如果出现read timeout 就把超时时间设置大点;如果出现404一般就是你访问的网址不对