gpt4 book ai didi

android - 使用Base64将图像从android上传到服务器

转载 作者:行者123 更新时间:2023-11-30 03:42:33 25 4
gpt4 key购买 nike

我使用Base64类从android服务器上载图像。

在服务器端,我有两件事:

-upload.php脚本和

-test.jpg图像文件

每次上传完成时,test.jpg都会被新图片覆盖。

如何解决这个问题?

这是我的上传代码:

InputStream is;
private void uploadSlike (Bitmap bitmapOrg) {

try {

ByteArrayOutputStream bao = new ByteArrayOutputStream();

bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte [] ba = bao.toByteArray();

String ba1=Base64.encodeBytes(ba);

ArrayList<NameValuePair> nameValuePairs = new

ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("image",ba1));



HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new

HttpPost("http://myserver/pictureupload.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

is = entity.getContent();

System.out.println(entity.getContentLength());


} catch (Exception e) {


System.out.println("Greska prilikom uploada:"+e);
}

System.out.println("zavrsni sam uplaod");


}


upload.php脚本

<?php

$base=$_REQUEST['image'];

echo $base;

// base64 encoded utf-8 string

$binary=base64_decode($base);

// binary, utf-8 bytes

header('Content-Type: bitmap; charset=utf-8');

// print($binary);

//$theFile = base64_decode($image_data);

$file = fopen('test.jpg', 'wb');

fwrite($file, $binary);

fclose($file);

echo '<img src=test.jpg>';

?>

最佳答案

嗨,兄弟,使用此图片上传名称不同的图片

$new_image_name = 'image_' . date('Y-m-d-H-i-s') . '_' . uniqid() . '.jpg';
$file = fopen($new_image_name, 'wb');

fwrite($file, $binary);

fclose($file);

关于android - 使用Base64将图像从android上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com