随笔 - 11  文章 - 2  trackbacks - 0
<2007年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

平台:Lucene 2.1.0,JRE 1.4,Oracle 10g,IBM Web Sphere。
      数据表:Article。字段:ID(自动增长),Title(String),Content(String)。共有550000条记录。
      对Article建立索引:

 1import org.apache.lucene.analysis.*;
 2import org.apache.lucene.analysis.cn.*;
 3import org.apache.lucene.document.*;
 4import org.apache.lucene.index.*;
 5import java.sql.*;
 6import oracle.jdbc.pool.*;
 7
 8public class Index {
 9 private String url="jdbc:oracle:thin:@//192.168.0.l:1521/Test";
10 private String user="terry";
11 private String password="dev";
12 private Connection con=null;
13 private Statement st=null;
14 private ResultSet rs=null;
15 private String indexUrl="E:\\ArticleIndex";
16
17 private ResultSet getResult() throws Exception{
18        OracleDataSource ods=new OracleDataSource();
19
20        ods.setURL(this.url);
21        ods.setUser(this.user);
22        ods.setPassword(this.password);
23
24 this.con=ods.getConnection();
25 this.st=this.con.createStatement();
26 this.rs=this.st.executeQuery("SELECT * FROM Article");
27
28 return this.rs;
29    }
30
31 public void createIndex() throws Exception{
32        ResultSet rs=this.getResult();
33
34        Analyzer chineseAnalyzer=new ChineseAnalyzer();
35        IndexWriter indexWriter=new IndexWriter(this.indexUrl,chineseAnalyzer,true);
36        indexWriter.setMergeFactor(100);
37        indexWriter.setMaxBufferedDocs(100);
38
39        java.util.Date startDate=new java.util.Date();
40
41        System.out.println("开始索引时间:"+startDate);
42
43        executeIndex(rs,indexWriter);
44
45        indexWriter.optimize();
46
47        indexWriter.close();
48
49        java.util.Date endDate=new java.util.Date();
50
51        System.out.println("索引结束时间:"+endDate);
52        System.out.println("共花费:"+(endDate.getTime()-startDate.getTime())+"ms");
53    }
54
55 private void executeIndex(ResultSet rs,IndexWriter indexWriter) throws Exception{
56 int i=0;
57
58 while(rs.next()){
59 int id=rs.getInt("ID");
60            String title=rs.getString("TITLE");
61            String info=rs.getString("CONTENT");
62
63            Document doc=new Document();
64
65            Field idField=new Field("ID",Integer.toString(id),Field.Store.YES,Field.Index.NO,Field.TermVector.NO);
66            Field titleField=new Field("Title",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
67         Field infoField=new Field("Content",title,Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.YES);
68
69            doc.add(idField);
70            doc.add(titleField);
71            doc.add(infoField);
72
73            indexWriter.addDocument(doc);
74
75            i++;
76        }
77
78 this.close();
79
80        System.out.println("共处理记录:"+i);
81    }
82
83 private void close() throws Exception{
84 this.rs.close();
85 this.st.close();
86 this.con.close();
87    }
88}

      查找:

 1import java.io.*;
 2import org.apache.lucene.analysis.cn.*;
 3import org.apache.lucene.search.*;
 4import org.apache.lucene.store.*;
 5import org.apache.lucene.document.*;
 6import org.apache.lucene.queryParser.QueryParser;
 7
 8import java.util.*;
 9
10public class Search {
11
12 private static final String indexUrl="E:\\ArticleIndex";
13
14 public static void main(String[] args) throws Exception {
15/**/    /*建立索引代码,查找时注释*/
16 //Index index=new Index();
17
18 //index.createIndex();
19
20
21
22
23        File indexDir=new File(indexUrl);
24        FSDirectory fdir=FSDirectory.getDirectory(indexDir);
25
26        IndexSearcher searcher=new IndexSearcher(fdir);
27
28//对中文建立解析(必须)
29        QueryParser parser=new QueryParser("Title",new ChineseAnalyzer());
30        Query query=parser.parse("李湘");
31
32        Date startDate=new Date();
33        System.out.println("检索开始时间:"+startDate);
34
35        Hits result=searcher.search(query);
36
37 for(int i=0;i<result.length();i++){
38            Document doc=result.doc(i);
39
40            System.out.println("内容:"+doc.get("Content"));
41        }
42
43        Date endDate=new Date();
44
45        System.out.println("共有记录:"+result.length());
46        System.out.println("共花费:"+(endDate.getTime()-startDate.getTime()));
47    }
48
49}

      经测试,建立索引文件大概花了11分钟。一般情况下,和用SQL执行LIKE查询差不多。

      当然,这只是我的粗略测试。最近一阶段,我会对Lucene进行代码深入研究。

posted @ 2007-08-16 10:57 jacksontoto 阅读(236) | 评论 (0)编辑 收藏
     摘要: 搜索流程中的第二步就是构建一个Query。下面就来介绍Query及其构建。 当用户输入一个关键字,搜索引擎接收到后,并不是立刻就将它放入后台开始进行关键字的检索,而应当首先对这个关键字进行一定的分析和处理,使之成为一种后台可以理解的形式,只有这样,才能提高检索的效率,同时检索出更加有效的结果。那么,在Lucene中,这种处理,其实就是构建一个Query对象。 就Query对象本身言,它只是Luce...  阅读全文
posted @ 2007-08-16 10:56 jacksontoto 阅读(682) | 评论 (0)编辑 收藏

 

JDBC TM入门指南
http://www.zebcn.com/html/200411\103.html
Java 程序编码规范
http://www.zebcn.com/html/200411\104.html
JavaBean入门
http://www.zebcn.com/html/200411\105.html
对JAVA语言的十个常见误解
http://www.zebcn.com/html/200411\114.html
简析JAVA的XML编程
http://www.zebcn.com/html/200411\115.html
Java技巧:列表排序
http://www.zebcn.com/html/200411\129.html
Java异常处理
http://www.zebcn.com/html/200411\130.html
Java中初学者比较爱出错的运算问题
http://www.zebcn.com/html/200411\131.html
类注释文档编写方法
http://www.zebcn.com/html/200411\132.html
对JAVA语言的十个常见误解
http://www.zebcn.com/html/200411\133.html
简析JAVA的XML编程(to:初学者们)
http://www.zebcn.com/html/200411\134.html
关于窗口的操作详谈
http://www.zebcn.com/html/200411\135.html
Java 语言中的 return 语句
http://www.zebcn.com/html/200411\136.html
Java连接各种数据库的实例
http://www.zebcn.com/html/200411\137.html
在Java中实现回调过程
http://www.zebcn.com/html/200411\138.html
Java 中对文件的读写操作之比较
http://www.zebcn.com/html/200412\166.html
[原创]Java的文件读和写
http://www.zebcn.com/html/200412\167.html
java-在Java中读写Excel文件
http://www.zebcn.com/html/200412\171.html
实战JMS (转)
http://www.zebcn.com/html/200412\172.html
取得时间的函数
http://www.zebcn.com/html/200412\177.html
JAVA-如何实现TIMER功能
http://www.zebcn.com/html/200412\179.html
数据库访问简单实现
http://www.zebcn.com/html/200412\180.html
将数据库操作封装到Javabean
http://www.zebcn.com/html/200412\187.html
用Java编写扫雷游戏--代码思想
http://www.zebcn.com/html/200412\190.html
Hibernate事务处理机制
http://www.zebcn.com/html/200412\191.html
jboss 4.0 中JSP调用EJB的简单例子
http://www.zebcn.com/html/200412\192.html
JSP WEBServer的实现原理
http://www.zebcn.com/html/200412\193.html
Spring 入门(一个简单的例子)
http://www.zebcn.com/html/200501\204.html

使用Java生成Pdf文档
http://www.zebcn.com/html/200501\205.html
JAVA生成JPG缩略图
http://www.zebcn.com/html/200501\206.html
学习J2ME编程需要掌握的七种技术
http://www.zebcn.com/html/200501\210.html
第一个EJB3.0范例
http://www.zebcn.com/html/200502\213.html
Java RMI 简单示例
http://www.zebcn.com/html/200502\215.html
[原创]java初学者之经验总结
http://www.zebcn.com/html/200502\217.html
Tomcat配置技巧Top 10
http://www.zebcn.com/html/200502\218.html
学习Java的30个基本概念
http://www.zebcn.com/html/200502\222.html
java常用的加密,解密,数字签名等API
http://www.zebcn.com/html/200502\227.html
一种简单的struts级连菜单实现方法
http://www.zebcn.com/html/200502\228.html
2005年4月5日Java新品发布公告
http://www.zebcn.com/html/200504\232.html
Java连接各种数据库的实例
http://www.zebcn.com/html/200504\241.html
Java常见问题集锦(来自Sun中国官方站)
http://www.zebcn.com/html/200504\243.html
java 文件操作大全
http://www.zebcn.com/html/200504\249.html
J2EE项目危机【翻译】
http://www.zebcn.com/html/200505\254.html
JAVA对数字证书的常用操作
http://www.zebcn.com/html/200510\275.html
三种整合Struts 应用程序与Spring的方式
http://www.zebcn.com/html/200511\278.html
[分享]Ant简介
http://www.zebcn.com/html/200511\282.html
Struts 的动态复选框
http://www.zebcn.com/html/200511\289.html
集成 Struts、Tiles 和 JavaServer Faces
http://www.zebcn.com/html/200511\293.html
J2ME应用程序内存优化三招
http://www.zebcn.com/html/200512\338.html
一个生成无重复数字的代码
http://www.zebcn.com/html/200512\351.html
RSS 开发教程
http://www.zebcn.com/html/200512\355.html
java写的贪吃蛇游戏
http://www.zebcn.com/html/200512\360.html
应用Java技术开发WAP应用程序
http://www.zebcn.com/html/200512\361.html
web框架Jakarta Tapestry 4.0-rc-3 发布
http://www.zebcn.com/html/200512\366.html
校验获取身份证信息的JAVA程序
http://www.zebcn.com/html/200512\368.html
模式实践:观察者模式与Spring
http://www.zebcn.com/html/200512\369.html
XML读写/存取属性的Java工具类库
http://www.zebcn.com/html/200512\370.html
JAVA写的四则混合运算
http://www.zebcn.com/html/200512\371.html
Web框架RIFE/Laszlo 1.3.1 发布
http://www.zebcn.com/html/200601\372.html
使用 JSF 架构进行设计
http://www.zebcn.com/html/200601\376.html
用java做的滚动的正弦曲线
http://www.zebcn.com/html/200601\378.html
EJB3.0和Spring比较
http://www.zebcn.com/html/200601\379.html
Struts 中常见错误
http://www.zebcn.com/html/200601\380.html
漫谈Java数据库存取技术
http://www.zebcn.com/html/200601\381.html
Java多线程程序设计
http://www.zebcn.com/html/200601\385.html
JAVA中的时间操作
http://www.zebcn.com/html/200601\386.html
12个最重要的J2EE最佳实践
http://www.zebcn.com/html/200601\389.html
Java中的异步网络编程
http://www.zebcn.com/html/200601\394.html
Java I/O重定向
http://www.zebcn.com/html/200601\395.html
构建高性能J2EE应用的10个技巧
http://www.zebcn.com/html/200601\397.html
java程序得到域名对应的所有IP地址
http://www.zebcn.com/html/200601\398.html
Java语言编程中更新XML文档的常用方法
http://www.zebcn.com/html/200601\401.html
HTTP代理如何正确处理Cookie
http://www.zebcn.com/html/200601\405.html

作者: fafile 2006-5-18 09:57 回复此发言


3
回复:Java精品文章收集

JavaMail 发送HTML邮件
http://www.zebcn.com/html/200602\411.html
JSP数据类型
http://www.zebcn.com/html/200602\412.html
Java5 多线程实践
http://www.zebcn.com/html/200602\415.html
JDK5.0的11个主要新特征
http://www.zebcn.com/html/200602\419.html
运用类反射机制简化Struts应用程序开发
http://www.zebcn.com/html/200602\420.html
用 Struts 实现动态单选按钮
http://www.zebcn.com/html/200602\421.html
JDO技术分析及企业应用研究
http://www.zebcn.com/html/200602\422.html
一个J2EE项目的最小工具集
http://www.zebcn.com/html/200602\425.html
一个实现MD5的简洁的java类
http://www.zebcn.com/html/200602\429.html
服务器与浏览器的会话
http://www.zebcn.com/html/200602\434.html
使用Spring JMS简化异步消息处理
http://www.zebcn.com/html/200603\443.html
采用HttpServlet 实现web文件下载
http://www.zebcn.com/html/200603\444.html
Spring AOP实际应用一例
http://www.zebcn.com/html/200603\445.html
JNI中文处理问题小结
http://www.zebcn.com/html/200603\446.html
java获取windows系统网卡mac地址
http://www.zebcn.com/html/200603\454.html
Swing vs. SWT 之调用堆栈性能比较
http://www.zebcn.com/html/200603\462.html
Using SVN with Ant
http://www.zebcn.com/html/200603\463.html
mud程序及内附的dom4j解析xml源代码
http://www.zebcn.com/html/200603\464.html
myeclipse中J2EE项目之间的组织结构
http://www.zebcn.com/html/200603\465.html
初学者如何开发出高质量的J2EE系统
http://www.zebcn.com/html/200603\467.html
WebLogic Server 管理最佳实践
http://www.zebcn.com/html/200603\474.html
WebLogic Server 性能调优
http://www.zebcn.com/html/200603\475.html
简化WebLogic 8.1项目的配置
http://www.zebcn.com/html/200603\476.html
WebLogic域配置策略-手动和模板选项
http://www.zebcn.com/html/200603\477.html
JDBC中获取数据表的信息
http://www.zebcn.com/html/200603\479.html
Java 开发中遇到的乱码问题
http://www.zebcn.com/html/200603\484.html
简易的http客户端附源代码
http://www.zebcn.com/html/200603\486.html
JDBC 4.0规范之目标
http://www.zebcn.com/html/200603\487.html
java的各种排序算法
http://www.zebcn.com/html/200603\495.html
用Java实现Web服务器
http://www.zebcn.com/html/200603\496.html
使用DBMS存储过程
http://www.zebcn.com/html/200603\497.html
Java剖析工具YourKit Java Profiler 6.0-EAP1 发布
http://www.zebcn.com/html/200603\3253.html
用Java快速开发Linux GUI应用
http://www.zebcn.com/html/200603\3260.html
java 理论与实践: 伪 typedef 反模式
http://www.zebcn.com/html/200604\3262.html
使用 Struts Validator (1)
http://www.zebcn.com/html/200604\3263.html
使用 Struts Validator (2)
http://www.zebcn.com/html/200604\3264.html
使用 Struts Validator (3)
http://www.zebcn.com/html/200604\3265.html
使用 Struts Validator (4)
http://www.zebcn.com/html/200604\3266.html
使用 Struts Validator (5)
http://www.zebcn.com/html/200604\3267.html
使用 Struts Validator (6)
http://www.zebcn.com/html/200604\3268.html
查询数据库后返回Iterator
http://www.zebcn.com/html/200604\3278.html
DOM属性用法速查手册
http://www.zebcn.com/html/200604\3279.html
论J2EE程序员的武功修为
http://www.zebcn.com/html/200604\3280.html
让window服务进程中自动加载MYSQL服务
http://www.zebcn.com/html/200604\3287.html
J2EE应用程序异常处理框架
http://www.zebcn.com/html/200604\3289.html
Java2下Applet数字签名具体实现方法
http://www.zebcn.com/html/200604\3290.html
使用tomcat4.1.31和mysql 配置数据源
http://www.zebcn.com/html/200604\3291.html
用java得到本机所有的ip地址
http://www.zebcn.com/html/200604\3292.html
使用Socket连接穿越CMWAP代理
http://www.zebcn.com/html/200604\3293.html
Java读取Excel方式对比
http://www.zebcn.com/html/200604\3294.html

posted @ 2007-08-15 08:50 jacksontoto 阅读(185) | 评论 (0)编辑 收藏

 

方式一:事先写好多个input.在点击时才显示。也就是说上传的最大个数是写死了的。
 html

<p>
<a href='#' onclick='javascript:viewnone(more1)'> 添加附件 </a>
<div id='more1' style='display:none'>
<input type="file" name="attach1" size="50"javascript:viewnone(more2)>
</span>
</div>
<div id='more2' style='display:none'>
<input type="file" name="attach2" size="50"'>
</div>
</p>

js

<SCRIPT language="javascript">
function viewnone(e){
    e.style.display=(e.style.display=="none")?"":"none";
  }
</script>

方式二:这种方式的动态多文件上传是实现了的,很简单的,不说废话看code
html

<input type="button" name="button" value="添加附件" onclick="addInput()">
<input type="button" name="button" value="删除附件" onclick="deleteInput()">
<span id="upload"></span>

js

<script type="text/javascript">
var attachname = "attach";
var i=1;
function   addInput(){
if(i>0){
var attach = attachname + i ;
if(createInput(attach))
                      i=i+1;
              }
          } 
function deleteInput(){
if(i>1){
                    i=i-1;
if(!removeInput())
                        i=i+1;
                }
          } 
function createInput(nm){   
var  aElement=document.createElement("input");   
             aElement.name=nm;
             aElement.id=nm;
             aElement.type="file";
             aElement.size="50";
//aElement.value="thanks";   
//aElement.onclick=Function("asdf()");  
if(document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
          }  
function removeInput(nm){
var aElement = document.getElementById("upload");
if(aElement.removeChild(aElement.lastChild) == null)
return false;
return true;   
          }  
</script>

方式三:动态多文件上传,只是在oFileInput.click();这个地方,这样做就不能上传这个文件了,因为发现它在上传之时就把这个input中的文件置空了。很可能是为了安全着想吧!
另外还有一点就是说,click()只有在ie中才能正常运行。
虽说这种方式最终没能实现上传,但还是留下来参考,看看是否有人可以真正实现上传。
 html

<A href="javascript:newUpload();">添加附件</A>
<TABLE width="100%" border="0" cellpadding="0" cellspacing="1">
<TBODY id="fileList"></TBODY>
</TABLE>
<DIV id="uploadFiles" style="display:block"></DIV>

js

<SCRIPT language="javascript">
//---新建上传
function newUpload(){
var oFileList = document.getElementById("fileList");
var fileCount = oFileList.childNodes.length + 1;
var oFileInput = newFileInput("upfile_" + fileCount);
if(selectFile(oFileInput)){
            addFile(oFileInput);
        }
    }
//----选择文件
function selectFile(oFileInput){
var oUploadFiles = document.getElementById("uploadFiles");
        oUploadFiles.appendChild(oFileInput);
        oFileInput.focus();
        oFileInput.click();//不能这样做,可能是为了安全着想吧!
var fileValue = oFileInput.value;
if(fileValue == ""){
            oUploadFiles.removeChild(oFileInput);
return false;
        }
else
return true;
    }
//---新建一个文件显示列表
function addFile(oFileInput){
var oFileList = document.getElementById("fileList");
var fileIndex = oFileList.childNodes.length + 1;
var oTR  = document.createElement("TR");
var oTD1 = document.createElement("TD");
var oTD2 = document.createElement("TD");
        oTR.setAttribute("id","file_" + fileIndex);
        oTR.setAttribute("bgcolor","#FFFFFF");
        oTD1.setAttribute("width","6%");
        oTD2.setAttribute("width","94%");
        oTD2.setAttribute("align","left");
        oTD2.innerText = oFileInput.value;
        oTD1.innerHTML = '<A href="javascript:removeFile('+ fileIndex + ');">删除</A>';
        oTR.appendChild(oTD1);
        oTR.appendChild(oTD2);
        oFileList.appendChild(oTR);
    }
//---移除上传的文件 
function removeFile(fileIndex){
var oFileInput = document.getElementById("upfile_" + fileIndex);
var oTR        = document.getElementById("file_" + fileIndex);
        uploadFiles.removeChild(oFileInput);
        fileList.removeChild(oTR);
    }
//---创建一个file input对象并返回
function newFileInput(_name){
var oFileInput  = document.createElement("INPUT");
        oFileInput.type = "file";
        oFileInput.id = _name;
        oFileInput.name = _name;
        oFileInput.size="50";
//oFileInput.setAttribute("id",_name);
//oFileInput.setAttribute("name",_name);
//oFileInput.outerHTML = '<INPUT type=file id=' + _name + ' name=' + _name + '>';
//alert(oFileInput.outerHTML);
return oFileInput;
    }
</SCRIPT>

posted on 2007-01-26 17:21 重归本垒(BNBN) 阅读(1656) 评论(4) 编辑 收藏 引用 所属分类: JS

评论
# re: 几种js实现的动态多文件上传 2007-01-27 13:20 施伟

呵呵,我的方法不知道和你的三种方法有没有可比性,个人感觉还不错!
做一个 添加附件 然后做一个type为file的input框,把此框和span定位重叠起来 把file框透明度设置为0 即完全看不到,但是确实存在。这个时候点span的时候就是在点这个file框 但是看不到file框子 是不是实现了呢? 然后再结合你第二种的方式给框编号 动态增加就可以实现多文件上传了 。
呵呵 我在我的程序里面这样实现的 很好用 如果有兴趣讨论到我blog留言 或者发邮件给我吧 多交流。。。
回复 更多评论

# re: 几种js实现的动态多文件上传 2007-01-29 18:00 重归本垒(BNBN)

@施伟
呵呵!施伟,你这样做,如果实现了,那么比我的方法更胜一筹了,我以前也这样考虑过,只是觉的好麻烦,而没有去实现它!
另外,还非常谢谢你能关注我的Bolg!
回复 更多评论

# re: 几种js实现的动态多文件上传 2007-02-12 17:12 路过的

施伟 的做法,是不是还是不能解决,先选择了一个文件,提交服务器之后这个file input域的值又被自动清空的问题?  回复 更多评论

# re: 几种js实现的动态多文件上传 2007-06-04 11:28 sangern

第二种方法不错  回复 更多评论

posted @ 2007-08-15 08:38 jacksontoto 阅读(10081) | 评论 (2)编辑 收藏