【发布时间】: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)
【问题讨论】:
-
Base64 类没有 getEncoder() 方法,但它确实有一堆编码方法:commons.apache.org/proper/commons-codec/apidocs/org/apache/…
-
在 groovy 中,无需外部库也可以这样做:
new File('/Users/me/Test.jpeg').getBytes().encodeBase64()
标签: java groovy encoding base64 apache-commons-codec