在 POST 中发送 base64 格式的 pdf 文件

Send a pdf file as base64 in POST

我想通过 POST 将 pdf 文件发送到端点 url,但它需要使用 base64。我将在 JSON 中发送其他参数,但无法弄清楚如何将 pdf 文件转换为 base64 并将其添加到 JSON。

仅在网络上使用 base64 转换器并复制文本并将其作为字符串粘贴到 JSON 中是否更容易?

你可以像这样在 java 中进行 base64 编码。

byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
String pdfInBase64 = new String(encodedBytes);

然后将 pdfInBase64 添加到您的 post。

因为 java 8 岁可以做:

    byte[] inFileBytes = Files.readAllBytes(Paths.get(pdfFileName)); 
    byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes);
    System.out.println(encoded.length);