129 lines
5.5 KiB
Java
129 lines
5.5 KiB
Java
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.apache.http.HttpEntity;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 测试上传完整OCR结果JSON文件到后端接口
|
|
*/
|
|
public class test_full_upload {
|
|
public static void main(String[] args) {
|
|
// 获取配置文件中的URL
|
|
String uploadUrl = null;
|
|
try {
|
|
// 从配置文件中读取upload_url
|
|
String configContent = new String(Files.readAllBytes(Paths.get("config.yaml")), StandardCharsets.UTF_8);
|
|
// 简单解析YAML查找upload_url
|
|
for (String line : configContent.split("\n")) {
|
|
if (line.trim().startsWith("upload_url:")) {
|
|
uploadUrl = line.substring(line.indexOf(":") + 1).trim();
|
|
// 移除可能的引号
|
|
uploadUrl = uploadUrl.replaceAll("^\"|\"$|^'|'$", "");
|
|
// 移除注释部分
|
|
if (uploadUrl.contains("#")) {
|
|
uploadUrl = uploadUrl.substring(0, uploadUrl.indexOf("#")).trim();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.err.println("读取配置文件失败: " + e.getMessage());
|
|
return;
|
|
}
|
|
|
|
if (uploadUrl == null || uploadUrl.isEmpty()) {
|
|
System.err.println("未配置上传URL");
|
|
return;
|
|
}
|
|
|
|
String jsonFilePath = "ocr_results/current_results.json";
|
|
|
|
try {
|
|
// 读取JSON文件内容
|
|
String jsonContent = new String(Files.readAllBytes(Paths.get(jsonFilePath)), StandardCharsets.UTF_8);
|
|
System.out.println("原始JSON内容 (数组格式): " + jsonContent);
|
|
|
|
// 解析JSON以尝试不同格式
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
List<Map<String, Object>> resultsList = objectMapper.readValue(jsonContent, List.class);
|
|
|
|
if (resultsList.isEmpty()) {
|
|
System.out.println("没有OCR结果可上传");
|
|
return;
|
|
}
|
|
|
|
// 获取第一个结果对象
|
|
Map<String, Object> singleResult = resultsList.get(0);
|
|
String singleObjectJson = objectMapper.writeValueAsString(singleResult);
|
|
System.out.println("\n单个对象格式JSON: " + singleObjectJson);
|
|
|
|
// 创建HTTP客户端
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
// 首先尝试上传数组格式
|
|
System.out.println("\n尝试上传数组格式...");
|
|
testUpload(httpClient, uploadUrl, jsonContent);
|
|
|
|
// 然后尝试上传单个对象格式
|
|
System.out.println("\n尝试上传单个对象格式...");
|
|
testUpload(httpClient, uploadUrl, singleObjectJson);
|
|
|
|
// 获取extracted_data部分
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> extractedData = (Map<String, Object>) singleResult.get("extracted_data");
|
|
extractedData.put("file_path", singleResult.get("file_path"));
|
|
extractedData.put("process_time", singleResult.get("process_time"));
|
|
String extractedDataJson = objectMapper.writeValueAsString(extractedData);
|
|
|
|
// 尝试上传提取的数据部分
|
|
System.out.println("\n尝试上传提取的数据部分...");
|
|
testUpload(httpClient, uploadUrl, extractedDataJson);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static void testUpload(CloseableHttpClient httpClient, String uploadUrl, String jsonContent) {
|
|
try {
|
|
// 构建HTTP请求
|
|
HttpPost httpPost = new HttpPost(uploadUrl);
|
|
httpPost.setHeader("Content-Type", "application/json");
|
|
httpPost.setHeader("Accept", "application/json");
|
|
|
|
// 设置请求体
|
|
httpPost.setEntity(new StringEntity(jsonContent, StandardCharsets.UTF_8));
|
|
|
|
System.out.println("正在发送请求到: " + uploadUrl);
|
|
|
|
// 发送请求
|
|
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
HttpEntity entity = response.getEntity();
|
|
String responseBody = entity != null ? EntityUtils.toString(entity) : null;
|
|
|
|
System.out.println("状态码: " + statusCode);
|
|
System.out.println("响应内容: " + responseBody);
|
|
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|
System.out.println("上传成功!");
|
|
} else {
|
|
System.out.println("上传失败!");
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println("上传异常: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |