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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:50:15  来源:igfitidea点击:

Sending blob or byte array in XML

javaandroidxmljakarta-eexmlhttprequest

提问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 encodedImagein 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;
}