| | |
| | | import com.agentsflex.core.message.AiMessage; |
| | | import com.agentsflex.core.prompt.HistoriesPrompt; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.gson.*; |
| | | import com.mybatisflex.core.query.QueryWrapper; |
| | | import okhttp3.*; |
| | | import okio.BufferedSource; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | | import tech.aiflowy.ai.entity.AiBotMessage; |
| | | import tech.aiflowy.ai.service.AiBotMessageService; |
| | |
| | | JsonElement dataElement = jsonObject.get("data"); |
| | | if (dataElement != null && !dataElement.isJsonNull()) { |
| | | JsonObject dataObject = dataElement.getAsJsonObject(); |
| | | if (dataObject != null && dataObject.has("node_type")) { |
| | | continue; |
| | | } |
| | | if (dataObject != null && dataObject.has("title")) { |
| | | JsonElement titleElement = dataObject.get("title"); |
| | | if (titleElement != null && !titleElement.isJsonNull()) { |
| | |
| | | return future; |
| | | } |
| | | |
| | | public String fileUpload(String userId, String filePath){ |
| | | // 要上传的文件路径,替换为实际的文件路径 |
| | | // String filePath = "C:\\Users\\admin\\Desktop\\国务院政策文件库.xlsx"; |
| | | // 用户标识,替换为实际的用户标识,要和发送消息接口的 user 保持一致 |
| | | public String fileUpload(String userId, MultipartFile file) { |
| | | // 用户标识,要和发送消息接口的 user 保持一致 |
| | | String user = userId; |
| | | |
| | | File file = new File(filePath); |
| | | if (!file.exists()) { |
| | | System.out.println("文件不存在:" + filePath); |
| | | return user; |
| | | // 判断文件是否为空 |
| | | if (file.isEmpty()) { |
| | | System.out.println("上传文件为空"); |
| | | return "上传文件为空"; |
| | | } |
| | | |
| | | OkHttpClient client = new OkHttpClient(); |
| | | |
| | | // 构建 multipart/form-data 请求体 |
| | | RequestBody requestBody = new MultipartBody.Builder() |
| | | .setType(MultipartBody.FORM) |
| | | .addFormDataPart("file", file.getName(), |
| | | RequestBody.create(MediaType.parse("application/octet-stream"), file)) |
| | | .addFormDataPart("user", user) |
| | | .build(); |
| | | MultipartBody.Builder requestBodyBuilder = null; |
| | | try { |
| | | requestBodyBuilder = new MultipartBody.Builder() |
| | | .setType(MultipartBody.FORM) |
| | | // 添加上传文件,这里直接用 MultipartFile 的字节数组构建请求体 |
| | | .addFormDataPart("file", file.getOriginalFilename(), |
| | | RequestBody.create(MediaType.parse("application/octet-stream"), file.getBytes())) |
| | | .addFormDataPart("user", user); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | RequestBody requestBody = requestBodyBuilder.build(); |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(apiUrl) |
| | | .url(apiUrl) // 替换为实际的接口地址常量 |
| | | .post(requestBody) |
| | | .header("Authorization", apiKey) |
| | | .header("Authorization", apiKey) // 替换为实际的授权密钥常量 |
| | | .build(); |
| | | |
| | | try (Response response = client.newCall(request).execute()) { |
| | |
| | | return responseBody; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | return e.getMessage(); |
| | | return "文件上传失败:" + e.getMessage(); |
| | | } |
| | | } |
| | | |