论坛首页 Java企业应用论坛

基于apache Lucene的mp3搜索器

浏览 1609 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-04-24   最后修改:2009-11-26
学了点Lucene就尝试用Lucene实现,分两部分,Mp3Indexer.java是创建索引的,mp3search.jsp是搜索mp3的页面。
下面是Mp3Indexer.java的代码。
package mp3indexer;
import java.io.*;
import java.text.*;
import java.util.*; 
import org.apache.lucene.analysis.cjk.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;

public class Mp3Indexer
{
 public final static String mp3Path="d:\mp3";//mp3所在目录
 public final static String indexPath="c:\mp3Indexer";//索引存放目录
 public static void main(String[] args) throws ClassNotFoundException, IOException{
  try {
   IndexWriter writer = new IndexWriter(indexPath, new CJKAnalyzer(), true); 
   indexMp3s(writer, new File(mp3Path)); 

   System.out.println("优化中....");
   writer.optimize();
   writer.close();

  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
}

public static void indexMp3s(IndexWriter writer, File file) throws Exception {
 if (file.isDirectory()) {
  String[] files = file.list();
  for (int i = 0; i < files.length; i++) {
   indexMp3s(writer, new File(file, files));
  }
 }
 else if (file.getPath().endsWith(".mp3")) { //只对 MP3 文件做索引
 System.out.print("正在处理文件:" + file + " ....");
 // Add mp3 file ....
 Document doc = new Document();
 doc.add(Field.Text("name", file.getName())); //索引文件名
 doc.add(Field.UnIndexed("modified", DateFormat.getDateTimeInstance().format(new Date(file.lastModified())))); //索引最后修改时间
 doc.add(Field.Text("size",""+NumberFormat.getNumberInstance().format(file.length()/1048576.0)+"MB")); //索引最后修改时间

 FileReader fReader = new FileReader(file);
 java.io.RandomAccessFile r=new RandomAccessFile(file,"r");
 r.seek(file.length()-128);
 byte[] bt=new byte[127];
 r.read(bt);
 String labelInfo=new String(bt,"GB2312");
 System.out.println(labelInfo);

 if (labelInfo.startsWith("TAG")) {
  doc.add(Field.Text("comment", labelInfo));
 }
 System.out.println("[处理完成]");

 r.close();
 fReader.close();
 writer.addDocument(doc);
 } //end else if
}

} //end class


论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics