【问题标题】:base64 String decoding using Apache Commons使用 Apache Commons 进行 base64 字符串解码
【发布时间】:2014-02-24 23:21:18
【问题描述】:

我正在尝试从 Dropbox 解码我的一个文件。使用此论坛中的帖子之一

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
String base64String = getFileResults.get_Response();//returns a file content in base64
String result = StringUtils.newStringUtf8(Base64.decodeBase64(base64String));

但 Netbeans 一直告诉我 decodeBase64() 不接受字符串,但 API 说它接受 base 64 中的字符串。

我错过了什么?

【问题讨论】:

  • 您缺少)
  • 我很抱歉发布这个......我不敢相信我错过了那个。谢谢。
  • 没关系,它仍然给出同样的错误
  • 您使用的是什么版本的 apache commons?请发布完整的错误消息。
  • 编译错误:Base64 中的 decode64 不能应用于给定类型。必需:找到字节[]:字符串

标签: java base64 apache-commons


【解决方案1】:

试试 Java SE 中的 javax.xml.bind.DatatypeConverter.parseBase64Binary(base64String)

【讨论】:

    【解决方案2】:
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.io.FileUtils;
    
    ...
    File file = new File( "path" );
    byte[] bytes = Base64.decodeBase64( "base64" );
    FileUtils.writeByteArrayToFile( file, bytes );
    

    【讨论】:

    • "Code-only" 答案应该避免,因为它们没有解释为什么 OP 的代码(如果有)、策略或概念是错误的,或者为什么答案是错误的是对的。提供解释不仅可以让 OPA,还可以让所有读者了解正在发生的事情;包括对正在使用的编程语言和/或 API 不熟悉的人,以及可能不熟悉代码的人,因此可能觉得它不直观(即使您这样做)。换句话说,请记住,why 远比 what 重要,尤其是在答案方面。
    猜你喜欢
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2020-10-08
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多