Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. import java.io.BufferedOutputStream;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.net.Socket;
  10. import java.nio.ByteBuffer;
  11.  
  12. import javax.print.DocFlavor.BYTE_ARRAY;
  13.  
  14. import org.apache.lucene.document.Document;
  15. import org.apache.lucene.queryParser.ParseException;
  16. import org.apache.lucene.search.ScoreDoc;
  17. import org.apache.lucene.search.TopDocs;
  18.  
  19.  
  20. public class Server extends Thread
  21. {
  22. private Socket socket;
  23. private long fileCount=0;
  24. private Indexer indexer;
  25. private String dataDir="E:\repository", indexDir="indexDir";
  26. private Searcher searcher;
  27.  
  28. public Server(Socket ClientSocket)
  29. {
  30. socket=ClientSocket;
  31. boolean f=new File(indexDir).mkdir();
  32. try
  33. {
  34. indexer = new Indexer(indexDir);
  35. int numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
  36. indexer.close();
  37. }
  38. catch (IOException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. }
  43. public void run()
  44. {
  45. try
  46. {
  47. // ServerSocket s=new ServerSocket(10);
  48. // Socket sock=s.accept();
  49. InputStream is=socket.getInputStream();
  50. OutputStream outs=socket.getOutputStream();
  51. DataInputStream fins=new DataInputStream(is);
  52. DataOutputStream fouts=new DataOutputStream (outs);
  53.  
  54. String username=fins.readUTF();
  55. String password=fins.readUTF();
  56.  
  57. fouts.writeBoolean(true);
  58. String directive=null;
  59. System.out.println(directive=fins.readUTF());
  60. while(!directive.equalsIgnoreCase("exit"))
  61. {
  62. if(directive.equalsIgnoreCase("upload"))
  63. {
  64. // long fileSize=fins.readLong();
  65. String filename=fins.readUTF();
  66. File file=new File("E:\repository\"+filename);
  67. BufferedOutputStream writer=new BufferedOutputStream(new FileOutputStream(file));
  68. int count=fins.readInt();
  69. byte[] buffer=new byte[512];
  70. while(count!=-1)
  71. {
  72. System.out.println(count);
  73. count=fins.readInt();
  74. if(count==-1)
  75. break;
  76. fins.read(buffer);
  77. for(int i=0;i<512;i++)
  78. System.out.println("item "+i+" in buffer is "+Byte.toString(buffer[i]));
  79. writer.write(buffer);
  80. }
  81.  
  82. writer.flush();
  83. System.out.println("file written");
  84. writer.close();
  85. System.out.println("file recieved");
  86. fileCount++;
  87. }
  88. if(directive.equalsIgnoreCase("search"))
  89. {
  90. String fileNameToSearch=fins.readUTF();
  91. System.out.println(fileNameToSearch);
  92. searcher = new Searcher(indexDir);
  93. TopDocs hits = searcher.search(fileNameToSearch);
  94. System.out.println(hits.totalHits);
  95. for(ScoreDoc scoreDoc : hits.scoreDocs) {
  96. Document doc = searcher.getDocument(scoreDoc);
  97. System.out.println("File: "
  98. + doc.get(LuceneConstants.FILE_PATH));
  99. }
  100. searcher.close();
  101. System.out.println("Search finished");
  102. }
  103. directive=fins.readUTF();
  104. }
  105.  
  106. fins.close();
  107. fouts.close();
  108. is.close();
  109. outs.close();
  110. socket.close();
  111. }
  112. catch (IOException e)
  113. {
  114. // TODO Auto-generated catch block
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119. }
  120.  
  121. import java.io.File;
  122. import java.io.IOException;
  123.  
  124. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  125. import org.apache.lucene.document.Document;
  126. import org.apache.lucene.index.CorruptIndexException;
  127. import org.apache.lucene.queryParser.ParseException;
  128. import org.apache.lucene.queryParser.QueryParser;
  129. import org.apache.lucene.search.IndexSearcher;
  130. import org.apache.lucene.search.Query;
  131. import org.apache.lucene.search.ScoreDoc;
  132. import org.apache.lucene.search.TopDocs;
  133. import org.apache.lucene.store.Directory;
  134. import org.apache.lucene.store.FSDirectory;
  135. import org.apache.lucene.util.Version;
  136.  
  137. public class Searcher {
  138.  
  139. IndexSearcher indexSearcher;
  140. QueryParser queryParser;
  141. Query query;
  142.  
  143. public Searcher(String indexDirectoryPath)
  144. throws IOException{
  145. Directory indexDirectory =
  146. FSDirectory.open(new File(indexDirectoryPath));
  147. indexSearcher = new IndexSearcher(indexDirectory);
  148. queryParser = new QueryParser(Version.LUCENE_36,
  149. LuceneConstants.FILE_NAME,
  150. new StandardAnalyzer(Version.LUCENE_36));
  151. }
  152.  
  153. public TopDocs search( String searchQuery) {
  154. try {
  155. query = queryParser.parse(searchQuery);
  156. return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
  157. } catch (ParseException e) {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. } catch (IOException e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. }
  164. return null;
  165.  
  166. }
  167.  
  168. public Document getDocument(ScoreDoc scoreDoc)
  169. throws CorruptIndexException, IOException{
  170. return indexSearcher.doc(scoreDoc.doc);
  171. }
  172.  
  173. public void close() throws IOException{
  174. indexSearcher.close();
  175. }
  176. }
  177.  
  178. import java.io.File;
  179. import java.io.FileFilter;
  180. import java.io.FileReader;
  181. import java.io.IOException;
  182.  
  183. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  184. import org.apache.lucene.document.Document;
  185. import org.apache.lucene.document.Field;
  186. import org.apache.lucene.index.CorruptIndexException;
  187. import org.apache.lucene.index.IndexWriter;
  188. import org.apache.lucene.store.Directory;
  189. import org.apache.lucene.store.FSDirectory;
  190. import org.apache.lucene.util.Version;
  191.  
  192. public class Indexer {
  193.  
  194. private IndexWriter writer;
  195.  
  196. @SuppressWarnings("deprecation")
  197. public Indexer(String indexDirectoryPath) throws IOException{
  198. //this directory will contain the indexes
  199. Directory indexDirectory =
  200. FSDirectory.open(new File(indexDirectoryPath));
  201.  
  202. //create the indexer
  203. writer = new IndexWriter(indexDirectory,
  204. new StandardAnalyzer(Version.LUCENE_36),true,
  205. IndexWriter.MaxFieldLength.UNLIMITED);
  206. }
  207.  
  208. public void close() throws CorruptIndexException, IOException{
  209. writer.close();
  210. }
  211.  
  212. private Document getDocument(File file) throws IOException{
  213. Document document = new Document();
  214.  
  215. //index file contents
  216. Field contentField = new Field(LuceneConstants.CONTENTS,
  217. new FileReader(file));
  218. //index file name
  219. Field fileNameField = new Field(LuceneConstants.FILE_NAME,
  220. file.getName().toString().trim(),
  221. Field.Store.YES,Field.Index.NOT_ANALYZED);
  222. //index file path
  223. Field filePathField = new Field(LuceneConstants.FILE_PATH,
  224. file.getCanonicalPath(),
  225. Field.Store.YES,Field.Index.NOT_ANALYZED);
  226.  
  227. document.add(contentField);
  228. document.add(fileNameField);
  229. document.add(filePathField);
  230.  
  231. return document;
  232. }
  233.  
  234. private void indexFile(File file) throws IOException{
  235. System.out.println("Indexing "+file.getCanonicalPath());
  236. Document document = getDocument(file);
  237. writer.addDocument(document);
  238. }
  239.  
  240. public int createIndex(String dataDirPath, FileFilter filter)
  241. throws IOException{
  242. //get all files in the data directory
  243. File[] files = new File(dataDirPath).listFiles();
  244.  
  245. for (File file : files) {
  246. if(!file.isDirectory()
  247. && !file.isHidden()
  248. && file.exists()
  249. && file.canRead()
  250. && filter.accept(file)
  251. ){
  252. indexFile(file);
  253. }
  254. }
  255. return writer.numDocs();
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement