我剛開始使用SOLR。我想索引一些HTML頁面,並從文檔中得到這個:在Java中用於SOLR的捲曲等效POST
curl "http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true" -F "[email protected]/home/binaryplease/workspace/SOLRTest/HTMLPages/hello2.html"
這與查詢返回預計結果的預期一樣工作。
我該如何在java應用程序中完成這個確切的POST?
我嘗試這樣做,因爲我不知道如何與HttpClient的做,但它不工作:
String command = "curl \"http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true\" -F \"[email protected]\"" +f.getAbsoluteFile() + "\"";
try {
proc = Runtime.getRuntime().exec(command);
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();
System.out.println("Inputstream " + getStringFromInputStream(in));
System.out.println("Errorstream " + getStringFromInputStream(err));
} catch (IOException e) {
e.printStackTrace();
}
什麼是正確的做法指數SOLR一個html文件,並用java做一個查詢? 我將不勝感激一個例子。
編輯:
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("myfile", "@/home/binaryplease/workspace/SOLRTest/HTMLPages/hello3.html"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
System.out.println("Content " + getStringFromInputStream(instream));
} finally {
instream.close();
}
}
}
我在做什麼錯了:我仍然不能正常工作,現在得到這個?
你是否使用了「在java中發送http post」一詞?它可能會導致你[這個StackOverflow問題](http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java) –
@RayToal看到我的編輯。 – user1759796
當你說「不工作」時,你是什麼意思? - 你有錯誤嗎?或者只是沒有看到預期的結果?有沒有可以提供的日誌?你能調試,看看是否有任何異常拋出?對我們來說,理解沒有具體問題的完整問題是一個挑戰。 –