【问题标题】:Decode a file encoded using base64 in Mac在 Mac 中解码使用 base64 编码的文件
【发布时间】:2018-12-27 01:36:34
【问题描述】:

我有一个在 mac book 中编码为 base 64 的文件 -

base64 cwallet.sso > cwallet.base64.sso

如何使用 Java 解码此文件?

我尝试使用下面的代码,但是生成的文件几乎是原始“cwallet.sso”文件大小的两倍。当我使用 base64 -D cwallet.base64.sso > cwallet.original.sso 编码时,我得到的文件与 cwallet.sso 大小相同。

Java 代码

File file = new File("<path>/cwallet_newfile.sso");
        String contents = FileUtils.readFileToString(new File("<path>/cwallet_base64.sso"), "UTF-8");
        byte[] decodedBytes = Base64.getDecoder().decode(contents.trim());
        String decodedString = new String(decodedBytes);
        FileUtils.write(file,decodedString,"UTF-8");

【问题讨论】:

    标签: java macos unix decode encode


    【解决方案1】:

    不要将(可能的)二进制文件转换为 UTF-8 String;而是将byte[]FileUtils.writeByteArrayToFile(File, byte[]) 类似

    byte[] decodedBytes = Base64.getDecoder().decode(contents.trim());
    // String decodedString = new String(decodedBytes);
    // FileUtils.write(file,decodedString,"UTF-8");
    FileUtils.writeByteArrayToFile(file, decodedBytes);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2015-07-14
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 2020-07-20
      • 2020-04-18
      • 1970-01-01
      相关资源
      最近更新 更多