OCR/test_tmp/test_multipart_upload.java
2025-05-13 11:07:14 +08:00

98 lines
4.0 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 测试上传OCR结果JSON文件到后端接口使用multipart/form-data方式
*/
public class test_multipart_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";
File jsonFile = new File(jsonFilePath);
if (!jsonFile.exists()) {
System.err.println("JSON文件不存在: " + jsonFilePath);
return;
}
System.out.println("开始测试JSON文件上传 (multipart/form-data方式)");
System.out.println("上传URL: " + uploadUrl);
System.out.println("文件路径: " + jsonFile.getAbsolutePath());
System.out.println("文件大小: " + jsonFile.length() + " 字节");
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
// 构建HTTP请求
HttpPost httpPost = new HttpPost(uploadUrl);
// 创建multipart实体
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", jsonFile, ContentType.APPLICATION_JSON, jsonFile.getName());
// 设置请求实体
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
System.out.println("正在发送multipart请求...");
// 发送请求
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
String responseBody = responseEntity != null ? EntityUtils.toString(responseEntity) : null;
System.out.println("状态码: " + statusCode);
System.out.println("响应内容: " + responseBody);
if (statusCode >= 200 && statusCode < 300) {
System.out.println("上传成功!");
} else {
System.out.println("上传失败!");
}
}
} catch (Exception e) {
System.out.println("上传异常: " + e.getMessage());
e.printStackTrace();
}
}
}