2012-06-09 30 views
1

HttpClient的,httpmime 4.1.3上傳文件到Solr與HttpClient的和MultipartEntity

我試圖通過http上傳文件到沒有成功的遠程服務器。

這裏是我的代碼:

HttpPost method; 
    method = new HttpPost(solrUrl + "/extract"); 
    method.getParams().setParameter("literal.id", fileId); 
    method.getParams().setBooleanParameter("commit", true); 

    MultipartEntity me = new MultipartEntity(); 
    me.addPart("myfile", new InputStreamBody(doubleInput, contentType, fileId)); 

    method.setEntity(me); 
    //method.setHeader("Content-Type", "multipart/form-data"); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpResponse hr = httpClient.execute(method); 

服務器是Solr的。

這是爲了取代調用curl這樣的工作bash腳本,

curl http://localhost:8080/solr/update/extract?literal.id=bububu&commit=true -F [email protected]

如果我嘗試設置「內容類型」,「多/表單數據」,接收部分說,有沒有邊界(這是真的):

HTTP Status 500 - the request was rejected because no multipart boundary was found

如果我忽略這個頭設置,服務器將發出錯誤說明的是,據我發現,表明該合作ntent類型不是多部分[2]:

HTTP Status 400. The request sent by the client was syntactically incorrect ([doc=null] missing required field: id).

這與[1],但我不能確定與此問題的答案。我想知道,

我處於同樣的情況,但不知道該怎麼做。我希望MultipartEntity能夠告訴HttpPost對象它是多部分的,形成數據並且有一些邊界,我不會自己設置內容類型。我不太清楚如何爲實體提供邊界 - MultipartEntity沒有像setBoundary這樣的方法。或者說,如何獲取隨機生成的邊界由自己指定它的addHeader中 - 沒有getBoundary methor要麼...

[1] Problem with setting header "Content-Type" in uploading file with HttpClient4

[2] http://lucene.472066.n3.nabble.com/Updating-the-index-with-a-csv-file-td490013.html

回答

1

我懷疑

method.getParams().setParameter("literal.id", fileId); 
method.getParams().setBooleanParameter("commit", true); 

在第一線,是fileId字符串或文件指針(或別的東西)?我希望這是一個字符串。至於第二行,你可以設置一個正常的參數。我想要解決HTTP Status 400。我不知道太多的Java(或者是淨?)

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_Error

+0

1)它是一個字符串,2)嘗試,要麼。這是Java和Apache HttpClient – fedd

+0

你能打印正在生成的請求字符串嗎? – aitchnyu

+0

你完全正確的問題位置。這是我如何設置參數。它們應該位於URL中,不能設置爲POST或Multipart參數。 所以我這樣做, 'fileId = URLEncoder.encode(fileId,「UTF-8」); method = new HttpPost(solrUrl +「/update/extract?literal.id =」+ fileId +「&commit = true」);' – fedd

相關問題