
本文档旨在指导开发者在使用 Citrus 框架时,如何正确接收 HTTP Post 请求中返回的 PDF 文件,并将其保存到本地。重点在于避免常见的编码问题,并提供一种简洁高效的文件写入方法。同时,介绍了一种自定义的 HttpClient,方便处理 HTTP 请求中的二进制数据。
在使用 Citrus 框架进行集成测试时,接收 HTTP Post 请求返回的 PDF 文件并将其写入文件是一个常见的需求。如果直接将接收到的 Payload 作为对象进行序列化,可能会导致文件内容为空。正确的做法是直接将 PDF 文件的字节数组写入文件。
以下是如何在 Citrus 中接收 PDF 文件并将其写入文件的步骤:
-
接收 PDF 文件:
使用 Citrus 的 http 方法接收 HTTP 响应,并确保指定正确的 contentType 为 MediaType.APPLICATION_PDF_VALUE。
runner.http(httpActionBuilder -> httpActionBuilder .client(vdeClient) .receive() .response(HttpStatus.OK) .contentType(MediaType.APPLICATION_PDF_VALUE) ); -
获取 Payload:
从消息存储中获取消息,并获取其 Payload。此时的 Payload 应该是 PDF 文件的字节数组。
Object pdfPayload = context.getMessageStore().getMessage("nameOfTheMessage").getPayload(); -
将 Payload 转换为字节数组:
如果 pdfPayload 已经是字节数组 (byte[]) 类型,则可以直接使用。如果不是,可能需要进行类型转换。 确保正确处理可能出现的类型转换异常。
byte[] pdfBytes = (byte[]) pdfPayload;
-
将字节数组写入文件:
使用 Files.write 方法将字节数组直接写入 PDF 文件。避免使用 ObjectOutputStream 进行序列化,因为这可能会导致文件内容为空。
try { Files.write(Paths.get("path/to/your/file.pdf"), pdfBytes); } catch (IOException e) { e.printStackTrace(); // 错误处理 }
示例代码:
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.core.io.FileSystemResource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;
import org.testng.annotations.Test;
public class PdfHandlingTest extends TestNGCitrusTestRunner {
@Test
public void testReceiveAndWritePdf() {
String pdfFileName = "received.pdf";
// 发送请求并接收PDF响应
http(httpActionBuilder -> httpActionBuilder
.client("vdeClient")
.send()
.post("/generatePdf")
.payload(new FileSystemResource("src/test/resources/payload.xml")) // 假设有一个payload.xml文件
.contentType(MediaType.APPLICATION_XML_VALUE));
http(httpActionBuilder -> httpActionBuilder
.client("vdeClient")
.receive()
.response(HttpStatus.OK)
.contentType(MediaType.APPLICATION_PDF_VALUE)
.name("pdfResponse"));
// 获取Payload并写入文件
receive(receiveBuilder -> receiveBuilder
.message(context -> {
byte[] pdfBytes = (byte[]) context.getMessageStore().getMessage("pdfResponse").getPayload();
try {
Files.write(Paths.get(pdfFileName), pdfBytes);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}));
}
}注意事项:
- 确保在 http 方法中设置了正确的 contentType,以便 Citrus 正确解析响应。
- 直接使用 Files.write 方法将字节数组写入文件,避免使用 ObjectOutputStream。
- 进行适当的错误处理,例如捕获 IOException 异常。
- vdeClient需要在Citrus上下文中正确配置。
- 根据实际情况替换示例代码中的文件名和路径。
自定义 HttpClient (可选):
如果需要更灵活地处理 HTTP 请求,可以考虑使用自定义的 HttpClient。 例如 MgntUtils 库提供的 HttpClient。
import com.mgnt.lifecycle.management.httpclient.HttpClient;
import com.mgnt.utils.TextUtils;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
public class HttpClientExample {
public static void downloadFile() {
HttpClient httpClient = new HttpClient();
httpClient.setRequestHeader("Accept", "application/octet-stream");
httpClient.setConnectionUrl("https://dl.k8s.io/release/v1.20.0//bin/linux/amd64/kubectl");
ByteBuffer buffer = null;
try {
buffer = httpClient.sendHttpRequestForBinaryResponse(HttpClient.HttpMethod.GET);
System.out.println(httpClient.getLastResponseCode() + " " + httpClient.getLastResponseMessage());
} catch (IOException ioe) {
System.out.println(httpClient.getLastResponseCode() + " " + httpClient.getLastResponseMessage());
System.out.println(TextUtils.getStacktrace(ioe, "com.mgnt.stam."));
}
try {
Files.write(Paths.get("C:\\Michael\\work\\Installations\\Kubernetes\\kubectl"), buffer.array());
} catch (IOException e) {
System.out.println(TextUtils.getStacktrace(e, "com.mgnt.stam."));
}
}
}总结:
通过以上步骤,您可以在 Citrus 框架中正确接收 HTTP Post 请求返回的 PDF 文件,并将其保存到本地。 关键在于正确处理 Payload,并使用 Files.write 方法直接写入字节数组。 如果需要更灵活地处理 HTTP 请求,可以考虑使用自定义的 HttpClient。










