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

问题描述:

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

?> 
+0

如何复制test.jpg放在并重命名上传? – 2013-03-19 19:16:45

我找到解决办法:

这里被修饰的PHP脚本:

<?php 

$base=$_REQUEST['image']; 

$ImageName=$_REQUEST['ImageName']; 

copy('test.jpg',$ImageName); 

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($ImageName, 'wb'); 

fwrite($file, $binary); 

fclose($file); 

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

?> 

这:

nameValuePairs.add(new BasicNameValuePair("ImageName","newImage.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);