尝试从Amazon S3中提取图像(返回S3ObjectInputStream)并将其发送到mandrill电子邮件api(采用base64编码的字符串).如何在Scala中完成?
Dax*_*ohl 13
我还设法只使用Apache公共; 不知道哪种方法更好,但我想我会留下这个答案的记录:
import org.apache.commons.codec.binary.Base64
import org.apache.commons.io.IOUtils
val bytes = IOUtils.toByteArray(stream)
val bytes64 = Base64.encodeBase64(bytes)
val content = new String(bytes64)
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案,可能有更高效的其他解决方案.
val is = new ByteArrayInputStream(Array[Byte](1, 2, 3)) // replace by your InputStream
val stream = Stream.continually(is.read).takeWhile(_ != -1).map(_.toByte)
val bytes = stream.toArray
val b64 = new sun.misc.BASE64Encoder().encode(bytes)
Run Code Online (Sandbox Code Playgroud)
你可以(也应该)用sun.miscapache commons Base64替换编码器,以获得更好的兼容性.
val b64 = org.apache.commons.codec.binary.Base64.encodeBase64(bytes)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10683 次 |
| 最近记录: |