You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package UnitTest;
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 java.io.File;
public class UploadMaxKBFile {
//获取身份识别码接口
private static final String baseUrl = "http://10.10.14.206:8080";
private static final String Authorization_URL = baseUrl + "/api/user/login";
//获取身份识别时登录的用户名
private static final String Authorization_USERNAME = "admin";
//获取身份识别时登录的密码
private static final String Authorization_PASSWORD = "Dsideal4r5t6y7u!@#";
//知识库上传文档接口地址 知识库id不知道id的可以从知识库的地址栏获取
static String zskId = "409225d8-c352-11ef-a419-0242ac120003";
//知识库url上传地址
static String qaUrl = baseUrl + "/api/dataset/" + zskId + "/document/qa";
public static void main(String[] args) throws Exception {
//获取身份识别
String authCode = getAuthorization(Authorization_USERNAME, Authorization_PASSWORD);
System.out.println("Authorization_CODE:" + authCode);
//获取知识库上传文档接口
String uploadFile = "D:\\dsWork\\QingLong\\Doc\\MaxKB\\各学校人员和班级统计【结果】.xlsx";
//使用hutool工具箱里面的httpPost功能向指定的接口发起上传文件的请求在Header中携带参数authCode
HttpResponse response = HttpRequest.post(qaUrl)
// 添加header
.header("Authorization_CODE", authCode)
// 添加文件
.form("file", new File(uploadFile))
.execute();
// 获取响应状态码
int status = response.getStatus();
// 获取响应内容
String result = response.body();
System.out.println("Status: " + status);
System.out.println("Response: " + result);
}
/**
* 发送请求获取识别码
*
* @throws Exception
*/
public static String getAuthorization(String username, String password) {
JSONObject requestBody = JSONUtil.createObj()
.set("username", username)
.set("password", password);
try {
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();
// 详细的状态处理
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));
}
} catch (Exception e) {
throw new RuntimeException("获取Authorization失败: " + e.getMessage(), e);
}
}
}