中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
基于Lucene的Mp3检索器
作者:未知 时间:2005-07-27 22:26 出处:CSDN 责编:chinaitpower
              摘要:基于Lucene的Mp3检索器

[原创]基于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


 


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有