package Tools.MaxKb.Service.Impl; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.jfinal.kit.PropKit; import java.io.File; public class MaxKbImpl { //MaxKB的url static final String baseUrl = PropKit.get("baseUrl"); //获取身份识别码接口 static final String Authorization_URL = baseUrl + "/api/user/login"; /** * 上传QA文件到知识库 * * @param authCode 身份识别码 * @param uploadFile 上传文件路径 */ public static String uploadQA(String authCode, String zskId, String uploadFile) { //知识库url上传地址 String qaUrl = baseUrl + "/api/dataset/" + zskId + "/document/qa"; //使用hutool工具箱里面的httpPost功能,向指定的接口发起上传文件的请求,在Header中携带参数authCode HttpResponse response = HttpRequest.post(qaUrl) .header("Accept", "application/json, text/plain, */*") .header("Accept-Encoding", "gzip,deflate") .header("Accept-Language", "zh-CN,zh;q=0.9") .header("Authorization", authCode)// 添加header .header("Connection", "keep-alive") .header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryhEiEDpZDka6byweA") .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36") .form("file", new File(uploadFile)).execute();// 添加文件 // 获取响应内容 String result = response.body(); JSONObject jo = JSONUtil.parseObj(result); int code = jo.getInt("code"); if (code != 200) { System.out.println("请求失败"); return null; } return jo.getJSONArray("data").getJSONObject(0).getStr("id"); } /** * 发送请求获取识别码 * * @throws Exception */ public static String getAuthorization() { //管理员账号与密码 String username = PropKit.get("MaxKBUsername"); String password = PropKit.get("MaxKBPassword"); JSONObject requestBody = JSONUtil.createObj() .set("username", username) .set("password", password); HttpResponse response = HttpRequest.post(Authorization_URL) .header("Content-Type", "application/json") .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36") .header("Origin", baseUrl) .header("Referer", baseUrl + "/ui/login") .body(requestBody.toString()) .timeout(10000) // 可选:设置超时时间(毫秒) .execute(); //System.out.println(response); // 详细的状态处理 if (response.isOk()) { String body = response.body(); if (StrUtil.isBlank(body)) { throw new RuntimeException("响应内容为空"); } try { JSONObject responseJson = JSONUtil.parseObj(body); String data = responseJson.getStr("data"); if (StrUtil.isBlank(data)) { throw new RuntimeException("未获取到data字段"); } return data; } catch (Exception e) { throw new RuntimeException("解析响应JSON失败: " + e.getMessage()); } } else { String errorBody = response.body(); throw new RuntimeException(String.format("请求失败 [%d]: %s", response.getStatus(), StrUtil.isBlank(errorBody) ? "无错误信息" : errorBody)); } } /** * 删除知识库文档 * * @param authCode 身份识别码 * @param zskId 知识库id * @param documentId 文档id * @return */ public static com.alibaba.fastjson.JSONObject delDocument(String authCode, String zskId, String documentId) { String url = baseUrl + "/api/dataset/" + zskId + "/document/" + documentId + "?dep=[object+Object]&__v_isRef=true&__v_isShallow=false&_rawValue=false&_value=false"; HttpResponse response = HttpRequest.delete(url) .header("Accept", "application/json, text/plain, */*") .header("Accept-Encoding", "gzip,deflate") .header("Accept-Language", "zh-CN,zh;q=0.9") .header("Authorization", authCode)// 添加header .header("Connection", "keep-alive") .header("Content-Type", "application/json") .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36") .execute(); return com.alibaba.fastjson.JSONObject.parseObject(response.body()); } }