| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
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。
數(shù)據(jù)表: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("索引結(jié)束時間:"+endDate);
52 System.out.println("共花費(fèi):"+(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("內(nèi)容:"+doc.get("Content"));
41 }
42
43 Date endDate=new Date();
44
45 System.out.println("共有記錄:"+result.length());
46 System.out.println("共花費(fèi):"+(endDate.getTime()-startDate.getTime()));
47 }
48
49}
經(jīng)測試,建立索引文件大概花了11分鐘。一般情況下,和用SQL執(zhí)行LIKE查詢差不多。
當(dāng)然,這只是我的粗略測試。最近一階段,我會對Lucene進(jìn)行代碼深入研究。
摘要: 搜索流程中的第二步就是構(gòu)建一個Query。下面就來介紹Query及其構(gòu)建。 當(dāng)用戶輸入一個關(guān)鍵字,搜索引擎接收到后,并不是立刻就將它放入后臺開始進(jìn)行關(guān)鍵字的檢索,而應(yīng)當(dāng)首先對這個關(guān)鍵字進(jìn)行一定的分析和處理,使之成為一種后臺可以理解的形式,只有這樣,才能提高檢索的效率,同時檢索出更加有效的結(jié)果。那么,在Lucene中,這種處理,其實(shí)就是構(gòu)建一個Query對象。 就Query對象本身言,它只是Luce... 閱讀全文JDBC TM入門指南
http://www.zebcn.com/html/200411\103.html
Java 程序編碼規(guī)范
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中初學(xué)者比較愛出錯的運(yùn)算問題
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:初學(xué)者們)
http://www.zebcn.com/html/200411\134.html
關(guān)于窗口的操作詳談
http://www.zebcn.com/html/200411\135.html
Java 語言中的 return 語句
http://www.zebcn.com/html/200411\136.html
Java連接各種數(shù)據(jù)庫的實(shí)例
http://www.zebcn.com/html/200411\137.html
在Java中實(shí)現(xiàn)回調(diào)過程
http://www.zebcn.com/html/200411\138.html
Java 中對文件的讀寫操作之比較
http://www.zebcn.com/html/200412\166.html
[原創(chuàng)]Java的文件讀和寫
http://www.zebcn.com/html/200412\167.html
java-在Java中讀寫Excel文件
http://www.zebcn.com/html/200412\171.html
實(shí)戰(zhàn)JMS (轉(zhuǎn))
http://www.zebcn.com/html/200412\172.html
取得時間的函數(shù)
http://www.zebcn.com/html/200412\177.html
JAVA-如何實(shí)現(xiàn)TIMER功能
http://www.zebcn.com/html/200412\179.html
數(shù)據(jù)庫訪問簡單實(shí)現(xiàn)
http://www.zebcn.com/html/200412\180.html
將數(shù)據(jù)庫操作封裝到Javabean
http://www.zebcn.com/html/200412\187.html
用Java編寫掃雷游戲--代碼思想
http://www.zebcn.com/html/200412\190.html
Hibernate事務(wù)處理機(jī)制
http://www.zebcn.com/html/200412\191.html
jboss 4.0 中JSP調(diào)用EJB的簡單例子
http://www.zebcn.com/html/200412\192.html
JSP WEBServer的實(shí)現(xiàn)原理
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
學(xué)習(xí)J2ME編程需要掌握的七種技術(shù)
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
[原創(chuàng)]java初學(xué)者之經(jīng)驗(yàn)總結(jié)
http://www.zebcn.com/html/200502\217.html
Tomcat配置技巧Top 10
http://www.zebcn.com/html/200502\218.html
學(xué)習(xí)Java的30個基本概念
http://www.zebcn.com/html/200502\222.html
java常用的加密,解密,數(shù)字簽名等API
http://www.zebcn.com/html/200502\227.html
一種簡單的struts級連菜單實(shí)現(xiàn)方法
http://www.zebcn.com/html/200502\228.html
2005年4月5日J(rèn)ava新品發(fā)布公告
http://www.zebcn.com/html/200504\232.html
Java連接各種數(shù)據(jù)庫的實(shí)例
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項(xiàng)目危機(jī)【翻譯】
http://www.zebcn.com/html/200505\254.html
JAVA對數(shù)字證書的常用操作
http://www.zebcn.com/html/200510\275.html
三種整合Struts 應(yīng)用程序與Spring的方式
http://www.zebcn.com/html/200511\278.html
[分享]Ant簡介
http://www.zebcn.com/html/200511\282.html
Struts 的動態(tài)復(fù)選框
http://www.zebcn.com/html/200511\289.html
集成 Struts、Tiles 和 JavaServer Faces
http://www.zebcn.com/html/200511\293.html
J2ME應(yīng)用程序內(nèi)存優(yōu)化三招
http://www.zebcn.com/html/200512\338.html
一個生成無重復(fù)數(shù)字的代碼
http://www.zebcn.com/html/200512\351.html
RSS 開發(fā)教程
http://www.zebcn.com/html/200512\355.html
java寫的貪吃蛇游戲
http://www.zebcn.com/html/200512\360.html
應(yīng)用Java技術(shù)開發(fā)WAP應(yīng)用程序
http://www.zebcn.com/html/200512\361.html
web框架Jakarta Tapestry 4.0-rc-3 發(fā)布
http://www.zebcn.com/html/200512\366.html
校驗(yàn)獲取身份證信息的JAVA程序
http://www.zebcn.com/html/200512\368.html
模式實(shí)踐:觀察者模式與Spring
http://www.zebcn.com/html/200512\369.html
XML讀寫/存取屬性的Java工具類庫
http://www.zebcn.com/html/200512\370.html
JAVA寫的四則混合運(yùn)算
http://www.zebcn.com/html/200512\371.html
Web框架RIFE/Laszlo 1.3.1 發(fā)布
http://www.zebcn.com/html/200601\372.html
使用 JSF 架構(gòu)進(jìn)行設(shè)計
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數(shù)據(jù)庫存取技術(shù)
http://www.zebcn.com/html/200601\381.html
Java多線程程序設(shè)計
http://www.zebcn.com/html/200601\385.html
JAVA中的時間操作
http://www.zebcn.com/html/200601\386.html
12個最重要的J2EE最佳實(shí)踐
http://www.zebcn.com/html/200601\389.html
Java中的異步網(wǎng)絡(luò)編程
http://www.zebcn.com/html/200601\394.html
Java I/O重定向
http://www.zebcn.com/html/200601\395.html
構(gòu)建高性能J2EE應(yīng)用的10個技巧
http://www.zebcn.com/html/200601\397.html
java程序得到域名對應(yīng)的所有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 回復(fù)此發(fā)言
JavaMail 發(fā)送HTML郵件
http://www.zebcn.com/html/200602\411.html
JSP數(shù)據(jù)類型
http://www.zebcn.com/html/200602\412.html
Java5 多線程實(shí)踐
http://www.zebcn.com/html/200602\415.html
JDK5.0的11個主要新特征
http://www.zebcn.com/html/200602\419.html
運(yùn)用類反射機(jī)制簡化Struts應(yīng)用程序開發(fā)
http://www.zebcn.com/html/200602\420.html
用 Struts 實(shí)現(xiàn)動態(tài)單選按鈕
http://www.zebcn.com/html/200602\421.html
JDO技術(shù)分析及企業(yè)應(yīng)用研究
http://www.zebcn.com/html/200602\422.html
一個J2EE項(xiàng)目的最小工具集
http://www.zebcn.com/html/200602\425.html
一個實(shí)現(xiàn)MD5的簡潔的java類
http://www.zebcn.com/html/200602\429.html
服務(wù)器與瀏覽器的會話
http://www.zebcn.com/html/200602\434.html
使用Spring JMS簡化異步消息處理
http://www.zebcn.com/html/200603\443.html
采用HttpServlet 實(shí)現(xiàn)web文件下載
http://www.zebcn.com/html/200603\444.html
Spring AOP實(shí)際應(yīng)用一例
http://www.zebcn.com/html/200603\445.html
JNI中文處理問題小結(jié)
http://www.zebcn.com/html/200603\446.html
java獲取windows系統(tǒng)網(wǎng)卡mac地址
http://www.zebcn.com/html/200603\454.html
Swing vs. SWT 之調(diào)用堆棧性能比較
http://www.zebcn.com/html/200603\462.html
Using SVN with Ant
http://www.zebcn.com/html/200603\463.html
mud程序及內(nèi)附的dom4j解析xml源代碼
http://www.zebcn.com/html/200603\464.html
myeclipse中J2EE項(xiàng)目之間的組織結(jié)構(gòu)
http://www.zebcn.com/html/200603\465.html
初學(xué)者如何開發(fā)出高質(zhì)量的J2EE系統(tǒng)
http://www.zebcn.com/html/200603\467.html
WebLogic Server 管理最佳實(shí)踐
http://www.zebcn.com/html/200603\474.html
WebLogic Server 性能調(diào)優(yōu)
http://www.zebcn.com/html/200603\475.html
簡化WebLogic 8.1項(xiàng)目的配置
http://www.zebcn.com/html/200603\476.html
WebLogic域配置策略-手動和模板選項(xiàng)
http://www.zebcn.com/html/200603\477.html
JDBC中獲取數(shù)據(jù)表的信息
http://www.zebcn.com/html/200603\479.html
Java 開發(fā)中遇到的亂碼問題
http://www.zebcn.com/html/200603\484.html
簡易的http客戶端附源代碼
http://www.zebcn.com/html/200603\486.html
JDBC 4.0規(guī)范之目標(biāo)
http://www.zebcn.com/html/200603\487.html
java的各種排序算法
http://www.zebcn.com/html/200603\495.html
用Java實(shí)現(xiàn)Web服務(wù)器
http://www.zebcn.com/html/200603\496.html
使用DBMS存儲過程
http://www.zebcn.com/html/200603\497.html
Java剖析工具YourKit Java Profiler 6.0-EAP1 發(fā)布
http://www.zebcn.com/html/200603\3253.html
用Java快速開發(fā)Linux GUI應(yīng)用
http://www.zebcn.com/html/200603\3260.html
java 理論與實(shí)踐: 偽 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
查詢數(shù)據(jù)庫后返回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服務(wù)進(jìn)程中自動加載MYSQL服務(wù)
http://www.zebcn.com/html/200604\3287.html
J2EE應(yīng)用程序異常處理框架
http://www.zebcn.com/html/200604\3289.html
Java2下Applet數(shù)字簽名具體實(shí)現(xiàn)方法
http://www.zebcn.com/html/200604\3290.html
使用tomcat4.1.31和mysql 配置數(shù)據(jù)源
http://www.zebcn.com/html/200604\3291.html
用java得到本機(jī)所有的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>
方式二:這種方式的動態(tài)多文件上傳是實(shí)現(xiàn)了的,很簡單的,不說廢話看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>
方式三:動態(tài)多文件上傳,只是在oFileInput.click();這個地方,這樣做就不能上傳這個文件了,因?yàn)榘l(fā)現(xiàn)它在上傳之時就把這個input中的文件置空了。很可能是為了安全著想吧!
另外還有一點(diǎn)就是說,click()只有在ie中才能正常運(yùn)行。
雖說這種方式最終沒能實(shí)現(xiàn)上傳,但還是留下來參考,看看是否有人可以真正實(shí)現(xiàn)上傳。
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);
}
//---創(chuàng)建一個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
呵呵,我的方法不知道和你的三種方法有沒有可比性,個人感覺還不錯! @施偉 施偉 的做法,是不是還是不能解決,先選擇了一個文件,提交服務(wù)器之后這個file input域的值又被自動清空的問題? 回復(fù) 更多評論 評論
# re: 幾種js實(shí)現(xiàn)的動態(tài)多文件上傳 2007-01-27 13:20 施偉
做一個 添加附件 然后做一個type為file的input框,把此框和span定位重疊起來 把file框透明度設(shè)置為0 即完全看不到,但是確實(shí)存在。這個時候點(diǎn)span的時候就是在點(diǎn)這個file框 但是看不到file框子 是不是實(shí)現(xiàn)了呢? 然后再結(jié)合你第二種的方式給框編號 動態(tài)增加就可以實(shí)現(xiàn)多文件上傳了 。
呵呵 我在我的程序里面這樣實(shí)現(xiàn)的 很好用 如果有興趣討論到我blog留言 或者發(fā)郵件給我吧 多交流。。。
回復(fù) 更多評論 # re: 幾種js實(shí)現(xiàn)的動態(tài)多文件上傳 2007-01-29 18:00 重歸本壘(BNBN)
呵呵!施偉,你這樣做,如果實(shí)現(xiàn)了,那么比我的方法更勝一籌了,我以前也這樣考慮過,只是覺的好麻煩,而沒有去實(shí)現(xiàn)它!
另外,還非常謝謝你能關(guān)注我的Bolg!
回復(fù) 更多評論 # re: 幾種js實(shí)現(xiàn)的動態(tài)多文件上傳 2007-02-12 17:12 路過的
# re: 幾種js實(shí)現(xiàn)的動態(tài)多文件上傳 2007-06-04 11:28 sangern
[ 2007-7-27 16:28:00 | By: 葉尋飛 ]
初學(xué)JSP時,寫了一些工具函數(shù)因?yàn)椴惶珪肑AVA下的正則表達(dá)式也只能這么寫啦!
發(fā)出來讓大家批評批評提點(diǎn)意見!有幾個函數(shù)不算是自己寫的希望愛挑剌的朋友嘴
下留情!我是新手我怕誰,臉皮不行的人水平也上不去呀.嘻嘻..
package mxzc.web.strctrl;
public class StringCtrl
{/********************************************
public synchronized String HTMLcode(String TXTcode) 功能:文本替換
public synchronized String Unhtmlcode(String str) 功能:(不完全)反文本
替換
public synchronized String Unhtmlcodea(String str) 功能:反文本替換
public synchronized boolean emailcheck (String email) 功能:檢查一個字
符串是否符合E-Mail
public synchronized boolean isemailstr(String email) 功能:檢查一個字
符串是否符合E-Mail
public synchronized boolean isqqstr(String qq) 功能:檢查一個字符串是
否符合QQ
public synchronized boolean isnumstr(String num) 功能:檢查一個字符串
是否為一數(shù)字串
public synchronized String userstrlow(String user) 功能:替換用戶名中
不合法的部分
public synchronized boolean userstrchk(String user) 功能:檢查字符串是
否符合用戶名法則
public synchronized boolean istelstr(String tel) 功能:檢查字符串是否
為TEL
public synchronized boolean urlcheck(String url) 功能:檢查字符串是否
為URL
public synchronized String isotogbk(String iso) 功能:ISO9006-1碼轉(zhuǎn)換
為GBK
public synchronized String gbktoiso(String gbk) 功能:GBK碼轉(zhuǎn)換為
ISO9006-1
public synchronized String dostrcut(String oldstr,int length) 功能:按
漢字長換行(英文按半個字長)
public synchronized String inttodateshow(int datenum) 功能:將1900年至
時間的秒數(shù)換為日期字符串
public synchronized String nowdateshow() 功能:顯示當(dāng)前日期
public synchronized java.util.Date inttodate(int datenum) 功能:將秒數(shù)
轉(zhuǎn)換為日期
public synchronized int datetoint() 功能:將時間換為從1900年至今的秒
數(shù)
public synchronized int datetoint(java.util.Date d) 功能:將時間換為從
1900年至?xí)r間的秒數(shù)
public synchronized String overlengthcut(String str,int length) 功能:
截取前幾個字符,單位為漢字字長
public synchronized String replace(String str,String suba,String subb)
功能:字符串替換
*********************************************/
private static final String isostr="ISO8859-1";
private static final String gbkstr="GBK";
public StringCtrl()
{
}
public synchronized boolean emailcheck (String email)
{
if(email==null)return false;
if(email.length()<6)return false;
if(email.indexOf("@")<2)return false;
if(email.indexOf(".")<4)return false;
if(email.endsWith(".")||email.endsWith("@"))return false;
if(email.lastIndexOf("@")>email.lastIndexOf(".")-1)return false;
if(email.lastIndexOf("@")!=email.indexOf("@"))return false;
String[] lowstr={"\@#","\"","\n","&","\t","\r","<",">","/","\\","#"};
for(int i=0;i<lowstr.length;i++)if(email.indexOf("lowstr")>0)return
false;
return true;
}
public synchronized boolean isemailstr(String email)
{
if(email==null)return false;
if(email.indexOf("@")==-1||email.indexOf(".")==-1||email.length()<6)
return false;
return true;
}
public synchronized boolean isqqstr(String qq)
{
if(qq==null)return false;
if(qq.length()>12)return false;
if(qq.length()<5)return false;
for(int i=0;i<qq.length();i++)
if(!(((int)qq.charAt(i))<=57&&((int)qq.charAt(i))>=48))return false;
return true;
}
public synchronized boolean isnumstr(String num)
{
if(num==null)return false;
if(num.length()<1)return false;
for(int i=0;i<num.length();i++)
if(!(((int)num.charAt(i))<=57&&((int)num.charAt(i))>=48))return false;
return true;
}
public synchronized String userstrlow(String user)
{
String newuserstr=user.trim();
char[] lowstr=
{@#\@#@#,@#\"@#,@#\n@#,@#&@#,@#\t@#,@#\r@#,@#<@#,@#>@#,@#/@#,@#\\@#,@##
@#};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],@#+@#);
return newuserstr;
}
public synchronized boolean userstrchk(String user)
{
String newuserstr=user.trim();
char[] lowstr=
{@#\@#@#,@#\"@#,@#\n@#,@#&@#,@#\t@#,@#\r@#,@#<@#,@#>@#,@#/@#,@#\\@#,@##
@#,@#~@#,@#`@#,@#!@#,@#@@#,@#$@#,@#%@#,@#^@#,@#*@#,@#(@#,@#)@#,@#-
@#,@#_@#,@#+@#,@#=@#,@#|@#,@#?@#,@#,@#,@#;@#,@#.@#};
for(int i=0;i<lowstr.length;i++)
newuserstr=newuserstr.replace(lowstr[i],@#+@#);
return (user.equals(newuserstr))?true:false;
}
public synchronized boolean istelstr(String tel)
{
if(tel==null)return false;
if(tel.length()<1)return false;
if(tel.length()>32)return false;
for(int i=0;i<tel.length();i++)
if(!(((int)tel.charAt(i))<=57&&((int)tel.charAt(i))>=48))if(tel.charAt
(i)!=@#-@#)return false;
return true;
}
public synchronized boolean urlcheck(String url)
{
if(url==null)return false;
if(url.length()<10)return false;
String urls=url.toLowerCase();
if(!urls.startsWith("http://"))return false;
if(url.indexOf("<")>0||url.indexOf(">")>0)return false;
return true;
}
public synchronized String isotogbk(String iso)throws Exception
{
if(iso!=null)return (new String(iso.getBytes(isostr),gbkstr));
if(iso.length()<1)return "";
return null;
}
public synchronized String gbktoiso(String gbk)throws Exception
{
if(gbk!=null)return (new String(gbk.getBytes(gbkstr),isostr));
if(gbk.length()<1)return "";
return null;
}
public synchronized String HTMLcode(String TXTcode)
{
String newstr="";
if(TXTcode==null)return "";
newstr=TXTcode;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,"\"",""");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"\@#","'");
return newstr;
}
public synchronized String Unhtmlcode(String str)
{
String newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
//newstr=replace(newstr,""","\"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,""","\"");
//newstr=replace(newstr,"<","<");
//newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","\@#");
return newstr;
}
public synchronized String Unhtmlcodea(String str)
{
String newstr="";
if(str==null)return "";
if(str.length()<1)return "";
newstr=str;
newstr=replace(newstr,"&","&");
newstr=replace(newstr,""","\"");
newstr=replace(newstr," "," ");
newstr=replace(newstr,"<","<");
newstr=replace(newstr,">",">");
newstr=replace(newstr,"'","\@#");
return newstr;
}
public synchronized String dostrcut(String oldstr,int length)
{
int i=0;
int j=0;
int k=0;
String newstr="";
if(oldstr==null)return "";
if(length<=0)return "";
for(i=0;i<oldstr.length();i++)
{
if(oldstr.charAt(i)==@#\n@#)j=0;
else if(((int)(oldstr.charAt(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
newstr=newstr.concat(oldstr.substring(k,i)+"\n");
k=i;
j=0;
}
}
newstr=newstr.concat(oldstr.substring(k)+"\n");
return newstr;
}
public synchronized String inttodateshow(int datenum)
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
String datestr="";
java.util.Date d;
d=new java.util.Date((long)(datenum)*1000);
java.util.Calendar ds=java.util.Calendar.getInstance();
ds.setTime(d);
year=ds.get(java.util.Calendar.YEAR);
month=ds.get(java.util.Calendar.MONTH);
day=ds.get(java.util.Calendar.DATE);
hour=ds.get(java.util.Calendar.HOUR_OF_DAY);
minute=ds.get(java.util.Calendar.MINUTE);
second=ds.get(java.util.Calendar.SECOND);
datestr=Integer.toString(year)+"/"+Integer.toString(1+month)
+"/"+Integer.toString(day);
return datestr;
}
public synchronized String nowdateshow()
{
int year=0;
int month=0;
int day=0;
int hour=0;
int minute=0;
int second=0;
String datestr="";
java.util.Calendar ds=java.util.Calendar.getInstance();
year=ds.get(java.util.Calendar.YEAR);
month=ds.get(java.util.Calendar.MONTH);
day=ds.get(java.util.Calendar.DATE);
hour=ds.get(java.util.Calendar.HOUR_OF_DAY);
minute=ds.get(java.util.Calendar.MINUTE);
second=ds.get(java.util.Calendar.SECOND);
datestr=Integer.toString(year)+"/"+Integer.toString(1+month)
+"/"+Integer.toString(day);
return datestr;
}
public synchronized java.util.Date inttodate(int datenum)
{
int year=0;
int month=0;
int day=0;
String datestr="";
java.util.Date d;
d=new java.util.Date((long)(datenum)*1000);
return d;
}
public synchronized int datetoint()
{
java.util.Date d=null;
long ds=0;
d=new java.util.Date();
ds=d.getTime();
return (int)(ds/1000);
}
public synchronized int datetoint(java.util.Date d)
{
long ds=0;
ds=d.getTime();
return (int)(ds/1000);
}
public synchronized String overlengthcut(String str,int length)
{
int i=0;
int j=0;
if(str==null)return "";
if(length<0)return "";
if(str.length()<=length)return str;
for(i=0;i<str.length();i++)
{
if(((int)(str.charAt(i)))>255)j+=2;
else j++;
if((j/2)>=length)
{
return str.substring(0,i);
}
}
return str;
}
public synchronized String replace(String str,String suba,String subb)
{
String newstr="";
int start=0;
int offset=0;
int subalength=0;
int strlength=0;
if(str==null||suba==null||subb==null)return str;
if(suba.equals(subb))return str;
if(str.length()<suba.length()||str.length()<subb.length())return str;
if(str.length()>0&&suba.length()>0&&subb.length()>0)
{
subalength=suba.length();
strlength=str.length();
while(true)
{
if(str.indexOf(suba)<0)break;
if(offset>strlength)break;
start=str.indexOf(suba,offset);
if(start<offset)break;
newstr=newstr.concat(str.substring(offset,start));
newstr=newstr.concat(subb);
offset=start+subalength;
}
newstr=newstr.concat(str.substring(offset));
return newstr;
}
else
{
return str;
}
}
}