2013-02-07 47 views

回答

6

下面是一个解决方案,其他人可能更有效。

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) 

你可以(也应该)也由Apache公地Base64了更好的兼容性更换sun.misc编码器。

val b64 = org.apache.commons.codec.binary.Base64.encodeBase64(bytes) 
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) 
+0

我认为你的回答是好,假设阿帕奇公共图书馆。 –

0

这里有一个simple encoder/decoder我写的,你可以包括源。所以,没有外部依赖。

的接口是有点多阶式的:

import io.github.marklister.base64.Base64._ // Same as Lomig Mégard's answer val b64 = bytes.toBase64

相关问题