亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么不能使用 httpurlconnection 發布整個 base64 字符串?

為什么不能使用 httpurlconnection 發布整個 base64 字符串?

幕布斯6054654 2022-07-27 09:46:29
我想將圖像上傳到服務器。我得到了圖像的位圖并將其編碼為base64。我使用 encodeToString 方法將圖像的 base64 轉換為字符串。我使用 httpurlconnection 將字符串發布到 PHP。實際上,我從 PHP 獲得的字符串不是整個字符串。我沒有收到任何錯誤。請給我反饋!public String httpURLConnectionPost(final String urlString){            String result="";            try {            URL url = new URL(urlString);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            connection.setRequestMethod("POST");            connection.setDoOutput(true);            connection.setDoInput(true);            connection.setUseCaches(false);            connection.connect();            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.download);            ByteArrayOutputStream baos = new ByteArrayOutputStream();            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);            byte[] imageBytes = baos.toByteArray();            String encodedImage = Base64.encodeToString(imageBytes, Base64.NO_CLOSE);            String body= "image="+encodedImage;            Log.d("serverPostData","body = " +body);            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));            writer.write(body);            writer.close();            int responseCode = connection.getResponseCode();            if(responseCode == HttpURLConnection.HTTP_OK){                InputStream inputStream = connection.getInputStream();                StringBuilder stringBuilder = new StringBuilder();                String line;                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));                while ((line = br.readLine()) != null) {                    stringBuilder .append(line);                }                result = stringBuilder .toString();            }        } catch (Exception e) {            e.printStackTrace();        }        return result;    }
查看完整描述

2 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

我解決了base64需要將空替換為“+”的問題。


<?php


include("mysqli_connect.php");


$image= $_POST['image'];

$change =  str_replace(" ","+",$image);


$a = uniqid() ; 


$ImagePath = "good/$a.JPEG";


$ServerURL = "https://172.30.10.1/$ImagePath";


 $InsertSQL = "INSERT INTO Photoshop(photo) VALUES('$ServerURL')" ;


 if(mysqli_query($connect, $InsertSQL)){

          file_put_contents($ImagePath,base64_decode($change ));

          echo $image;          

 }else{

    echo "failed";

}


mysqli_close();

?>


查看完整回答
反對 回復 2022-07-27
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

試試這個將圖像轉換為base64:


Uri uri = data.getData();

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

String encodedImageData = getEncoded64ImageStringFromBitmap(bitmap);

現在在 API 中傳遞數據:


 JsonObject data1 = new JsonObject();

 data1.addProperty("imageData", "data:image/png;base64," + encodedImageData);

現在設置 ImageBitmap:


image.setImageBitmap(bitmap);

這是方法:


private String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {


    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);

    byte[] byteFormat = stream.toByteArray();

    // get the base 64 string

    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);


    return imgString;

}

對于 HttpUrlConnection 試試這個......


jsonObject = new JSONObject();

jsonObject.put("imageString", encodedImage);

String data = jsonObject.toString();

String yourURL = "yourphpfileurl";

URL url = new URL(yourURL);


HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

connection.setFixedLengthStreamingMode(data.getBytes().length);

connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");


OutputStream out = new BufferedOutputStream(connection.getOutputStream());

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));

writer.write(data);

writer.flush();

writer.close();

out.close();

connection.connect();


InputStream in = new BufferedInputStream(connection.getInputStream());

BufferedReader reader = new BufferedReader(new InputStreamReader(

                in, "UTF-8"));

        StringBuilder sb = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {

            sb.append(line);

        }

        in.close();

        String result = sb.toString();

        connection.disconnect();

希望這個對你有用...


查看完整回答
反對 回復 2022-07-27
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號