main
HuangHai 2 months ago
parent dcedea5d1a
commit 28c58fc9f9

@ -0,0 +1,104 @@
package com.dsideal.aiSupport.Util.Midjourney.Kit;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.dsideal.aiSupport.Plugin.YamlProp;
import com.dsideal.aiSupport.Util.KeLing.Kit.KlErrorCode;
import com.jfinal.kit.Prop;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
public class MjCommon {
private static final Logger log = LoggerFactory.getLogger(MjCommon.class);
// 获取项目根目录路径
protected static String projectRoot = System.getProperty("user.dir").replace("\\","/")+"/dsAiSupport";
// 拼接相对路径
protected static String basePath = projectRoot + "/src/main/java/com/dsideal/aiSupport/Util/Midjourney/Example/";
protected static String ak; // 填写access key
public static Prop PropKit; // 配置文件工具
protected static final String BASE_URL = "https://goapi.gptnb.ai";
static {
//加载配置文件
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
PropKit = new YamlProp(configFile);
ak = PropKit.get("GPTNB.sk");
}
/**
* URL
*
* @param fileUrl URL
* @param saveFilePath
* @throws Exception
*/
public static void downloadFile(String fileUrl, String saveFilePath) throws Exception {
try {
// 使用Hutool下载文件
long fileSize = HttpUtil.downloadFile(fileUrl, FileUtil.file(saveFilePath));
log.info("文件下载成功,保存路径: {}, 文件大小: {}字节", saveFilePath, fileSize);
} catch (Exception e) {
log.error("文件下载失败: {}", e.getMessage(), e);
throw new Exception("文件下载失败: " + e.getMessage(), e);
}
}
/**
*
*
* @param taskId ID
* @return
* @throws Exception
*/
@SneakyThrows
public static JSONObject queryTaskStatus(String taskId) {
// 创建OkHttpClient
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 创建请求
Request request = new Request.Builder()
.url(BASE_URL + "/mj/task/" + taskId + "/fetch")
.method("GET", null)
.addHeader("Authorization", "Bearer " + ak)
.build();
// 发送请求并获取响应
log.info("查询Midjourney任务状态: {}", taskId);
Response response = client.newCall(request).execute();
// 检查响应状态
if (!response.isSuccessful()) {
String errorMsg = "Midjourney API请求失败状态码: " + response.code();
log.error(errorMsg);
throw new Exception(errorMsg);
}
// 解析响应
String responseBody = response.body().string();
log.info("查询Midjourney任务状态响应: {}", responseBody);
return JSON.parseObject(responseBody);
}
}

@ -1,5 +1,6 @@
package com.dsideal.aiSupport.Util.Midjourney;
import com.dsideal.aiSupport.Util.Midjourney.Kit.MjCommon;
import lombok.SneakyThrows;
import okhttp3.*;
import com.alibaba.fastjson.JSON;
@ -14,10 +15,8 @@ import java.util.concurrent.TimeUnit;
* Midjourney API
* Midjourney imagine
*/
public class Midjourney {
private static final Logger log = LoggerFactory.getLogger(Midjourney.class);
private static final String BASE_URL = "https://goapi.gptnb.ai";
private static final String API_KEY = "sk-amQHwiEzPIZIB2KuF5A10dC23a0e4b02B48a7a2b6aFa0662";
public class Txt2Img extends MjCommon {
private static final Logger log = LoggerFactory.getLogger(Txt2Img.class);
/**
* imagine
@ -73,7 +72,7 @@ public class Midjourney {
.url(BASE_URL + "/mj/submit/imagine")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + API_KEY)
.addHeader("Authorization", "Bearer " + ak)
.build();
// 发送请求并获取响应
@ -109,46 +108,6 @@ public class Midjourney {
return taskId;
}
/**
*
*
* @param taskId ID
* @return
* @throws Exception
*/
@SneakyThrows
public static JSONObject queryTaskStatus(String taskId) {
// 创建OkHttpClient
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
// 创建请求
Request request = new Request.Builder()
.url(BASE_URL + "/mj/task/" + taskId + "/fetch")
.method("GET", null)
.addHeader("Authorization", "Bearer " + API_KEY)
.build();
// 发送请求并获取响应
log.info("查询Midjourney任务状态: {}", taskId);
Response response = client.newCall(request).execute();
// 检查响应状态
if (!response.isSuccessful()) {
String errorMsg = "Midjourney API请求失败状态码: " + response.code();
log.error(errorMsg);
throw new Exception(errorMsg);
}
// 解析响应
String responseBody = response.body().string();
log.info("查询Midjourney任务状态响应: {}", responseBody);
return JSON.parseObject(responseBody);
}
@SneakyThrows
public static void main(String[] args) {
// 提示词
@ -161,6 +120,7 @@ public class Midjourney {
int maxRetries = 1000;
int retryCount = 0;
int retryInterval = 5000; // 5秒
String imageUrl = null;
while (retryCount < maxRetries) {
JSONObject result = queryTaskStatus(taskId);
@ -177,7 +137,7 @@ public class Midjourney {
if (status != null && !status.isEmpty()) {
if ("SUCCESS".equals(status)) {
// 任务成功获取图片URL
String imageUrl = result.getString("imageUrl");
imageUrl = result.getString("imageUrl");
log.info("生成的图片URL: {}", imageUrl);
break;
} else if ("FAILED".equals(status)) {
@ -190,7 +150,7 @@ public class Midjourney {
String description = result.getString("description");
if (description != null && description.contains("成功") && !"0%".equals(progress)) {
// 如果描述包含"成功"且进度不为0%,可能任务已完成
String imageUrl = result.getString("imageUrl");
imageUrl = result.getString("imageUrl");
if (imageUrl != null && !imageUrl.isEmpty()) {
log.info("生成的图片URL: {}", imageUrl);
break;
@ -207,5 +167,15 @@ public class Midjourney {
if (retryCount >= maxRetries) {
log.error("查询任务状态超时,已达到最大重试次数: {}", maxRetries);
}
// 如果获取到了图片URL则下载保存
if (imageUrl != null && !imageUrl.isEmpty()) {
// 生成文件名使用时间戳和任务ID
String fileName = "mj_" + System.currentTimeMillis() + "_" + taskId + ".png";
// 完整保存路径
String savePath = basePath + "/" + fileName;
// 下载图片
downloadFile(imageUrl, savePath);
}
}
}
Loading…
Cancel
Save