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

7 months ago
package UnitTest;
7 months ago
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;
7 months ago
import java.io.File;
public class UploadMaxKBFile {
7 months ago
//获取身份识别码接口
private static final String baseUrl = "http://10.10.14.206:8080";
private static final String Authorization_URL = baseUrl + "/api/user/login";
7 months ago
//获取身份识别时登录的用户名
private static final String Authorization_USERNAME = "admin";
//获取身份识别时登录的密码
private static final String Authorization_PASSWORD = "Dsideal4r5t6y7u!@#";
7 months ago
//知识库上传文档接口地址 知识库id不知道id的可以从知识库的地址栏获取
static String zskId = "409225d8-c352-11ef-a419-0242ac120003";
//知识库url上传地址
static String qaUrl = baseUrl + "/api/dataset/" + zskId + "/document/qa";
7 months ago
public static void main(String[] args) throws Exception {
//获取身份识别
7 months ago
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);
7 months ago
}
7 months ago
/**
*
*
* @throws Exception
*/
public static String getAuthorization(String username, String password) {
JSONObject requestBody = JSONUtil.createObj()
.set("username", username)
.set("password", password);
7 months ago
try {
7 months ago
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("响应内容为空");
7 months ago
}
7 months ago
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());
}
7 months ago
} else {
7 months ago
String errorBody = response.body();
throw new RuntimeException(String.format("请求失败 [%d]: %s",
response.getStatus(),
StrUtil.isBlank(errorBody) ? "无错误信息" : errorBody));
7 months ago
}
} catch (Exception e) {
7 months ago
throw new RuntimeException("获取Authorization失败: " + e.getMessage(), e);
7 months ago
}
}
}