|
|
|
@ -80,7 +80,7 @@ public class JiMengUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void doRequest(String method, Map<String, String> queryList, byte[] body, String saveImgPath) throws Exception {
|
|
|
|
|
private static String doRequest(String method, Map<String, String> queryList, byte[] body) throws Exception {
|
|
|
|
|
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
if (body == null) {
|
|
|
|
@ -149,21 +149,7 @@ public class JiMengUtil {
|
|
|
|
|
String responseBody = new String(ByteStreams.toByteArray(is));
|
|
|
|
|
is.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject jo = JSON.parseObject(responseBody);
|
|
|
|
|
String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString();
|
|
|
|
|
|
|
|
|
|
// 对 Base64 字符串进行解码
|
|
|
|
|
Base64.Decoder decoder = Base64.getDecoder();
|
|
|
|
|
byte[] bytes = decoder.decode(imgBase64);
|
|
|
|
|
|
|
|
|
|
try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
os.flush(); // 刷新缓冲区,确保数据写入文件
|
|
|
|
|
System.out.println("文件保存成功!");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return responseBody;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String signStringEncoder(String source) {
|
|
|
|
@ -222,11 +208,37 @@ public class JiMengUtil {
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static void generateImage(String prompt) throws Exception {
|
|
|
|
|
String saveImgPath = "d:/Temp/1.jpg";
|
|
|
|
|
public static void generateImage(String prompt, String saveImgPath) throws Exception {
|
|
|
|
|
JSONObject req = new JSONObject();
|
|
|
|
|
req.put("req_key", "jimeng_high_aes_general_v21_L");
|
|
|
|
|
req.put("prompt", prompt);
|
|
|
|
|
doRequest("POST", new HashMap<>(), req.toString().getBytes(), saveImgPath);
|
|
|
|
|
String responseBody = doRequest("POST", new HashMap<>(), req.toString().getBytes());
|
|
|
|
|
JSONObject jo = JSON.parseObject(responseBody);
|
|
|
|
|
String imgBase64 = jo.getJSONObject("data").getJSONArray("binary_data_base64").getFirst().toString();
|
|
|
|
|
// 对 Base64 字符串进行解码
|
|
|
|
|
Base64.Decoder decoder = Base64.getDecoder();
|
|
|
|
|
byte[] bytes = decoder.decode(imgBase64);
|
|
|
|
|
|
|
|
|
|
try (OutputStream os = new FileOutputStream(saveImgPath)) { // 使用-with try-resources 自动关闭资源
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
os.flush(); // 刷新缓冲区,确保数据写入文件
|
|
|
|
|
System.out.println("文件保存成功!");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成视频
|
|
|
|
|
*
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public static void generateVideo(String prompt) throws Exception {
|
|
|
|
|
JSONObject req = new JSONObject();
|
|
|
|
|
req.put("req_key", "jimeng_vgfm_t2v_l20");
|
|
|
|
|
req.put("prompt", prompt);
|
|
|
|
|
doRequest("POST", new HashMap<>(), req.toString().getBytes());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|