下面是一个简洁的说明和示范,介绍如何用Java调用Dify工作流接口,并获取工作流执行结果中的文件(假设Dify提供了HTTP API支持工作流调用和结果查询)。
一、准备工作
- 确认你有Dify工作流的API访问权限和API密钥(Token)。
- 确认Dify工作流的调用接口地址和获取输出结果的接口地址。
- 了解工作流输出文件的返回格式(通常是URL或者文件ID)。
二、Java调用示例步骤
1. 调用工作流接口,触发工作流执行
示例用HttpClient
发送POST请求启动工作流(假设接口是POST https://api.dify.ai/workflows/{workflowId}/execute
):
import java.net.http.*;
import java.net.URI;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
public class DifyWorkflowClient {
private static final String API_TOKEN = "YOUR_DIFY_API_TOKEN";
private static final String WORKFLOW_ID = "YOUR_WORKFLOW_ID";
public static void main(String[] args) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String executeUrl = "https://api.dify.ai/workflows/" + WORKFLOW_ID + "/execute";
// 构造请求体,根据API需求修改
String jsonRequestBody = "{\"input\": {\"param1\": \"value1\", \"param2\": \"value2\"}}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(executeUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_TOKEN)
.POST(BodyPublishers.ofString(jsonRequestBody))
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println("触发工作流响应:" + response.body());
// 通常返回会包含执行ID或任务ID,用于后续查询
// 这里假设响应JSON中包含executionId字段
String executionId = extractExecutionId(response.body());
// 后续查询工作流结果
getWorkflowResult(client, executionId);
}
private static String extractExecutionId(String responseBody) {
// 简单示例,实际用JSON库解析
// 假设responseBody = {"executionId":"abc123", ...}
int start = responseBody.indexOf("\"executionId\":\"") + 14;
int end = responseBody.indexOf("\"", start);
if (start > 13 && end > start) {
return responseBody.substring(start, end);
}
return null;
}
private static void getWorkflowResult(HttpClient client, String executionId) throws Exception {
if (executionId == null) {
System.err.println("无法获取executionId,无法查询结果");
return;
}
String resultUrl = "https://api.dify.ai/workflows/executions/" + executionId + "/result";
// 这里可能需要等待几秒,直到工作流执行完成,实际项目中可做轮询
Thread.sleep(5000);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(resultUrl))
.header("Authorization", "Bearer " + API_TOKEN)
.GET()
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println("工作流结果:" + response.body());
// 假设返回的JSON包含文件URL或文件列表
String fileUrl = extractFileUrl(response.body());
if (fileUrl != null) {
downloadFile(client, fileUrl, "output_file");
}
}
private static String extractFileUrl(String responseBody) {
// 简单示例,根据实际字段解析
// 假设responseBody = {"outputs": {"file": "https://files.dify.ai/xxx.pdf"}}
int start = responseBody.indexOf("\"file\":\"") + 8;
int end = responseBody.indexOf("\"", start);
if (start > 7 && end > start) {
return responseBody.substring(start, end);
}
return null;
}
private static void downloadFile(HttpClient client, String fileUrl, String filename) throws Exception {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(fileUrl))
.GET()
.build();
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
java.nio.file.Files.write(java.nio.file.Paths.get(filename), response.body());
System.out.println("文件已下载:" + filename);
}
}
三、说明
- 代码中
extractExecutionId
和extractFileUrl
是演示简单字符串提取,实际建议使用Jackson
或Gson
解析JSON。 - 工作流调用后需要轮询或延时等待执行完成,再请求结果接口。
- 返回结果中包含的文件一般是外部下载地址,直接用Java HTTP请求下载二进制流即可保存。
- 具体API路径和字段请参考Dify官方API文档。
如果你能提供更具体的Dify API文档或接口细节,我可以帮你写更精准的调用示例代码。
发表回复