2013-03-19 146 views
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>'; 

?> 
+0

如何複製test.jpg放在並重命名上傳? – 2013-03-19 19:16:45

回答

0

我找到解決辦法:

這裏被修飾的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")); 
1

嘿老兄使用這一個圖像用不同的名稱

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

fwrite($file, $binary); 

fclose($file); 
相關問題