【问题标题】:Base 64 encoding an image file in Groovy using Apache Commons使用 Apache Commons 在 Groovy 中对图像文件进行 Base 64 编码
【发布时间】:2019-09-28 09:44:41
【问题描述】:

我正在尝试使用site 上的说明将图像文件编码为 Base64 字符串。唯一的区别是我有一个 groovy 脚本(而不是 Java),我的整个脚本只是....

  @Grapes(
    @Grab(group='commons-io', module='commons-io', version='2.6')
  )

 import org.apache.commons.io.FileUtils
 import org.apache.commons.codec.binary.Base64


  byte[] fileContent = FileUtils.readFileToByteArray(new File('/Users/me/Test.jpeg'));
  String encodedString = Base64.getEncoder().encodeToString(fileContent);

当我运行它时,我得到以下异常并且无法弄清楚为什么......

 groovy.lang.MissingMethodException: 
 No signature of method: static org.apache.commons.codec.binary.Base64.getEncoder() is applicable for argument types: () values: []
 Possible solutions: encode([B), encode(java.lang.Object)

【问题讨论】:

标签: java groovy encoding base64 apache-commons-codec


【解决方案1】:

您正在导入org.apache.commons.codec.binary.Base64

使用Base64.getEncoder().encodeToString(fileContent),您调用的是java.util.Base64,它的方法不是org.apache.commons.codec.binary.Base64

由于 java.util.* 是 Groovy 的默认导入之一,您的代码在删除 import org.apache.commons.codec.binary.Base64 时应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-19
    • 2011-09-16
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2013-08-16
    • 1970-01-01
    相关资源
    最近更新 更多