java 以 XML 格式发送 blob 或字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10616769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending blob or byte array in XML
提问by varunrao321
I am a novice back end developer. I am developing a REST webservice. My requirement is to send BLOB content from the server to Mobile Side. My douubt is, is it possible to send BLOB in XML or should I convert it into ByteArray and send it?
我是一个新手后端开发人员。我正在开发 REST 网络服务。我的要求是将 BLOB 内容从服务器发送到移动端。我的疑问是,是否可以在 XML 中发送 BLOB 或者我应该将其转换为 ByteArray 并发送它?
回答by V.J.
First of all. Convert your Bitmap into ByteArray and then Convert that byte array to Base64 String format and send that Base64 String format in xml.
首先。将您的位图转换为 ByteArray,然后将该字节数组转换为 Base64 字符串格式并以 xml 发送该 Base64 字符串格式。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 0 , baos); //bmp is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Now send encodedImage
in your xml...
现在发送encodedImage
您的xml...
Base64 to bitmap conversion
Base64 到位图的转换
public static Bitmap convertByteArrayToBitmap(String Base64String)
{
byte[] data = Base64.decode(Base64String, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
return bitmap;
}