java - 编码字符串的 Http get/post

标签 java android base64 inputstream

我对发送和获取图像编码数据有疑问。首先,我在字符串中将图像作为 Base64 编码类型,该字符串具有如下值: ...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZ...

现在,如果我再次解码,并且如果我使用 BitmapFactory 来适应 imageview,那么图像就可以了。

byte[] bytes= stream.toByteArray();                               
imagestr=Base64.encodeBytes(bytes).toString();
//If i code below it is working
byte[] decode = Base64.decode(imagestr);
decoded = BitmapFactory.decodeByteArray(decode, 0, decode.length);

//If i send to the server and handle it in servlet file
String pic = request.getParameter("p");
byte[] servdec = Base64.decode(pic);
//and if i use the servdec to output a image file file is corrupted.
//I noticed the pic and imagestr are different 
//imagestr = **...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZ...**
//pic      = **...D/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko MzZ...**
//pic has no + sign.

我使用了replaceAll,但它仅适用于这种情况。这可能会导致更多的问题。那么有什么解决方案可以建议吗谢谢您的回答...

嗨,这个字符串位于该函数的 pic 中,在该函数之后 servlet 将处理这个!pic 在该函数中有 + 号
公共(public)字符串uuidfaceid(字符串uuid,字符串faceid,字符串名称,字符串图片){

URL url = null;

try {

url = new 

URL("http://"+Constants.SERVER_NAME+Constants.SERVER_PORT+"/MeetInTouch/UF"+"? uuid="+uuid+"&faceid="+faceid+"&name="+name+"&pic="+pic);

} catch (MalformedURLException e1) {

e1.printStackTrace();

}

URLConnection ucon = null;

try {

ucon = url.openConnection();

} catch (IOException e1) {

e1.printStackTrace();

}

try {

ucon.connect();

} catch (IOException e1) {

e1.printStackTrace();

}

最佳答案

包含 Base64 编码字符串的“p”参数在某个时刻已被“url 解码”。您所要做的就是在尝试解码 Base64 之前再次对其进行编码:

String pic = request.getParameter("p");
pic = URLEncoder.encode(pic, "ISO-8859-1");
byte[] servdec = Base64.decode(pic);

关于java - 编码字符串的 Http get/post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15453466/

相关文章:

java - Web应用的具体缓存场景

java - 闹钟管理器不重复?

java - 从 Java 运行 Ruby 命令行应用程序

java - 2D 相机翻译

java - Elastic Beanstalk 上的 Renjin 'IOException while loading package'

android - 如何在不同的计算机上签署apk?

android - 无法获取未知属性 'manifestOutputDirectory'

android - 如何在android Canvas 上使用base64编码的图像数据绘制图像

ios - 使用 NSMutableURLRequest 的字符串请求

php - Android 和 PHP 中的 AES CBC 通过 Base64