【问题标题】:Send a pdf file as base64 in POST在 POST 中以 base64 格式发送 pdf 文件
【发布时间】:2016-05-13 20:11:53
【问题描述】:

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

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

【问题讨论】:

    标签: java android json pdf base64


    【解决方案1】:

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

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

    然后将 pdfInBase64 添加到您的帖子中。

    【讨论】:

    • 我应该在哪里添加 pdf 文件的路径?
    【解决方案2】:

    因为java 8你可以做到:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 2011-09-09
      • 2023-03-04
      • 2017-10-07
      • 1970-01-01
      • 2015-03-12
      相关资源
      最近更新 更多