Posted By:
sarika_sonawane
Posted On:
Tuesday, March 30, 2004 09:14 PM
Servlet I wann to index files from folder present on remote pc like xyz.com/documents is folder name and inside that my files are present. I wrote following code public class Servlet extends HttpServlet { PrintWriter out = response.getWriter(); index_path=request.getParameter("index_path"); search_path=request.getParameter("search_path"); out.println("Index path"+index_path); out.println(" search path"+search_path); static String index_path; static String search_path; protected void processRequest(HttpServletRequest request, HttpServletResp
More>>
Servlet
I wann to index files from folder present on remote pc like
xyz.com/documents is folder name and inside that my files are present. I wrote following code
public class Servlet extends HttpServlet {
PrintWriter out = response.getWriter();
index_path=request.getParameter("index_path");
search_path=request.getParameter("search_path");
out.println("Index path"+index_path);
out.println("
search path"+search_path);
static String index_path;
static String search_path;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
index_path=request.getParameter("index_path");
search_path=request.getParameter("search_path");
try
{
Date start = new Date();
File f = new File(index_path);
IndexWriter writer = new IndexWriter(f,new StandardAnalyzer(), true );
//String searchPath = (new File(search_path)).getAbsolutePath();
//int i = searchPath.indexOf("bin");
//searchPath = searchPath.substring(i+4);
//out.println("
path is"+searchPath);
//URL u = new URL(search_path);
//out.println("
path"+u.getPath());
//out.println("
path"+u.getFile());
//out.println("
path"+u.toString());
//out.println("
path"+u.getHost());
indexDocs( writer, new File(search_path));
writer.optimize();
writer.close();
Date end = new Date();
out.println("
");
out.println("
");
out.println("
");
out.println("
");
out.println("
Your Given folder is indexd within ");
out.print( end.getTime() - start.getTime() );
out.println(" total milliseconds");
out.println("
");
out.println("
");
out.close();
}
}
static void indexDocs( IndexWriter writer, File file )
throws Exception
{
System.out.println("
directory"+file.isDirectory());
if ( file.isDirectory() )
{
String[] files = file.list();
if ( files != null )
{
for ( int i = 0; i
< files.length; i++ )
indexDocs( writer, new File( file, files[i] ) );
}
}
else
{
System.out.println( "adding " + file );
try
{
writer.addDocument( document( file ) );
}
catch ( FileNotFoundException e )
{
System.out.println( ">>>" );
e.printStackTrace();
}
}
}
static Document document( File file ) throws FileNotFoundException
{
Document doc = new Document();
doc.add( Field.Text( "path", file.getPath().substring(search_path.length() ) ) );
doc.add( Field.Keyword( "modified",
DateField.timeToString( file.lastModified() ) ) );
Reader reader = new BufferedReader(
new InputStreamReader( new FileInputStream( file ) ) );
doc.add( Field.Text( "contents", reader ) );
return doc;
}
<<Less