【发布时间】:2014-07-06 00:58:14
【问题描述】:
我刚开始使用 SOLR。我想索引一些 html 页面并从文档中获得:
curl "http://localhost:8983/solr/update/extract?literal.id=doc1&commit=true" -F "myfile=@/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 \"myfile=@\"" +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帖子”这个短语吗?它可能会将您带到this StackOverflow question
-
@RayToal 查看我的编辑。
-
你说“不工作”是什么意思 - 你有错误吗?或者只是没有看到预期的结果?有没有可以提供的日志?能不能调试一下看看有没有抛出异常?在没有细节的情况下理解整个问题对我们来说是一个挑战。
-
嗯,没有特别的错误,我得到一个 200 响应,但文件没有被索引。如果我查询 html 文件中出现的字符串,我不会得到任何结果。