2014-02-19 82 views
-1

我正嘗試將映像上傳到服務器。上傳到服務器後的空映像文件

這裏是我的Android寫的代碼到圖像文件發送與其他一些參數到服務器一起:

static String imagePath = "/storage/sdcard0/Pictures/image.jpg"; 
static String url = "http://example.com/api"; 
static String user_id = "99401"; 

public static void executeMultipartPost() throws IOException, ClientProtocolException { 

    HttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    HttpPost httppost = new HttpPost(url); 
    File file = new File(imagePath); 
    FileBody fb = new FileBody(file); 

    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 

    builder.addPart("user_id", new StringBody(user_id)); 
    builder.addPart("type", new StringBody("single")); 
    builder.addPart("userfile", fb); 
    final HttpEntity entity = builder.build(); 


    httppost.setEntity(entity); 

    Log.i(TAG, "Executing request: " + httppost.getRequestLine()); 

    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity resEntity = response.getEntity(); 
    Log.i(TAG, ""+response.getStatusLine()); 
    if (resEntity != null) { 
     Log.i(TAG, EntityUtils.toString(resEntity)); 
    } 
    if (resEntity != null) { 
     resEntity.consumeContent(); 
    } 
    httpclient.getConnectionManager().shutdown(); 
} 

一切正常,當它上傳到服務器,除了,服務器接收的空的圖像。我只能看到一個0字節的圖像文件。我猜想客戶端代碼中的某些內容沒有正確設置。

我在logcat中得到執行代碼後的反應是這樣的,(從Laravel錯誤日誌):

<html><h2>Unhandled Exception</h2> 
    <h3>Message:</h3> 
    <pre>copy(): The first argument to copy() function cannot be a directory</pre> 
    <h3>Location:</h3> 
    <pre>/home/ked.ai/www/laravel/file.php on line 92</pre> 
    <h3>Stack Trace:</h3> 
    <pre>#0 /home/ked.ai/www/laravel/laravel.php(42): Laravel\Error::native(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92) 
    #1 [internal function]: Laravel\{closure}(2, 'copy(): The fir...', '/home/ked.ai/ww...', 92, Array) 
    #2 /home/ked.ai/www/laravel/file.php(92): copy('/', '/home/ked.ai/ww...') 
    #3 /home/ked.ai/www/application/controllers/api2/item.php(190): Laravel\File::copy('/', '/home/ked.ai/ww...') 
    #4 [internal function]: Api2_Item_Controller->post_new() 
    #5 /home/ked.ai/www/laravel/routing/controller.php(325): call_user_func_array(Array, Array) 
    #6 /home/ked.ai/www/laravel/routing/controller.php(285): Laravel\Routing\Controller->response('new', Array) 
    #7 /home/ked.ai/www/laravel/routing/controller.php(165): Laravel\Routing\Controller->execute('new', Array) 
    #8 /home/ked.ai/www/laravel/routing/route.php(153): Laravel\Routing\Controller::call('[email protected]', Array) 
    #9 /home/ked.ai/www/laravel/routing/route.php(124): Laravel\Routing\Route->response() 
    #10 /home/ked.ai/www/laravel/laravel.php(167): Laravel\Routing\Route->call() 
    #11 /home/ked.ai/www/public/index.php(34): require('/home/ked.ai/ww...') 
    #12 {main}</pre></html> 

好像它傳遞一個目錄而不是文件的。任何解決方法?

+0

使用FTP將圖像上傳到服務器 – suresh

+1

否。Laravel是服務器部分。我爲我的誤會道歉。我只想使用API​​網址從android設備提交我的圖片。 – AimanB

+0

我正面臨着一個同樣的問題。我的問題是,當我編碼圖像到基地64和發送到服務器使用改造它顯示灰色圖像,但是當我編碼相同的基本64字符串和轉換爲位圖使用位圖inimage它完美地顯示圖像。不能弄清楚這個問題你能幫助我嗎? –

回答

0

我已經設法找到解決方案。

我只需要這一行

builder.addPart("userfile", fb); 

更改爲

builder.addPart("userfile[]", fb); 

因爲在服務器端,$_FILES['userfile']接收陣列型。

謝謝。

0

您可以使用此方法將圖像傳遞給php服務器。

  1. 使用Base64.encodeBytes(byte_data)將圖像轉換爲base64字符串。
  2. 在服務器端php代碼通過base64_decode(str)將base64字符串轉換爲圖像。
+0

服務器使用'$ _FILE'獲取文件。那麼你認爲有必要將圖像轉換爲base64嗎? Furthermor,我不是後端工程師,所以我沒有權力編輯它:( – AimanB

+0

是圖像路徑是否正確? –

+0

是的,圖像路徑是正確的。如果我追加'file://'到路徑,它無法找到 – AimanB

0

其工作代碼:

public void executeMultipartPost() throws Exception { 

     try { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bm.compress(CompressFormat.JPEG, 75, bos); 
      byte[] data = bos.toByteArray(); 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost postRequest = new HttpPost(url_path); 
      ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg"); 
      // File file= new File("/mnt/sdcard/forest.png"); 
      // FileBody bin = new FileBody(file); 
      MultipartEntity reqEntity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE); 

      reqEntity.addPart("file", bab); 
      reqEntity.addPart("category ", new StringBody("1")); 
      reqEntity.addPart("user_id ", new StringBody("55")); 

      postRequest.setEntity(reqEntity); 

      HttpResponse response = httpClient.execute(postRequest); 

      String my_response = convertStreamToString(response.getEntity() 
        .getContent()); 
      Toast.makeText(getApplicationContext(), my_response, 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

public static String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

試試吧。

+0

仍然一樣。端使用'$ _FILE ['userfile']'從客戶端獲取'userfile'參數。發送ByteArray有什麼問題。 – AimanB