簡體   English   中英

在 POST 中將 pdf 文件作為 base64 發送

[英]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 添加到您的帖子中。

因為 java 8 你可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM