How to put a byte array into XML in Scala

倾然丶 夕夏残阳落幕 提交于 2019-12-07 05:37:26

Your current version is just writing the bytes (I'm assuming request.getContent().array() is an array of bytes) as space-separated base-10 integers:

scala> val bytes = 1 to 10 map(_.toByte) toArray
bytes: Array[Byte] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> <FileContents>{bytes}</FileContents>
res0: scala.xml.Elem = <FileContents>1 2 3 4 5 6 7 8 9 10</FileContents>

This definitely isn't what you want. You can use a library like Apache Commons Codec to encode the byte array as a string (here I'm using the Base64 encoder):

scala> import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.Base64

scala> <FileContents>{Base64.encodeBase64String(bytes)}</FileContents>
res1: scala.xml.Elem = <FileContents>AQIDBAUGBwgJCg==</FileContents>

You might have to tinker with the options a bit, but this is much more likely to be what you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!