我正在尝试将Android相机捕获的图像上传到服务器,但图片未保存。
机器人:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] array = stream.toByteArray();
encoded_string =android.util.Base64.encodeToString(array, android.util.Base64.NO_WRAP);
String phpUrl = "http://192.168.43.104:80/dogCountingApp/newDog.php";
URL url = new URL(phpUrl);
HttpURLConnection httpurlconnection = (HttpURLConnection)url.openConnection();
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setDoOutput(true);
OutputStream outputstream = httpurlconnection.getOutputStream();
BufferedWriter bufferedwriter = new BufferedWriter(new OutputStreamWriter(outputstream,"UTF-8"));
String data = URLEncoder.encode("encoded_string","UTF-8") + "=" + URLEncoder.encode(encoded_string,"UTF-8")+"&"+
URLEncoder.encode("image_name","UTF-8")+"="+URLEncoder.encode(image_name,"UTF-8");
bufferedwriter.write(data);
bufferedwriter.flush();
bufferedwriter.close();
outputstream.close();
腓
<?php
header('content-type : bitmap; charset = utf-8');
if(isset($_POST["encoded_string"])){
$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$decoded_string = base64_decode($encoded_string);
$path = 'images/'.$image_name;
$file = fopen($path, 'wb');
fwrite($file, $decoded_string);
fclose($file);
}
?>
非常感谢任何帮助。