为什么不能使用 httpurlconnection 发布整个 base64 字符串?

我想将图像上传到服务器。

  1. 我得到了图像的位图并将其编码为base64。

  2. 我使用 encodeToString 方法将图像的 base64 转换为字符串。

  3. 我使用 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;

    }


幕布斯6054654
浏览 149回答 2
2回答

HUH函数

我解决了base64需要将空替换为“+”的问题。<?phpinclude("mysqli_connect.php");$image= $_POST['image'];$change =&nbsp; str_replace(" ","+",$image);$a = uniqid() ;&nbsp;$ImagePath = "good/$a.JPEG";$ServerURL = "https://172.30.10.1/$ImagePath";&nbsp;$InsertSQL = "INSERT INTO Photoshop(photo) VALUES('$ServerURL')" ;&nbsp;if(mysqli_query($connect, $InsertSQL)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_put_contents($ImagePath,base64_decode($change ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $image;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;}else{&nbsp; &nbsp; echo "failed";}mysqli_close();?>

牛魔王的故事

试试这个将图像转换为base64:Uri uri = data.getData();Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);String encodedImageData = getEncoded64ImageStringFromBitmap(bitmap);现在在 API 中传递数据:&nbsp;JsonObject data1 = new JsonObject();&nbsp;data1.addProperty("imageData", "data:image/png;base64," + encodedImageData);现在设置 ImageBitmap:image.setImageBitmap(bitmap);这是方法:private String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {&nbsp; &nbsp; ByteArrayOutputStream stream = new ByteArrayOutputStream();&nbsp; &nbsp; bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);&nbsp; &nbsp; byte[] byteFormat = stream.toByteArray();&nbsp; &nbsp; // get the base 64 string&nbsp; &nbsp; String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);&nbsp; &nbsp; 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(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in, "UTF-8"));&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; String line = null;&nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(line);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; in.close();&nbsp; &nbsp; &nbsp; &nbsp; String result = sb.toString();&nbsp; &nbsp; &nbsp; &nbsp; connection.disconnect();希望这个对你有用...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java