how to upload image in base64 on server(如何在服务器上上传base64中的图像)
问题描述
我有一个问题,我正在将图像上传到服务器上,但事实并非如此.我已经在 base64 中转换图像并通过 json.但是 json 没有正确关闭,因为我遇到了错误.错误 id om postimafe 变量.在这个变量{"key"""encode
中,这里是json没有关闭.
i have a problem, i am uploading image on server but it is not. i have convert image in base64 and get through json. but json is not properly closed due to this i m getting error. error id om postimafe variable. in this variable {"key"""encode
, here is json is not closed.
// code for convert base64
public static String getBase64String(String baseFileUri)
{
String encodedImageData = "";
try
{
System.out.println("getBase64String method is called :" +baseFileUri);
Bitmap bm = BitmapFactory.decodeFile(baseFileUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImageData = Base64.encodeToString(b, Base64.DEFAULT);
//ArrayList<NameValuePair> imagearraylistvalue = new ArrayList<NameValuePair>();
//imagearraylistvalue.add(new BasicNameValuePair("image", encodedImage));
System.out.println("encode data in upload file :" +encodedImageData );
}
catch(Exception ex)
{
System.out.println("Exception in getBase64String method in Utility class :" +ex);
}
return encodedImageData ;
}
// code for json and uplod base64 to server but i m getting error
System.out.println("fullupload image for 1:" +fulluploadimgpath);
String base64String = Utility.getBase64String(fulluploadimgpath);
System.out.println("base64String is in :" +base64String);
if (base64String != null)
{
JSONObject postImageData = new JSONObject();
postImageData.put("media",base64String);
System.out.println("post image :" +postImageData);
HttpResponse imgPostResponse = Utility.postDataOnUrl(Utility.getBaseUrl()+"user/upload",obj.toString());
System.out.println("fullupload image for imgPostResponse:" +imgPostResponse);
if (imgPostResponse != null)
{
String imgResponse = Utility.readUrlResponseAsString(imgPostResponse);
System.out.println("imgResponse is in imgResponse :" +imgResponse);
if (imgResponse != null|| imgResponse.trim().length() != 0)
{
JSONObject jResObj = new JSONObject();
if (jResObj.getBoolean("rc"))
{
obj.put(hidobj.getReceiveAs(),jResObj.getLong("ident"));
}
}
推荐答案
String encodedImageData =getEncoded64ImageStringFromBitmap(your bitmap);
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
这篇关于如何在服务器上上传base64中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在服务器上上传base64中的图像
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01