我在这个论坛上搜索了这个问题,发现了很多答案,但是他没有一个答案可以解决我的问题
我想通过Java连接到Solr应用程序(版本6.1.0),但它给了我错误。
我的代码是:

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
public class solrConnection {
    public void solrConnectionTest()  {
    try{
    String baseURL = "http://"IP-Address":8983/solr/#/test1/test2";
    @SuppressWarnings("deprecation")
    SolrClient solr = new HttpSolrClient(baseURL);
    SolrQuery query = new SolrQuery(":");
    query=new SolrQuery();
    query.set("qt", "/select");
    query.set("q", "sample test");
    query.set("wt", "json");
    QueryResponse response = solr.query(query);
    System.out.println("Response is : "+response);
    System.out.println("Program executed successfully");
    }catch(Exception e){
        e.printStackTrace();
    }
    }
}

Error is:


org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://"IP-Address":8983/solr/#/test1/test2: Expected mime type application/octet-stream but got text/html. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<head>

  <title>Solr Admin</title>

  <link rel="icon" type="image/ico" href="img/favicon.ico?_=4.8.1">

  <link rel="stylesheet" type="text/css" href="css/styles/common.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/analysis.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/cloud.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/cores.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/dashboard.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/dataimport.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/files.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/index.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/java-properties.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/logging.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/menu.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/plugins.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/documents.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/query.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/replication.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/schema-browser.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/styles/threads.css?_=4.8.1">
  <link rel="stylesheet" type="text/css" href="css/chosen.css?_=4.8.1">

  <meta http-equiv="x-ua-compatible" content="IE=9">

  <script type="text/javascript">

  var app_config = {};

  app_config.solr_path = '\/solr';
  app_config.core_admin_path = '\/admin\/cores';

  </script>

</head>
<body>

  <div id="wrapper">

    <div id="header">

      <a href="./" id="solr"><span>Apache SOLR</span></a>

      <p id="environment">&nbsp;</p>

    </div>

    <div id="main" class="clearfix">

      <div id="init-failures">

          <h2>SolrCore Initialization Failures</h2>
          <ul></ul>
          <p>Please check your logs for more information</p>

      </div>

      <div id="content-wrapper">
        <div id="content">

          &nbsp;

        </div>
      </div>

      <div id="menu-wrapper">
        <div>

          <ul id="menu">

            <li id="index" class="global"><p><a href="#/">Dashboard</a></p></li>

            <li id="logging" class="global"><p><a href="#/~logging">Logging</a></p>
              <ul>
                <li class="level"><a href="#/~logging/level">Level</a></li>
              </ul>
            </li>

            <li id="cloud" class="global optional"><p><a href="#/~cloud">Cloud</a></p>
              <ul>
                <li class="tree"><a href="#/~cloud?view=tree">Tree</a></li>
                <li class="graph"><a href="#/~cloud">Graph</a></li>
                <li class="rgraph"><a href="#/~cloud?view=rgraph">Graph (Radial)</a></li>
                <li class="dump"><a href="#/~cloud">Dump</a></li>
              </ul>
            </li>

            <li id="cores" class="global"><p><a href="#/~cores">Core Admin</a></p></li>

            <li id="java-properties" class="global"><p><a href="#/~java-properties">Java Properties</a></li>

            <li id="threads" class="global"><p><a href="#/~threads">Thread Dump</a></p></li>

          </ul>

          <div id="core-selector">
            <div id="has-cores">
              <select data-placeholder="Core Selector"></select>
            </div>
            <p id="has-no-cores"><a href="#/~cores">
              No cores available
              <span>Go and create one</span>
            </a></p>
          </div>
          <div id="core-menu">
            <ul></ul>
          </div>

        </div>
      </div>

      <div id="meta">

        <ul>

          <li class="documentation"><a href="http://lucene.apache.org/solr/"><span>Documentation</span></a></li>
          <li class="issues"><a href="http://issues.apache.org/jira/browse/SOLR"><span>Issue Tracker</span></a></li>
          <li class="irc"><a href="http://webchat.freenode.net/?channels=#solr"><span>IRC Channel</span></a></li>
          <li class="mailinglist"><a href="http://wiki.apache.org/solr/UsingMailingLists"><span>Community forum</span></a></li>
          <li class="wiki-query-syntax"><a href="http://wiki.apache.org/solr/SolrQuerySyntax"><span>Solr Query Syntax</span></a></li>

        </ul>

      </div>

    </div>

  </div>

  <div id="connection_status">

    <span>Connection lost &hellip;</span>

  </div>

  <script type="text/javascript"> var require = { urlArgs: '_=4.8.1' }; </script>
  <script src="js/require.js?_=4.8.1" data-main="js/main"></script>

</body>
</html>

    at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:558)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:259)
    at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:248)
    at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
    at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:942)
    at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:957)
    at roughwork.solrConnection.solrConnectionTest(solrConnection.java:36)
    at roughwork.solrConnection.main(solrConnection.java:47)

最佳答案

我现在可以使用HttpURLConnection连接到Solr。下面是我的代码,它正在成功运行。

InputFileDeclared input = new InputFileDeclared();
String url = "My URL";
private void sendGet() throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print result
        String data = response.toString();
        System.out.println(response.toString());
}

09-28 04:18