package UnitTest; import lombok.Getter; import lombok.Setter; import org.json.JSONArray; import org.json.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.*; @Getter @Setter public class UploadMaxKBFile { /*** * 具体思路如下 * 1.获取身份识别码 * 2.将文件分段并拿到分段值 * 3.将分段值传入知识库上传文档接口,完成上传操作 * ***/ //获取身份识别吗接口 private static final String Authorization_URL = "http://10.10.14.206:8080/api/user/login"; //获取身份识别时登录的用户名 private static final String Authorization_USERNAME = "admin"; //获取身份识别时登录的密码 private static final String Authorization_PASSWORD = "Dsideal4r5t6y7u!@#"; //身份识别码 private static String Authorization_CODE = ""; //分段接口地址 private static final String Split_URL = "http://10.10.14.206:8080/api/dataset/document/split"; //知识库上传文档接口地址 其中111b924c-44dd-11ef-aaae-0242ac110002为知识库id,不知道id的可以从知识库的地址栏获取 private static final String Upload_URL = "http://10.10.14.206:8080/api/dataset/7a6ca162-45a7-11ef-9059-0242ac110002/document/_bach"; //文件地址 private static final String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\ceshi\\编码测试.docx"; //文件名 private static String FILE_NAME = "编码测试"; public static void main(String[] args) throws Exception { //获取身份识别 getAuthorization(); //文件分段 //HashMap map_content = uploadFile(Split_URL,FILE_PATH); //上传文件 //sendPostRequest_1(Upload_URL,map_content); } //发送请求获取识别码 public static void getAuthorization() throws Exception { URL url = new URL(Authorization_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("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"); connection.setRequestProperty("Origin", "http://localhost:8080"); connection.setRequestProperty("Referer", "http://localhost:8080/ui/login"); connection.setDoOutput(true); JSONObject json = new JSONObject(); json.put("username", Authorization_USERNAME); json.put("password", Authorization_PASSWORD); String jsonBody = json.toString(); try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); try (BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { StringBuilder response = new StringBuilder(); String responseLine = null; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } JSONObject jsonObject = new JSONObject(response.toString()); Authorization_CODE = jsonObject.getString("data"); System.out.println("拿到的身份识别吗 " + Authorization_CODE); } } public static void sendPostRequest_1(String urlString, HashMap map_content) { try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); //身份识别,每个可能不同可以通过F12调用方法的时候看看传的是什么 connection.setRequestProperty("Authorization", Authorization_CODE); //处理分段后的文件内容将其转换为json串 ArrayList paragraphs = new ArrayList<>(); Set keys = map_content.keySet(); for (String key : keys) { Paragraph paragraph = new Paragraph(); paragraph.setTitle(key); paragraph.setContent(map_content.get(key)); paragraphs.add(paragraph); } DocumentWrapper documentWrapper = new DocumentWrapper(); documentWrapper.setName(FILE_NAME); documentWrapper.setParagraphs(paragraphs); String jsonBody = JsonUtil.objectToJson(documentWrapper); jsonBody = "[" + jsonBody + "]"; //发送请求 OutputStream os = connection.getOutputStream(); os.write(jsonBody.getBytes("UTF-8")); os.flush(); int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { // success BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Print result System.out.println(response.toString()); } else { System.out.println("POST request did not work."); } } catch (Exception e) { e.printStackTrace(); } } /** * 将文件分段 * return:HashMap<分段名, 分段内容> */ public static HashMap uploadFile(String targetUrl, String filePath) { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(targetUrl); HashMap map_content = new HashMap<>(); try { File file = new File(filePath); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()); HttpEntity multipart = builder.build(); //将需要分段的文件放入接口请求中 post.setEntity(multipart); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); //解析返回的分段内容 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent())); String reposeBody = reader.readLine(); JSONObject jsonObject = new JSONObject(reposeBody); JSONArray array = jsonObject.getJSONArray("data"); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); // FILE_NAME=object.getString("name"); JSONArray content = object.getJSONArray("content"); for (int j = 0; j < content.length(); j++) { JSONObject content_object = content.getJSONObject(j); map_content.put(content_object.getString("title"), content_object.getString("content")); } } } catch (IOException e) { e.printStackTrace(); } return map_content; } @Getter @Setter static class DocumentWrapper { private String name; private List paragraphs; } static class JsonUtil { public static String objectToJson(Object obj) { try { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } catch (Exception e) { e.printStackTrace(); return null; } } public T jsonToObject(String json, Class clazz) { try { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(json, clazz); } catch (Exception e) { e.printStackTrace(); return null; } } } @Getter @Setter static class Paragraph { private String title; private String content; } private String name; private List paragraphs; }