Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.nio.ByteBuffer;
- import javax.print.DocFlavor.BYTE_ARRAY;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.search.ScoreDoc;
- import org.apache.lucene.search.TopDocs;
- public class Server extends Thread
- {
- private Socket socket;
- private long fileCount=0;
- private Indexer indexer;
- private String dataDir="E:\repository", indexDir="indexDir";
- private Searcher searcher;
- public Server(Socket ClientSocket)
- {
- socket=ClientSocket;
- boolean f=new File(indexDir).mkdir();
- try
- {
- indexer = new Indexer(indexDir);
- int numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
- indexer.close();
- }
- catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void run()
- {
- try
- {
- // ServerSocket s=new ServerSocket(10);
- // Socket sock=s.accept();
- InputStream is=socket.getInputStream();
- OutputStream outs=socket.getOutputStream();
- DataInputStream fins=new DataInputStream(is);
- DataOutputStream fouts=new DataOutputStream (outs);
- String username=fins.readUTF();
- String password=fins.readUTF();
- fouts.writeBoolean(true);
- String directive=null;
- System.out.println(directive=fins.readUTF());
- while(!directive.equalsIgnoreCase("exit"))
- {
- if(directive.equalsIgnoreCase("upload"))
- {
- // long fileSize=fins.readLong();
- String filename=fins.readUTF();
- File file=new File("E:\repository\"+filename);
- BufferedOutputStream writer=new BufferedOutputStream(new FileOutputStream(file));
- int count=fins.readInt();
- byte[] buffer=new byte[512];
- while(count!=-1)
- {
- System.out.println(count);
- count=fins.readInt();
- if(count==-1)
- break;
- fins.read(buffer);
- for(int i=0;i<512;i++)
- System.out.println("item "+i+" in buffer is "+Byte.toString(buffer[i]));
- writer.write(buffer);
- }
- writer.flush();
- System.out.println("file written");
- writer.close();
- System.out.println("file recieved");
- fileCount++;
- }
- if(directive.equalsIgnoreCase("search"))
- {
- String fileNameToSearch=fins.readUTF();
- System.out.println(fileNameToSearch);
- searcher = new Searcher(indexDir);
- TopDocs hits = searcher.search(fileNameToSearch);
- System.out.println(hits.totalHits);
- for(ScoreDoc scoreDoc : hits.scoreDocs) {
- Document doc = searcher.getDocument(scoreDoc);
- System.out.println("File: "
- + doc.get(LuceneConstants.FILE_PATH));
- }
- searcher.close();
- System.out.println("Search finished");
- }
- directive=fins.readUTF();
- }
- fins.close();
- fouts.close();
- is.close();
- outs.close();
- socket.close();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- import java.io.File;
- import java.io.IOException;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.CorruptIndexException;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.queryParser.QueryParser;
- import org.apache.lucene.search.IndexSearcher;
- import org.apache.lucene.search.Query;
- import org.apache.lucene.search.ScoreDoc;
- import org.apache.lucene.search.TopDocs;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.util.Version;
- public class Searcher {
- IndexSearcher indexSearcher;
- QueryParser queryParser;
- Query query;
- public Searcher(String indexDirectoryPath)
- throws IOException{
- Directory indexDirectory =
- FSDirectory.open(new File(indexDirectoryPath));
- indexSearcher = new IndexSearcher(indexDirectory);
- queryParser = new QueryParser(Version.LUCENE_36,
- LuceneConstants.FILE_NAME,
- new StandardAnalyzer(Version.LUCENE_36));
- }
- public TopDocs search( String searchQuery) {
- try {
- query = queryParser.parse(searchQuery);
- return indexSearcher.search(query, LuceneConstants.MAX_SEARCH);
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- public Document getDocument(ScoreDoc scoreDoc)
- throws CorruptIndexException, IOException{
- return indexSearcher.doc(scoreDoc.doc);
- }
- public void close() throws IOException{
- indexSearcher.close();
- }
- }
- import java.io.File;
- import java.io.FileFilter;
- import java.io.FileReader;
- import java.io.IOException;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.CorruptIndexException;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.util.Version;
- public class Indexer {
- private IndexWriter writer;
- @SuppressWarnings("deprecation")
- public Indexer(String indexDirectoryPath) throws IOException{
- //this directory will contain the indexes
- Directory indexDirectory =
- FSDirectory.open(new File(indexDirectoryPath));
- //create the indexer
- writer = new IndexWriter(indexDirectory,
- new StandardAnalyzer(Version.LUCENE_36),true,
- IndexWriter.MaxFieldLength.UNLIMITED);
- }
- public void close() throws CorruptIndexException, IOException{
- writer.close();
- }
- private Document getDocument(File file) throws IOException{
- Document document = new Document();
- //index file contents
- Field contentField = new Field(LuceneConstants.CONTENTS,
- new FileReader(file));
- //index file name
- Field fileNameField = new Field(LuceneConstants.FILE_NAME,
- file.getName().toString().trim(),
- Field.Store.YES,Field.Index.NOT_ANALYZED);
- //index file path
- Field filePathField = new Field(LuceneConstants.FILE_PATH,
- file.getCanonicalPath(),
- Field.Store.YES,Field.Index.NOT_ANALYZED);
- document.add(contentField);
- document.add(fileNameField);
- document.add(filePathField);
- return document;
- }
- private void indexFile(File file) throws IOException{
- System.out.println("Indexing "+file.getCanonicalPath());
- Document document = getDocument(file);
- writer.addDocument(document);
- }
- public int createIndex(String dataDirPath, FileFilter filter)
- throws IOException{
- //get all files in the data directory
- File[] files = new File(dataDirPath).listFiles();
- for (File file : files) {
- if(!file.isDirectory()
- && !file.isHidden()
- && file.exists()
- && file.canRead()
- && filter.accept(file)
- ){
- indexFile(file);
- }
- }
- return writer.numDocs();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement