2011-02-18 54 views
0

我一直在尋找這最後一兩天,似乎無法找到解決我的問題。我正在嘗試使用httppost將映像發佈到服務器。 我試圖這樣做的兩種方式,並完成雙方的職位,但沒有內容,即內容長度爲0 首先是如下:Android - 圖片上傳不發送內容

String url = "MYURL"; 
     HttpClient httpClient = new DefaultHttpClient(); 

    try { 
      httpClient.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second 
      HttpPost post = new HttpPost(url); 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      File file = new File(SDCardRoot,"/DCIM/100MSDCF/DSC00004.jpg"); 
      FileEntity entity; 
      entity = new FileEntity(file,"binary/octet-stream"); 
      entity.setChunked(true); 
      post.setEntity(entity); 
      post.addHeader("Header", "UniqueName"); 
      HttpResponse response = httpClient.execute(post); 
      if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { 
        Log.e("Here","--------Error--------Response Status line code:"+response.getStatusLine()); 
      }else { 
        // Here every thing is fine. 
      } 

      HttpEntity resEntity = response.getEntity(); 
      if (resEntity == null) { 
       Log.e("Here","---------Error No Response!!-----"); 
      } 
    } catch (Exception ex) { 
      Log.e("Here","---------Error-----"+ex.getMessage()); 
      ex.printStackTrace(); 
    } finally { 
       httpClient.getConnectionManager().shutdown(); 
    } 

,第二個是:

String url = "MYURL"; 
     //File SDCardRoot = Environment.getExternalStorageDirectory(); 
     File file = new File(Environment.getExternalStorageDirectory(),"/DCIM/100MSDCF/DSC00004.jpg"); 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1); 
      reqEntity.setContentType("binary/octet-stream"); 
      reqEntity.setChunked(true); 
      // Send in multiple parts if needed 
      httppost.setEntity(reqEntity); 
      HttpResponse response = httpclient.execute(httppost); 
      Log.d("finishing", "The try catch function"); 
     } catch (Exception e) { 
      // show error 
     }*/ 

正如你所看到的,我已經硬編碼了一個特定圖像的路徑,當它啓動並運行時,這是動態的。 任何人都可以看到我做錯了什麼?我是否遺漏了一些東西?我知道我使用setChunked和setContenttype - 是否有setContent選項? 任何幫助將不勝感激。

謝謝, jr83。

回答

1

您可以使用通過發送多部分信息來上傳圖像;您可能會發現this討論有用。