2015-10-26 53 views
0

嗨我正在製作一個應用程序,它將圖片發送到服務器。使用Apache棄用函數的版本可以工作,但我不知道爲什麼我無法獲得更新的解決方案。任何人都知道這裏的錯誤在哪裏?用httpurlconnection發送圖片

最新的解決方案:它不會給logcat中的錯誤,但是當我去服務器時沒有上傳任何東西。起初,我認爲錯誤在於我如何傳遞參數,但我嘗試了幾種不同的解決方案,比如使用Uri.builder,使用HashMap和stringBuilder編碼參數的方法,傳遞字符串像這樣......並且什麼也沒有工作。我需要幫助,這是真的快把我逼瘋了

@Override 
    protected Void doInBackground(Void... params) { 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); 
     String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 
     try { 
      byteArrayOutputStream.flush(); 
      byteArrayOutputStream.close(); 
     }catch (IOException e){ 

     } 
     HttpURLConnection connection; 
     try { 
      String urlSt = "http://phoenixcoding.tk/SavePicture.php"; 
      URL url = new URL(urlSt); 
      connection = (HttpURLConnection) url.openConnection(); 

      connection.setRequestMethod("POST"); 

      /*Uri.Builder builder = new Uri.Builder() 
        .appendQueryParameter("name", name) 
        .appendQueryParameter("image", encodedImage); 

      String query = builder.build().getEncodedQuery();*/ 

      OutputStream os = connection.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8")); 
      writer.write("name=example&image=" + encodedImage); 

      writer.flush(); 
      writer.close(); 
      os.close(); 

      connection.connect(); 

     }catch (IOException e){ 
      e.printStackTrace(); 
     } 
     return null; 
} 

前一種解決方案:它工作得很好

@Override 
    protected Void doInBackground(Void... params) { 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); 
     String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 
     try { 
      byteArrayOutputStream.flush(); 
      byteArrayOutputStream.close(); 
     }catch (IOException e){ 

     }ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>(); 
     dataToSend.add(new BasicNameValuePair("name", name)); 
     dataToSend.add(new BasicNameValuePair("image", encodedImage)); 

     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://phoenixcoding.tk/SavePicture.php"); 

     try{ 
      post.setEntity(new UrlEncodedFormEntity(dataToSend)); 
      client.execute(post); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 

     return null; 

    } 

的SavePhoto.php文件:

<?php 
$name = $_POST["name"]; 
$image = $_POST["image"]; 
$decodedImage = base64_decode("$image"); 
file_put_contents("pictures/" . $name . ".JPG", $decodedImage); 

>

回答

0

謝謝,我終於做到了工作。我不知道這是爲什麼,但是在打開連接後添加一個InputStream對象後,圖片上傳正確。我所做的只是添加了這些行:

 InputStream is = connection.getInputStream(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder str = new StringBuilder(); 
     String strLine; 
     while ((strLine = reader.readLine()) != null) { 
      str.append(strLine + "\n"); 
     } 
     is.close(); 
1

試試這個在您的php代碼中:

if(isset($_POST["image"]) && !empty($_POST["image"])){ 
     $profile_pic = ''; 
     $data= $_POST['image']; 

     $data = str_replace('data:image/png;base64,', '', $data); 
     $data = str_replace(' ', '+', $data); 
     $binary=base64_decode($data); 
     header('Content-Type: bitmap; charset=utf-8'); 
     // Images will be saved under 'www/pictures' folder 
     $new_name = $_POST['name'] . '.png'; 
     $success =file_put_contents('pictures/'.$new_name,$binary); 

     $profile_pic = $new_name; 

    } 
1

我想這條線是越野車 - $decodedImage = base64_decode("$image");ü必須這樣寫,而不是$decodedImage = base64_decode($image);

調試做到這一點: -

<?php 
file_put_contents("post.txt",print_r($_POST,true)); 
$name = $_POST["name"]; 
..... 
?> 

觀點如下: - http://phoenixcoding.tk/post.txt

(如果文件沒有保存,那麼在這種情況下有權限問題做一個目錄「測試」,並給予它權限755即使它不起作用使該目錄爲777,然後你的網址將是http://phoenixcoding.tk/test/post.txt

你將要做的是收集所有傳入的$_POST文件,然後你就會知道發佈的數據是什麼,這將澄清錯誤的位置,在Android端或PHP端如果發佈是好的,那麼android代碼是好的,問題是在PHP代碼中。

我希望它會幫助你修復問題...

感謝所有對你的答案