1
我使用Base64類從android上的服務器上傳圖像。使用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>';
?>
如何複製test.jpg放在並重命名上傳? – 2013-03-19 19:16:45