如何将base64格式的图片上传到服务器上

2023-11-26

我有一个问题,我正在服务器上上传图像,但事实并非如此。我已经将图像转换为base64并通过json. but json由于这个原因,没有正确关闭,我收到错误。 postimage 变量上的错误 ID。在这个变量中{"key"""encode, 这是json没有关闭。

        // 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将base64格式的图片上传到服务器上 的相关文章

随机推荐