java对接chatgpt的图片生成接口
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 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一般就是你访问的网址不对
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)