2014-04-07 46 views
3

我試圖上傳一個文件,以及一些變量到一個web服務。從我在網上閱讀的內容中,MultipartEntityBuilder將成爲這項工作的工具。但是,當我以描述的方式使用它時,post變量(包括文本和文件)似乎不會與查詢一起發送。接收端反應就像沒有附加變量後綴一樣。MultipartEntityBuilder不發送帖子變量

我已經嘗試通過用下面的代碼文件指向的URL測試理論:

<?php  
$data = "";  
foreach ($_POST as $key => $value) { 
    $data = $data . "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>"; 
} 
echo $data;  
?> 

,並且調用返回沒有的數據(並沒有當我使用過賬等方法返回的數據

是否有任何想法,可能是什麼問題?

The code in question: 

     String url = "http://aktivthospital.dk.web1.aktivthospital.dk/index.php"; 

     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     httpPost.addHeader("Content-Type", "multipart/form-data"); 

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

     ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8); 
     entityBuilder.addPart("option", new StringBody("com_redtournament", contentType)); 
     entityBuilder.addPart("task", new StringBody("competition.apiServiceUploadImage", contentType)); 
     entityBuilder.addPart("username", new StringBody(_username, contentType)); 
     entityBuilder.addPart("password", new StringBody(_password, contentType)); 
     entityBuilder.addPart("comp_id", new StringBody(String.valueOf(_competitionId), contentType)); 
     entityBuilder.addPart("img_title", new StringBody(_title, contentType)); 
     File file = new File(_imagepath); 
     entityBuilder.addPart("img_file",new FileBody(file)); 
     httpPost.setEntity(entityBuilder.build()); 

     HttpResponse response = httpClient.execute(httpPost); 
+0

你有很多零件。你不想使用1部分的URL編碼對,1的文件? – njzk2

+0

我需要發送所有的東西作爲一個電話。否則,Web服務將不知道如何處理該文件。是否有可能分裂它,並仍然是一次呼叫? – Rubberduck

+0

不是我在說什麼。你正在製作一個包含很多零件的多部件。通常,有2個部分,而不是7.1與參數名稱值對,一個與文件。 – njzk2

回答

2

所以,我好不容易纔得到的東西的工作,我不知道如果你能做到相同的,但值得注意的是鄰thers。爲了背景,我正在試圖張貼給詹金斯。

無論我多麼努力地嘗試,我只能將文件添加到MultipartEntityBuilder。是什麼讓事情變得更糟(可能)是因爲我太懶惰/自大以改變我處理POST的方式,所以我必須找出一種方法,使其儘可能少地更改代碼並進行重寫。

我試圖 .addPart(name, new StringBody(value, contentType)).addTextBody(name, value)

我加入我的參數作爲JSON發揮各地,但也不能工作。最後,我記得最初有什麼參數可以工作。我最終決定了一些不同的東西,但是我能夠將這個最初的成功與MultipartEntityBuilder結合起來,取得圓滿成功。

如果我的POST有文件,我使用MultipartEntityBuilder並添加那些像我以前一樣。然而,對於參數,我將它們添加到URI中。因此,舉例來說,什麼工作對我來說是這樣的:

http://jenkins.url/.../buildWIthParameters?firstname=firstvalue&secondname=secondvalue

而且還設置了實體擁有的文件。兩者的結合起作用。

現在,我知道我們有完全獨立的,無關的項目,但我認爲這是值得記錄我的成功的,如果沒有其他原因,除了給你別的東西去嘗試。

2

這裏的工作方法(機器人):

public void post(final String url, 
       final File file, 
       final String fileName, 
       final Map<String, String> params, 
       final FileUploadCallback callback) { 
    ExecutorService executorService = Executors.newCachedThreadPool(); 
    executorService.execute(new Runnable() { 
     @Override 
     public void run() { 
      HttpPost httpPost = new HttpPost(url); 

      MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
      if (null != params && params.size() > 0) { 
       for (String key : params.keySet()) { 
        builder.addTextBody(key, params.get(key), ContentType.TEXT_PLAIN); 
       } 
      } 

      if (null != file && file.exists()) { 
       builder.addBinaryBody(fileName, file, ContentType.create("image/jpg"), fileName); 
      } 

      final HttpEntity entity = builder.build(); 
      httpPost.setEntity(entity); 

      int statusCode = -1; 
      String responseBody; 

      try { 
       HttpResponse httpResponse = mHttpClient.execute(httpPost); 

       StatusLine status = httpResponse.getStatusLine(); 
       statusCode = status.getStatusCode(); 
       responseBody = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); 

       if (statusCode >= 300) { 
        sendFailure(callback, statusCode, responseBody, null); 
       } else { 
        sendSuccess(callback, statusCode, responseBody); 
       } 
      } catch (IOException e) { 
       sendFailure(callback, statusCode, null, e); 
      } 
     } 
    }); 
} 
+0

J2EE Java8:'addPart()'不起作用(正如所料),而'addTextBody()'yes(也如預期的那樣)。 – peterh