2013-02-07 49 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

相關問題