2013-11-21 2 views
0

이 CSV 데이터를 SOLR로 보내려고합니다.HTTP에서 CSV 파일 보내기

id,author,name,year,category 
001-001,H. G. Wells,The Time Machine,1895,Fiction 
001-002,H. G. Wells,The Wonderful Visit,1895,Fiction 

여기 내 코드가 작동하지 않습니다.

private static final String postURL = "http://localhost:8983/solr/update/csv?separator=,&commit=true"; 

    public static void main(String[] args) throws IOException { 
    BookCsvIndexer poster = new BookCsvIndexer(); 
    BufferedReader dbr = new BufferedReader(new FileReader("E:/solr-example/csv/books.csv")); 
    String line; 
    StringBuilder dsb = new StringBuilder(); 
    while ((line = dbr.readLine()) != null) { 
     dsb.append(line); 
    } 
    dbr.close(); 
    String data = dsb.toString(); 
    System.out.println(data); 
    poster.doPost(data); 
    } 

    public void doPost(String data) { 
    String contentLength = Integer.toString(data.getBytes().length); 
    System.out.println("Length=" + contentLength); 
    StringBuilder sb = new StringBuilder(); 
    try { 
     URL url = new URL(postURL); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setDoInput(true); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Accept", "text/plain"); 
     connection.setRequestProperty("Connection", "close"); 
     connection.setRequestProperty("Content-Type", "application/csv; charset=utf-8"); 
     connection.setRequestProperty("Content-Length", "" + contentLength); 
     connection.connect(); 

     if (data.length() > 0) { 
     OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); 
     wr.write(data); 
     wr.flush(); 
     wr.close(); 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream()))); 
     String output; 
     System.out.println("Server Respose CODE=" + connection.getResponseCode()); 
     while ((output = br.readLine()) != null) { 
     sb.append(output); 
     } 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    System.out.println(sb.toString()); 
    } 

'Content-Type'을 변경하여 JSON 및 기타 텍스트 파일을 보내는데이 코드를 사용할 수 있습니다. 나는이 코드를 사용하여 많은 REST 서비스를 사용하고있다. 이 코드가 SOLR로 CSV를 전송하는 데 왜 작동하지 않는지 이해할 수 없습니다.

다음은 위 코드를 실행 한 후 SOLR 로그입니다.

INFO - 2013-11-21 11:20:09.090; org.apache.solr.update.DirectUpdateHandler2; start commit{,optimize=false,openSearcher=true,waitSearcher=true,expungeDeletes=false,softCommit=false,prepareCommit=false} 
INFO - 2013-11-21 11:20:09.093; org.apache.solr.core.SolrDeletionPolicy; SolrDeletionPolicy.onInit: commits: num=1 
    commit{dir=NRTCachingDirectory([email protected]:\solr-example\solr\collection1\data\index [email protected]; maxCacheMB=48.0 maxMergeSizeMB=4.0),segFN=segments_1,generation=1} 
INFO - 2013-11-21 11:20:09.093; org.apache.solr.core.SolrDeletionPolicy; newest commit generation = 1 
INFO - 2013-11-21 11:20:09.094; org.apache.solr.update.DirectUpdateHandler2; No uncommitted changes. Skipping IW.commit. 
INFO - 2013-11-21 11:20:09.094; org.apache.solr.search.SolrIndexSearcher; Opening [email protected] main 
INFO - 2013-11-21 11:20:09.095; org.apache.solr.update.DirectUpdateHandler2; end_commit_flush 
INFO - 2013-11-21 11:20:09.095; org.apache.solr.core.QuerySenderListener; QuerySenderListener sending requests to [email protected] main{StandardDirectoryReader(segments_1:1:nrt)} 
INFO - 2013-11-21 11:20:09.096; org.apache.solr.core.QuerySenderListener; QuerySenderListener done. 
INFO - 2013-11-21 11:20:09.096; org.apache.solr.handler.component.SpellCheckComponent$SpellCheckerListener; Building spell index for spell checker: suggest 
INFO - 2013-11-21 11:20:09.096; org.apache.solr.spelling.suggest.Suggester; build() 
INFO - 2013-11-21 11:20:09.099; org.apache.solr.core.SolrCore; [collection1] Registered new searcher [email protected] main{StandardDirectoryReader(segments_1:1:nrt)} 
INFO - 2013-11-21 11:20:09.100; org.apache.solr.update.processor.LogUpdateProcessor; [collection1] webapp=/solr path=/update/csv params={commit=true&separator=,} {commit=} 0 17 

그리고 마지막 줄 System.out.println(sb.toString());의 응답

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <lst name="responseHeader"> 
    <int name="status">0</int> 
    <int name="QTime">17</int></lst> 
</response> 

의 URL 내가 성공적으로 POSTMAN, REST를위한 크롬 확장 프로그램과 같은 REST 클라이언트 소프트웨어를 사용하여이 CSV 데이터를 게시 할 수 있기 때문에 CSV 파일이 올바른지 고객. 이제 Java를 사용하여 동일한 작업을 수행하려고합니다.

내가 누락 된 항목이 있습니까?

+0

당신이 "작동하지 않습니다"무엇을 의미합니까? 너는 무엇이 잘못되었는지 말하지 않았다. –

+0

solrconfig.xml에서 요청 처리기를 추가 하시겠습니까? 그리고 @JimGarrison이 말한 것, 로그의 예외 또는 무엇이 잘못 될까요? – cheffe

+0

@JimGarrison 오류 메시지는 없지만 아무것도 SOLR로 보내지 않습니다. – Mawia

답변

0

아파치 평민 HttpClient를 사용하여 서버에 파일을 게시 할 수 있습니다.

File file = new File("file-path"); 
MultipartPostMethod method = new MultipartPostMethod(post-file-url); 
method.addParameter("fileName", file.getName()); 
method.addPart(new FilePart("file", "file", file)); 
httpClient.executeMethod(method); 
String response = method.getResponseBodyAsString(); 

HttpClient를 의존 :

<dependency> 
    <groupId>commons-httpclient</groupId> 
    <artifactId>commons-httpclient</artifactId> 
    <version>3.1</version> 
</dependency> 
+0

"HttpClient"및이 패키지의 다른 도움 클래스가 사용되지 않습니다. – agilob