| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
29 | 30 | 31 | 1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 | 1 | |||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
平臺: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進行代碼深入研究。
摘要: 搜索流程中的第二步就是構建一個Query。下面就來介紹Query及其構建。 當用戶輸入一個關鍵字,搜索引擎接收到后,并不是立刻就將它放入后臺開始進行關鍵字的檢索,而應當首先對這個關鍵字進行一定的分析和處理,使之成為一種后臺可以理解的形式,只有這樣,才能提高檢索的效率,同時檢索出更加有效的結果。那么,在Lucene中,這種處理,其實就是構建一個Query對象。 就Query對象本身言,它只是Luce... 閱讀全文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 回復此發言
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
<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
呵呵,我的方法不知道和你的三種方法有沒有可比性,個人感覺還不錯! @施偉 施偉 的做法,是不是還是不能解決,先選擇了一個文件,提交服務器之后這個file input域的值又被自動清空的問題? 回復 更多評論 評論
# 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 路過的
# re: 幾種js實現的動態多文件上傳 2007-06-04 11:28 sangern