手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

基于Lucene的Mp3检索器

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

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


上一篇: [原创]Swing技巧.4:设置系统窗口边框
下一篇: Spring学习笔记:1、概念理解

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!