[原创]基于apache Lucene的mp3搜索器 前些日子找机器上的一首老歌时,费了些周折,后想到既然这些mp3有自己的标签信息,为何不利用起来呢?笔者就尝试用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[i]));   }  }  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 
  
  
 
  |