| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
29 | 30 | 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 | 9 |
平臺(tái):Lucene 2.1.0,JRE 1.4,Oracle 10g,IBM Web Sphere。
數(shù)據(jù)表:Article。字段:ID(自動(dòng)增長(zhǎng)),Title(String),Content(String)。共有550000條記錄。
對(duì)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("開始索引時(shí)間:"+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é)束時(shí)間:"+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/**/ /*建立索引代碼,查找時(shí)注釋*/
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//對(duì)中文建立解析(必須)
29 QueryParser parser=new QueryParser("Title",new ChineseAnalyzer());
30 Query query=parser.parse("李湘");
31
32 Date startDate=new Date();
33 System.out.println("檢索開始時(shí)間:"+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)測(cè)試,建立索引文件大概花了11分鐘。一般情況下,和用SQL執(zhí)行LIKE查詢差不多。
當(dāng)然,這只是我的粗略測(cè)試。最近一階段,我會(huì)對(duì)Lucene進(jìn)行代碼深入研究。
摘要: 搜索流程中的第二步就是構(gòu)建一個(gè)Query。下面就來介紹Query及其構(gòu)建。 當(dāng)用戶輸入一個(gè)關(guān)鍵字,搜索引擎接收到后,并不是立刻就將它放入后臺(tái)開始進(jìn)行關(guān)鍵字的檢索,而應(yīng)當(dāng)首先對(duì)這個(gè)關(guān)鍵字進(jìn)行一定的分析和處理,使之成為一種后臺(tái)可以理解的形式,只有這樣,才能提高檢索的效率,同時(shí)檢索出更加有效的結(jié)果。那么,在Lucene中,這種處理,其實(shí)就是構(gòu)建一個(gè)Query對(duì)象。 就Query對(duì)象本身言,它只是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
對(duì)JAVA語言的十個(gè)常見誤解
http://www.zebcn.com/html/200411\114.html
簡(jiǎn)析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é)者比較愛出錯(cuò)的運(yùn)算問題
http://www.zebcn.com/html/200411\131.html
類注釋文檔編寫方法
http://www.zebcn.com/html/200411\132.html
對(duì)JAVA語言的十個(gè)常見誤解
http://www.zebcn.com/html/200411\133.html
簡(jiǎn)析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ù)庫(kù)的實(shí)例
http://www.zebcn.com/html/200411\137.html
在Java中實(shí)現(xiàn)回調(diào)過程
http://www.zebcn.com/html/200411\138.html
Java 中對(duì)文件的讀寫操作之比較
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í)間的函數(shù)
http://www.zebcn.com/html/200412\177.html
JAVA-如何實(shí)現(xiàn)TIMER功能
http://www.zebcn.com/html/200412\179.html
數(shù)據(jù)庫(kù)訪問簡(jiǎn)單實(shí)現(xiàn)
http://www.zebcn.com/html/200412\180.html
將數(shù)據(jù)庫(kù)操作封裝到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的簡(jiǎn)單例子
http://www.zebcn.com/html/200412\192.html
JSP WEBServer的實(shí)現(xiàn)原理
http://www.zebcn.com/html/200412\193.html
Spring 入門(一個(gè)簡(jiǎn)單的例子)
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
第一個(gè)EJB3.0范例
http://www.zebcn.com/html/200502\213.html
Java RMI 簡(jiǎn)單示例
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個(gè)基本概念
http://www.zebcn.com/html/200502\222.html
java常用的加密,解密,數(shù)字簽名等API
http://www.zebcn.com/html/200502\227.html
一種簡(jiǎn)單的struts級(jí)連菜單實(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ù)庫(kù)的實(shí)例
http://www.zebcn.com/html/200504\241.html
Java常見問題集錦(來自Sun中國(guó)官方站)
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對(duì)數(shù)字證書的常用操作
http://www.zebcn.com/html/200510\275.html
三種整合Struts 應(yīng)用程序與Spring的方式
http://www.zebcn.com/html/200511\278.html
[分享]Ant簡(jiǎn)介
http://www.zebcn.com/html/200511\282.html
Struts 的動(dòng)態(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
一個(gè)生成無重復(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工具類庫(kù)
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è)計(jì)
http://www.zebcn.com/html/200601\376.html
用java做的滾動(dòng)的正弦曲線
http://www.zebcn.com/html/200601\378.html
EJB3.0和Spring比較
http://www.zebcn.com/html/200601\379.html
Struts 中常見錯(cuò)誤
http://www.zebcn.com/html/200601\380.html
漫談Java數(shù)據(jù)庫(kù)存取技術(shù)
http://www.zebcn.com/html/200601\381.html
Java多線程程序設(shè)計(jì)
http://www.zebcn.com/html/200601\385.html
JAVA中的時(shí)間操作
http://www.zebcn.com/html/200601\386.html
12個(gè)最重要的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個(gè)技巧
http://www.zebcn.com/html/200601\397.html
java程序得到域名對(duì)應(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個(gè)主要新特征
http://www.zebcn.com/html/200602\419.html
運(yùn)用類反射機(jī)制簡(jiǎn)化Struts應(yīng)用程序開發(fā)
http://www.zebcn.com/html/200602\420.html
用 Struts 實(shí)現(xiàn)動(dòng)態(tài)單選按鈕
http://www.zebcn.com/html/200602\421.html
JDO技術(shù)分析及企業(yè)應(yīng)用研究
http://www.zebcn.com/html/200602\422.html
一個(gè)J2EE項(xiàng)目的最小工具集
http://www.zebcn.com/html/200602\425.html
一個(gè)實(shí)現(xiàn)MD5的簡(jiǎn)潔的java類
http://www.zebcn.com/html/200602\429.html
服務(wù)器與瀏覽器的會(huì)話
http://www.zebcn.com/html/200602\434.html
使用Spring JMS簡(jiǎn)化異步消息處理
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
簡(jiǎn)化WebLogic 8.1項(xiàng)目的配置
http://www.zebcn.com/html/200603\476.html
WebLogic域配置策略-手動(dòng)和模板選項(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
簡(jiǎn)易的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存儲(chǔ)過程
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ù)庫(kù)后返回Iterator
http://www.zebcn.com/html/200604\3278.html
DOM屬性用法速查手冊(cè)
http://www.zebcn.com/html/200604\3279.html
論J2EE程序員的武功修為
http://www.zebcn.com/html/200604\3280.html
讓window服務(wù)進(jìn)程中自動(dòng)加載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方式對(duì)比
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>
方式二:這種方式的動(dòng)態(tài)多文件上傳是實(shí)現(xiàn)了的,很簡(jiǎ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>
方式三:動(dòng)態(tài)多文件上傳,只是在oFileInput.click();這個(gè)地方,這樣做就不能上傳這個(gè)文件了,因?yàn)榘l(fā)現(xiàn)它在上傳之時(shí)就把這個(gè)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;
}
//---新建一個(gè)文件顯示列表
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)建一個(gè)file input對(duì)象并返回
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) 評(píng)論(4) 編輯 收藏 引用 所屬分類: JS
呵呵,我的方法不知道和你的三種方法有沒有可比性,個(gè)人感覺還不錯(cuò)! @施偉 施偉 的做法,是不是還是不能解決,先選擇了一個(gè)文件,提交服務(wù)器之后這個(gè)file input域的值又被自動(dòng)清空的問題? 回復(fù) 更多評(píng)論 第二種方法不錯(cuò) 回復(fù) 更多評(píng)論 評(píng)論
# re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-01-27 13:20 施偉
做一個(gè) 添加附件 然后做一個(gè)type為file的input框,把此框和span定位重疊起來 把file框透明度設(shè)置為0 即完全看不到,但是確實(shí)存在。這個(gè)時(shí)候點(diǎn)span的時(shí)候就是在點(diǎn)這個(gè)file框 但是看不到file框子 是不是實(shí)現(xiàn)了呢? 然后再結(jié)合你第二種的方式給框編號(hào) 動(dòng)態(tài)增加就可以實(shí)現(xiàn)多文件上傳了 。
呵呵 我在我的程序里面這樣實(shí)現(xiàn)的 很好用 如果有興趣討論到我blog留言 或者發(fā)郵件給我吧 多交流。。。
回復(fù) 更多評(píng)論 # re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-01-29 18:00 重歸本壘(BNBN)
呵呵!施偉,你這樣做,如果實(shí)現(xiàn)了,那么比我的方法更勝一籌了,我以前也這樣考慮過,只是覺的好麻煩,而沒有去實(shí)現(xiàn)它!
另外,還非常謝謝你能關(guān)注我的Bolg!
回復(fù) 更多評(píng)論 # re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-02-12 17:12 路過的
# re: 幾種js實(shí)現(xiàn)的動(dòng)態(tài)多文件上傳 2007-06-04 11:28 sangern
[ 2007-7-27 16:28:00 | By: 葉尋飛 ]
初學(xué)JSP時(shí),寫了一些工具函數(shù)因?yàn)椴惶珪?huì)用JAVA下的正則表達(dá)式也只能這么寫啦!
發(fā)出來讓大家批評(píng)批評(píng)提點(diǎn)意見!有幾個(gè)函數(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) 功能:檢查一個(gè)字
符串是否符合E-Mail
public synchronized boolean isemailstr(String email) 功能:檢查一個(gè)字
符串是否符合E-Mail
public synchronized boolean isqqstr(String qq) 功能:檢查一個(gè)字符串是
否符合QQ
public synchronized boolean isnumstr(String num) 功能:檢查一個(gè)字符串
是否為一數(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) 功能:按
漢字長(zhǎng)換行(英文按半個(gè)字長(zhǎng))
public synchronized String inttodateshow(int datenum) 功能:將1900年至
時(shí)間的秒數(shù)換為日期字符串
public synchronized String nowdateshow() 功能:顯示當(dāng)前日期
public synchronized java.util.Date inttodate(int datenum) 功能:將秒數(shù)
轉(zhuǎn)換為日期
public synchronized int datetoint() 功能:將時(shí)間換為從1900年至今的秒
數(shù)
public synchronized int datetoint(java.util.Date d) 功能:將時(shí)間換為從
1900年至?xí)r間的秒數(shù)
public synchronized String overlengthcut(String str,int length) 功能:
截取前幾個(gè)字符,單位為漢字字長(zhǎng)
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;
}
}
}
在Java編程中,中文字體編碼難倒了不少程序員,如果抓住了影響Java中文顯示的幾個(gè)關(guān)鍵因素,問題將迎刃而解。
Java是目前最流行的面向?qū)ο蟮木幊陶Z言之一,Java支持UTF-8、ISO-8
859-1、GBK等各種字體編碼,可筆者發(fā)現(xiàn)Java中字體編碼的問題仍難倒了不少程序員,網(wǎng)上雖然也有不少關(guān)于在Java中如何正確顯示中文的文章,但都不夠全面,筆者特意總結(jié)如下。
影響Java中字體編碼正確顯示的有幾個(gè)因素: 1)數(shù)據(jù)庫(kù)的連接方式; 2)網(wǎng)頁中使用的字體編碼; 3)數(shù)據(jù)庫(kù)里存放數(shù)據(jù)的字體編碼; 4)Java的缺省字體編碼。如果在編程中遇到不能正確顯示中文時(shí),要先弄清楚以上幾項(xiàng)所使用的字體編碼,再分析找出原因,即可解決問題。
眾所周知,JSP是Java的一種,和網(wǎng)頁有關(guān),而網(wǎng)頁也有自己的中文編碼系統(tǒng),所以JSP處理中文要比純Java的類文件更為麻煩。本文的測(cè)試數(shù)據(jù)庫(kù)是MySQL3.2,數(shù)據(jù)庫(kù)連接驅(qū)動(dòng)是用org.gjt.mm.mysql.Driver,這里主要討論UTF-8和GBK的顯示( GB2312是GBK的一個(gè)子集,Java中可以使用GBK來代替GB系列)。我們先來研究JSP中字體編碼問題, 下面第一到第六點(diǎn)是針對(duì)JSP的(因?yàn)閺臄?shù)據(jù)庫(kù)里讀出中文數(shù)據(jù)與寫入中文數(shù)據(jù)有所區(qū)別,咱們分別說明,前三點(diǎn)是從讀取數(shù)據(jù)庫(kù)到顯示在網(wǎng)頁,后三點(diǎn)是從網(wǎng)頁輸入數(shù)據(jù)到存入數(shù)據(jù)庫(kù)),第七到第九點(diǎn)針對(duì)純Java的類文件。 以下rs表示ResultSet的一個(gè)實(shí)例,是執(zhí)行Select語句之后產(chǎn)生的數(shù)據(jù)集。
一、數(shù)據(jù)庫(kù)連接方式使用UTF-8
在連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)后面加上這句參數(shù)useUnicode=true&characterEncoding=
UTF-8,例如jdbc:mysql://localhost/DBVF?autoReconnect=true&useUnicode=
true&characterEncoding=UTF-8,從數(shù)據(jù)庫(kù)里讀出中文顯示在使用GBK的JSP的網(wǎng)頁里,如果數(shù)據(jù)庫(kù)里存放的字體編碼是UTF-8,在JSP中使用 str=new String(rs.getBytes(1),"UTF-8")或者str=rs.getString(1),可以正確顯示中文。如果數(shù)據(jù)庫(kù)里存放的是GBK數(shù)據(jù),那么JSP中也要使用str=new String(rs.getBytes(1),"GBK")來顯示正確的中文。值得注意的是如果頁面使用UTF-8,數(shù)據(jù)庫(kù)里存放的是UTF-8,也可以用str=new String(rs.getBytes(1),"GBK")正確顯示中文。如果網(wǎng)頁是UTF-8,而數(shù)據(jù)庫(kù)里存放的是GBK,無法直接顯示中文,需要2步轉(zhuǎn)換, str=new String(rs.getBytes(1),"GBK"); 再str=new String(str.getBytes("UTF-8"),"GBK"),才可以正確顯示中文。
二、數(shù)據(jù)庫(kù)連接方式使用GBK
在連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)后面加上這句參數(shù)useUnicode=true&characterEncoding=
GBK,例如jdbc:mysql://localhost/DBVF?autoReconnect=true&UseUnicode=true&
characterEncoding=GBK,從數(shù)據(jù)庫(kù)里讀出中文,顯示在使用GBK的JSP的網(wǎng)頁里,如果數(shù)據(jù)庫(kù)里存放的字體編碼是UTF-8,在JSP中一定要使用 str=new String(rs.getBytes(1),"UTF-8"),才正確顯示中文。如果數(shù)據(jù)庫(kù)里存放的是GBK數(shù)據(jù),那么JSP中也要使用str=new String(rs.getBytes(1),"GBK") 或者直接使用str=rs.getString(1),即可顯示正確的中文。 如果網(wǎng)頁是UTF-8,而數(shù)據(jù)庫(kù)里存放的是GBK,只能用str=new String(rs.getString(1).getBytes("UTF-8"),"GBK")的方法來顯示中文; 如果網(wǎng)頁是UTF-8,而數(shù)據(jù)庫(kù)里存放的是UTF-8,可用str=new String(rs.getBytes(1),"GBK") 或者rs.getString(1)方法來顯示中文。
三、使用缺省數(shù)據(jù)庫(kù)連接方式
連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)后面沒有這句參數(shù)useUnicode=&characterEncoding=,例如jdbc:mysql://localhost/DBVF?autoReconnect=true,沒有參數(shù)useUnicode=true&characterEncoding,表示使用默認(rèn)的ISO-8895-1編碼。
1. 從數(shù)據(jù)庫(kù)里讀出中文,顯示在GBK的網(wǎng)頁里。如果數(shù)據(jù)庫(kù)里存放的字體編碼是UTF-8,在JSP網(wǎng)頁中一定要使用語句 str=new String(rs.getBytes(1),"UTF-8") 或者str= new String(rs.getString(1).getBytes("ISO-8859-1"),"UTF-8"),才可正確顯示中文。如果數(shù)據(jù)庫(kù)里存放的是GBK數(shù)據(jù),那么JSP中也要使用str=new String(rs.getBytes(1),"GBK")或str=new String(rs.getString(1).getBytes("ISO-8859-1"),"GBK") 顯示正確的中文。
2. 如果網(wǎng)頁是UTF-8,不能直接正確顯示GBK,需要2步轉(zhuǎn)換,str=new String(rs.getBytes(1),"GBK"),再str=new String(str.getBytes("UTF-8"),"GBK") 才可以正確顯示中文。如果數(shù)據(jù)庫(kù)里存的是UTF-8,直接用str=new String(rs.getBytes(1),"GBK")或者str=new String(rs.getString(1).getBytes("ISO-8859-1"),"GBK")就可以顯示中文了。
以上是讀取數(shù)據(jù)庫(kù)里中文正確顯示在網(wǎng)頁上,下面三點(diǎn)是如何正確存入數(shù)據(jù)庫(kù)。
四、數(shù)據(jù)庫(kù)連接方式使用UTF-8編碼
JSP中要把網(wǎng)頁輸入的中文存入數(shù)據(jù)庫(kù),通常有一個(gè)提交(Submit)的過程,是用str=request.getParameter("username"),然后執(zhí)行update或者insert語句來存入數(shù)據(jù)庫(kù)。如何賦值給str很重要,而且這里中文輸入與網(wǎng)頁所使用的字體編碼有關(guān)。
1、 網(wǎng)頁使用UTF-8,使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8")或者str= new String(request.getParameter("username").getBytes(),"UTF-8"),都可以使得存到數(shù)據(jù)庫(kù)里的數(shù)據(jù)是UTF-8編碼。
2. 網(wǎng)頁使用GBK,使用str= new String(request.getParameter("username").getBytes(),"GBK"),那么存入數(shù)據(jù)庫(kù)的是UTF-8編碼。
3. 值得注意的是使用UTF-8的數(shù)據(jù)庫(kù)連接方式不能存得GBK。
五、數(shù)據(jù)庫(kù)連接方式使用GBK編碼
1. 輸入使用GBK網(wǎng)頁,存到數(shù)據(jù)庫(kù)里是GBK的方法: str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK") 或者str= new String(request.getParameter("username").getBytes(),"GBK")。
2. 網(wǎng)頁使用GBK,想存入U(xiǎn)TF-8到數(shù)據(jù)庫(kù)里,要分2步: 先str=new String(request.getParameter("username").getBytes(),"GBK"),再str=new String(str.getBytes("UTF-8"),"GBK")即可。
3. 網(wǎng)頁使用UTF-8,而且使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"GBK") 或者str= new String(request.getParameter("username").getBytes(),"UTF-8"),那么存到數(shù)據(jù)庫(kù)里的數(shù)據(jù)是UTF-8編碼。
4. 網(wǎng)頁使用UTF-8,而且使用str= new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"),那么存到數(shù)據(jù)庫(kù)里的數(shù)據(jù)是GBK編碼。
六、數(shù)據(jù)庫(kù)連接方式使用缺省,即不使用參數(shù)useUnicode和characterEncoding
1. 網(wǎng)頁使用GBK,如果使用str= request.getParameter("username")或者str= new String(request.getParameter("username").getBytes()),那么在數(shù)據(jù)庫(kù)里的數(shù)據(jù)是GBK碼。網(wǎng)頁使用UTF-8 和使用str= request.getParameter("username"),則存入數(shù)據(jù)庫(kù)是UTF-8編碼。
2. 如果使用str= new String(request.getParameter("username").getBytes("ISO-8859-1")),那么根據(jù)網(wǎng)頁提供的字體編碼而存到數(shù)據(jù)庫(kù)里,比如是UTF-8的網(wǎng)頁,那么存到數(shù)據(jù)庫(kù)中就是UTF-8編碼,如果使用GBK網(wǎng)頁,那么存到數(shù)據(jù)庫(kù)里的字就是GBK編碼。
3. 如果使用str= new String(request.getParameter("username").getBytes("UTF-8"),"UTF-8")這一種組合能存到正確的數(shù)據(jù)外,其他存到數(shù)據(jù)庫(kù)里的數(shù)據(jù)則都是亂碼或者錯(cuò)誤碼。在這個(gè)UTF-8組合的特例中,網(wǎng)頁使用的是GBK,則存放到數(shù)據(jù)庫(kù)里就是GBK,網(wǎng)頁使用UTF-8,那么存到數(shù)據(jù)庫(kù)里的就是UTF-8。
4. 網(wǎng)頁是GBK的要存得UTF-8,一定需要2步: company=new String(request.getParameter("company").getBytes(),"GBK")和company=new String(company.getBytes("UTF-8"))。
5. 網(wǎng)頁是UTF-8的,不能存得GBK在數(shù)據(jù)庫(kù)里,一句話,改變數(shù)據(jù)庫(kù)連接方式不能存得GBK碼。
以上所有的都是基于JSP網(wǎng)頁和數(shù)據(jù)庫(kù)交換數(shù)據(jù),下面討論一下純JAVA編程下的字體編碼轉(zhuǎn)換。
七、數(shù)據(jù)庫(kù)連接方式使用UTF-8編碼
1. 數(shù)據(jù)庫(kù)里的中文是UTF-8,可以轉(zhuǎn)換為GBK,但不能把GBK存入數(shù)據(jù)庫(kù)。
2. 數(shù)據(jù)庫(kù)是GBK,如果轉(zhuǎn)換為UTF-8,使用content=new String(rs.getBytes(2),"GBK")直接將content存入數(shù)據(jù)庫(kù)就可為UTF-8。
八、數(shù)據(jù)庫(kù)連接方式使用GBK編碼
1. 數(shù)據(jù)庫(kù)里的中文是UTF-8,如果轉(zhuǎn)換為GBK,使用content= new String(rs.getString(2).getBytes(),"UTF-8"),再直接使用update或者insert語句插入到數(shù)據(jù)庫(kù),即存得GBK。如果使用content= new String(rs.getString(2).getBytes(),"GBK")或者content= new String(rs.getString(2).getBytes()),再存入數(shù)據(jù)庫(kù)即存得還是UTF-8編碼。
2. 數(shù)據(jù)庫(kù)里的中文是GBK,如果轉(zhuǎn)換為UTF-8,使用content= new String(rs.getString(2).getBytes("UTF-8"))或者content= new String(rs.getString(2).getBytes("UTF-8"),"GBK"),再直接使用update或者insert語句插入到數(shù)據(jù)庫(kù),即存得UTF-8。
3. 如果某個(gè)String是GBK,要轉(zhuǎn)換為UTF-8,也是使用content= new String(GBKstr.getBytes("UTF-8"))或者content= new String(GBKstr.getBytes("UTF-8"),"GBK"); 如果某個(gè)String是UTF-8,要轉(zhuǎn)換為GBK,應(yīng)該使用new String(UTFstr.getBytes("GBK"),"UTF-8")。
九、數(shù)據(jù)庫(kù)連接方式使用缺省,即不跟參數(shù)
1. str2=new String(GBKstr.getBytes("UTF-8"),"ISO-8859-1"),可以將數(shù)據(jù)庫(kù)里的GBK編碼轉(zhuǎn)換為UTF-8。
2. 讀取UTF-8然后存入U(xiǎn)TF-8,則用str1=new String(UTFstr.getBytes(),"ISO-8859-1")或者str1=new String(UTFstr.getBytes("GBK"),"ISO-8859-1")。
3. 不能實(shí)現(xiàn)數(shù)據(jù)庫(kù)里的UTF-8轉(zhuǎn)換為GBK。
如果采用UTF-8的數(shù)據(jù)庫(kù)連接方式或者缺省數(shù)據(jù)連接方式,那么無法將UTF-8轉(zhuǎn)為GBK;而GBK的數(shù)據(jù)庫(kù)連接方式可以實(shí)現(xiàn)UTF-8和GBK的相互轉(zhuǎn)換。建議大家采用GBK的數(shù)據(jù)連接方式。
第三步:對(duì)動(dòng)態(tài)輸出內(nèi)容編碼
每一個(gè)ISO-8859-1字符集中的字符都可以編碼為一個(gè)數(shù)值,完整的對(duì)應(yīng)關(guān)系請(qǐng)見下面的幾個(gè)表:
數(shù) 值
編碼表示法
含 義
表現(xiàn)形式
�-
-
Unused
-
-
HorizontalTab
space
-
Linefeed
space
-
-
Unused
-
-
Space
space
!
-
Exclamationmark
!
"
"
Quotationmark
"
#
-
Numbersign
#
$
-
Dollarsign
$
%
-
Percentsign
%
&
&
Ampersand
&
'
-
Apostrophe
'
(
-
Leftparenthesis
(
)
-
Rightparenthesis
)
*
-
Asterisk
*
+
-
Plussign
+
,
-
Comma
,
-
-
Hyphen
-
.
-
Period(fullstop)
.
/
-
Solidus(slash)
/
0-9
-
Digits(0-9)
0-9
:
-
Colon
:
;
-
Semi-colon
;
<
<
Lessthan
<
=
-
Equalssign
=
>
>
Greaterthan
>
?
-
Questionmark
?
@
-
Commercialat
@
A-Z
-
UppercaseA-Z
A-Z
[
-
Leftsquarebracket
[
\
-
Reversesolidus(backslash)
\
]
-
Rightsquarebracket
]
^
-
Caret
^
_
-
Horizontalbar
_
`
-
Acuteaccent
`
a-z
-
Lowercasea-z
a-z
{
-
Leftcurlybrace
{
|
-
Verticalbar
|
}
-
Rightcurlybrace
}
~
-
Tilde
~
-?
-
Unused
-
Non-breakingspace
?
?
Invertedexclamation
?
¢
¢
Centsign
¢
£
£
Poundsterlingsign
£
¤
¤
Generalcurrencysign
¤
¥
¥
Yensign
¥
&brVBar;
|
Brokenverticalbar
|
§
§
Sectionsign
§
¨
¨
UMLaut(dierisis)
¨
?
?
Copyright
?
a
a
Feminineordinal
a
?
?
Leftanglequote,guillemotleft
?
?
?
Notsign
?
Softhyphen
?
?
ReGISteredtrademark
?
ˉ
ˉ
Macronaccent
ˉ
°
°
Degreesign
°
±
±
Plusorminus
±
2
2
Superscripttwo
2
3
3
Superscriptthree
3
′
′
Acuteaccent
′
μ
μ
Microsign
μ
?
?
Paragraphsign
?
·
·
Middledot
·
?
?
Cedilla
?
1
1
Superscriptone
1
o
o
Masculineordinal
o
?
?
Rightanglequote,guillemotright
?
?
?
Fraction(onequarter)
?
?
?
Fraction(onehalf)
?
?
?
Fraction(threequarters)
?
?
?
Invertedquestionmark
?
à
à
CapitalA,graveaccent
à
á
á
CapitalA,acuteaccent
á
?
?
CapitalA,circumflexaccent
?
?
?
CapitalA,tilde
?
?
?
CapitalA,umlaut(dierisis)
?
?
?
CapitalA,ring
?
?
?
CapitalAEdipthong(ligature)
?
?
?
CapitalC,cedilla
?
è
è
CapitalE,graveaccent
è
é
é
CapitaE,acuteaccent
é
ê
ê
CapitalE,circumflexaccent
ê
&EUML;
?
CapitalE,umlaut(dierisis)
?
ì
ì
CapitalI,graveaccent
ì
í
í
CapitalI,acuteaccent
í
?
?
CapitalI,circumflexaccent
?
?
?
CapitalI,umlaut(dierisis)
?
D
D
CapitalEth,Icelandic
D
?
?
CapitalN,tilde
?
ò
ò
CapitalO,graveaccent
ò
ó
ó
CapitalO,acuteaccent
ó
?
?
CapitalO,circumflexaccent
?
?
?
CapitalO,tilde
?
?
?
CapitalO,umlaut(dierisis)
?
×
×
Multiplysign
×
?
?
CapitalO,slash
?
ù
ù
CapitalU,graveaccent
ù
ú
ú
CapitalU,acuteaccent
ú
?
?
CapitalU,circumflexaccent
?
ü
ü
CapitalU,umlaut(dierisis)
ü
Y
Y
CapitalY,acuteaccent
Y
T
T
CapitalThorn,Icelandic
T
?
?
Smallsharps,German(szligature)
?
à
à
Smalla,graveaccent
à
á
á
Smalla,acuteaccent
á
a
a
Smalla,circumflexaccent
a
?
?
Smalla,tilde
?
&aUML;
?
Smalla,umlaut(dierisis)
?
?
?
Smalla,ring
?
?
?
Smallaedipthong(ligature)
?
?
?
Smallc,cedilla
?
è
è
Smalle,graveaccent
è
é
é
Smalle,acuteaccent
é
ê
ê
Smalle,circumflexaccent
ê
?
?
Smalle,umlaut(dierisis)
?
ì
ì
Smalli,graveaccent
ì
í
í
Smalli,acuteaccent
í
?
?
Smalli,circumflexaccent
?
?
?
Smalli,umlaut(dierisis)
?
e
e
Smalleth,Icelandic
e
?
?
Smalln,tilde
?
ò
ò
Smallo,graveaccent
òò
ó
ó
Smallo,acuteaccent
ó
?
?
Smallo,circumflexaccent
?
?
?
Smallo,tilde
?
?
?
Smallo,umlaut(dierisis)
?
÷
÷
Divisionsign
÷
?
?
Smallo,slash
?
ù
ù
Smallu,graveaccent
ù
ú
ú
Smallu,acuteaccent
ú
?
?
Smallu,circumflexaccent
?
ü
ü
Smallu,umlaut(dierisis)
ü
y
y
Smally,acuteaccent
y
t
t
Smallthorn,Icelandic
t
?
?
Smally,umlaut(dierisis)
?
根據(jù)上表,我們來看2個(gè)應(yīng)用實(shí)例:
? 2000 Some Co., Inc.:用?表示版權(quán)標(biāo)識(shí)信息。
? 2000 Some Co., Inc.:用?同樣標(biāo)識(shí)版權(quán)標(biāo)識(shí)信息。
對(duì)不被信任的數(shù)據(jù)進(jìn)行編碼要優(yōu)于過濾不被信任的數(shù)據(jù)。在客戶端瀏覽器需要顯示一些特殊字符的情況下,這種處理方式會(huì)恢復(fù)特殊字符的廬山真面目。當(dāng)然,對(duì)所有不被信任的數(shù)據(jù)進(jìn)行編碼,是個(gè)相當(dāng)耗費(fèi)資源的工作。根據(jù)需要,Web開發(fā)者可以在編碼和過濾2種方法中進(jìn)行權(quán)衡,選擇一種或者混合使用。
該文章轉(zhuǎn)載自1024k:http://www.1024k.cn/web/2007/200701/15407.html
第二步:鑒別特殊的字符
什么是特殊字符?HTML定義如下:特殊字符就是那些能夠影響頁面顯示效果的字符。依據(jù)上下文的不同,特殊字符也會(huì)有所不同。下面我們來分類看看:
● 在塊級(jí)別元素所包含的內(nèi)容中應(yīng)考慮的特殊字符有:
<:引入一個(gè)標(biāo)記
&:引入一個(gè)字符實(shí)體
>:結(jié)束一個(gè)標(biāo)記
● 在屬性值中應(yīng)考慮的特殊字符有:
":在以雙引號(hào)包裹屬性值的情況下,"標(biāo)記了屬性值的結(jié)尾。
':在以單引號(hào)包裹屬性值的情況下,'標(biāo)記了屬性值的結(jié)尾。
空白字符:在屬性值沒有被任何引號(hào)包裹的情況下,空白字符標(biāo)記了屬性值的結(jié)尾,比如空格、tab。
&:當(dāng)需要在屬性值中引入字符實(shí)體時(shí),就需要使用&。
● 搜索引擎系統(tǒng)會(huì)在搜索結(jié)果頁面中包含一個(gè)再次運(yùn)行搜索的鏈接,這個(gè)鏈接中會(huì)包含編碼過的搜索查詢字符串。這種情況下應(yīng)考慮的特殊字符有:
空格、tab以及換行符:它們標(biāo)記了URL的結(jié)尾。
&:它標(biāo)記一個(gè)字符實(shí)體,或者一個(gè)獨(dú)立的CGI參數(shù)。
非ASCII字符:就是ISO-8859-1編碼中ASCII碼大于128的字符,它們不允許在URL中使用。
%:無論服務(wù)器對(duì)由escape編碼的HTTP參數(shù)如何解碼,都必須過濾出字符%。
● 在 之間的內(nèi)容中應(yīng)考慮的特殊字符有:分號(hào),圓括號(hào),大括號(hào)以及換行符。
● 在服務(wù)器端腳本中應(yīng)考慮的特殊字符有:!
● 其他情況下應(yīng)考慮的特殊字符有:=。但注意,現(xiàn)有的攻擊事件還沒有利用過這個(gè)字符,但作為一個(gè)全面考慮,這里還是列出來。
另外,我們還要重視一種情況:其他附加字符也可能包含在特殊字符列表中。總之,Web開發(fā)者必須對(duì)應(yīng)用程序的輸入內(nèi)容進(jìn)行檢查,以確定哪些字符會(huì)影響程序的執(zhí)行。
該文章轉(zhuǎn)載自1024k:http://www.1024k.cn/web/2007/200701/15408.html
在各種Internet攻擊行為中,通過Web方式侵入系統(tǒng)造成信息泄漏、數(shù)據(jù)丟失的事件非常普遍。例如,攻擊者在頁面表單中輸入惡意內(nèi)容,繞過輸入檢查,攻擊系統(tǒng)。為此,Web開發(fā)者應(yīng)采取有效的措施從信息采集的入口處堵截惡意內(nèi)容的進(jìn)入。本文將對(duì)這方面的問題進(jìn)行分析,并提供幾種應(yīng)對(duì)方法。
問題分析
Web頁面包含文本和HTML標(biāo)記,它們由服務(wù)器建立,被客戶端解釋。HTML標(biāo)記一般被服務(wù)器特殊對(duì)待。例如,“<”一般指示一個(gè)HTML標(biāo)記的開始,“<P>”可以影響頁面的顯示格式,<Script>可以將腳本代碼引入瀏覽器而執(zhí)行。
對(duì)于靜態(tài)頁面,服務(wù)器可以完全控制它在客戶端如何解釋。但是對(duì)于動(dòng)態(tài)頁面,服務(wù)器就不可能完全控制它在客戶端的解釋行為了。這樣就產(chǎn)生了問題:如果動(dòng)態(tài)頁面中包含了不可信的內(nèi)容,那么無論是服務(wù)器端還是客戶端,就不能保證是否會(huì)發(fā)生安全問題。
現(xiàn)在,幾乎所有的商用Web服務(wù)器就會(huì)建立動(dòng)態(tài)頁面。最典型的一個(gè)例子就是我們經(jīng)常要使用的搜索引擎,它接受用戶的查詢內(nèi)容后,搜索數(shù)據(jù)庫(kù),然后將動(dòng)態(tài)內(nèi)容寫入一個(gè)頁面模板,最后顯示給用戶包含搜索結(jié)果的頁面。這種情況下,檢查動(dòng)態(tài)內(nèi)容是否包含了特殊字符就非常重要,例如“<”。如果包含了特殊字符,用戶端的瀏覽器就可能將之誤解為HTML標(biāo)記或者引入執(zhí)行程序,而不是當(dāng)做文本信息顯示出來。危險(xiǎn)也就產(chǎn)生于此!如果不對(duì)動(dòng)態(tài)頁面進(jìn)行特殊字符的檢查,那么攻擊者就有可能在交互頁面的輸入表單中寫入些特殊字符串,從而導(dǎo)致輸出頁面執(zhí)行非法行為。這種例子很多,比如我們編寫一個(gè)留言簿,卻不對(duì)輸入內(nèi)容進(jìn)行特殊字符的校驗(yàn),那么攻擊者就有可能填寫特殊字符,最終導(dǎo)致留言簿頁面的非正常工作,例如填寫一段惡意代碼:死循環(huán)JavaScript腳本、重復(fù)打開窗口的JavaScript腳本。
應(yīng)對(duì)措施分析
通常,不被信任的內(nèi)容主要來自以下幾個(gè)方面:URL參數(shù),表單輸入元素,Cookies和數(shù)據(jù)庫(kù)查詢。要減輕這些方面可能導(dǎo)致的攻擊,建議采取如下的步驟:
1、為每個(gè)由服務(wù)器產(chǎn)生的Web頁面明確地設(shè)置字符集編碼
2、鑒別特殊的字符
3、對(duì)動(dòng)態(tài)輸出內(nèi)容編碼
4、過濾動(dòng)態(tài)輸出內(nèi)容中的特殊字符
5、檢查cookies 值
以下詳細(xì)分析這5個(gè)步驟。
第一步:明確地設(shè)置字符集編碼
字符集編碼就是頁面的字符編碼體系,在瀏覽器中可以通過“查看/編碼”來轉(zhuǎn)換:
很多Web頁面都省略了字符集編碼設(shè)置,也就是說在頁面源代碼頭部HTTP一節(jié)中沒有定義charset參數(shù)。早期的HTML版本中,如果沒有定義charset參數(shù),字符集編碼就默認(rèn)為ISO-8859-1。但實(shí)際上,許多瀏覽器都有各自的默認(rèn)字符集編碼。因此,HTML版本4規(guī)定,如果沒有指定charset參數(shù),任何可能的字符集編碼都可以使用,這就依賴于用戶的瀏覽器種類了。
如果Web服務(wù)器不能指定使用哪個(gè)字符集編碼,那么它就不能區(qū)分出特殊字符。沒有指定字符集編碼的Web頁面之所以可以在大多數(shù)時(shí)間中工作良好,這是因?yàn)樵诖蠖鄶?shù)的字符集編碼類別中,同一字符對(duì)應(yīng)一個(gè)小于128的字節(jié)值。對(duì)于大于128的特殊字符,例如“<”,將采用16位字符編碼方案處理。一些瀏覽器能夠識(shí)別并執(zhí)行這種編碼方案,但同時(shí),攻擊者也可能據(jù)此使用惡意腳本,使防范難度加大,因?yàn)榉?wù)器可能完全不能了解哪個(gè)字節(jié)代表哪個(gè)特殊字符。例如,字符集UTF-7為“<”和“>”提供了可選編碼,幾種流行的瀏覽器一般將它們看做標(biāo)記的起始和結(jié)束字符。為了不造成服務(wù)器和客戶端的字符編碼的不一致,Web服務(wù)器應(yīng)該明確設(shè)置字符集,以確認(rèn)插入的數(shù)據(jù)是否為特殊字符編碼的后續(xù)字節(jié)。比如,下面的代碼強(qiáng)行設(shè)置了頁面使用ISO-8859-1字符集編碼:
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>HTML SAMPLE</TITLE>
</HEAD>
<BODY>
<P>This is a sample HTML page
</BODY>
</HTML>
該文章轉(zhuǎn)載自1024k:http://www.1024k.cn/web/2007/200701/15409.html
本來以為上一次的配置就搞定了,結(jié)果本地測(cè)試好好的,到了服務(wù)器上調(diào)試就完蛋了,本地只測(cè)試了一個(gè)ASP站和一個(gè)JSP站,而實(shí)際情況是多個(gè)asp站和jsp站,又試了兩次還是不行,終于在第三次嘗試后搞定了,寫下來做個(gè)紀(jì)念。
第一次嘗試使用:
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.5hope.com
DcumentRoot "G:\5hope
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.5hope.com:88/
ProxyPassReverse / www.5hope.com:88/
</VirtualHost>
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
本以為這樣設(shè)置多站點(diǎn)就搞定了,結(jié)果發(fā)現(xiàn)只識(shí)別第一個(gè)站點(diǎn),訪問別的站點(diǎn)都是這個(gè)站的內(nèi)容,折騰了一上午,沒成功。
第二次嘗試使用:
<VirtualHost *:80>
#添加了這個(gè)屬性**********
ProxyPreserveHost On
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "D:\tomcat5.0.28\conf\workers.properties"
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.openria.cn
DirectoryIndex index.html index.htm index.jsp
JkMount /* ajp13
JkAutoAlias "D:\tomcat-5.0.28\Webapps\ria"
<Directory "D:\tomcat-5.0.28\webapps\ria">
Options Indexes FollowSymLinks
allow from all
</Directory>
</VirtualHost>
這回經(jīng)過查官方資料,發(fā)現(xiàn)了一個(gè)屬性,叫ProxyPreserveHost On,試了一下,是可以用實(shí)現(xiàn)多個(gè)虛擬的asp站點(diǎn)了,但是和我的tomcat站點(diǎn)定義沖突,訪問不了jsp站,又不行,只好再找。
第三次嘗試使用:
NameVirtualHost *:80
<VirtualHost *:80>
ProxyPreserveHost On
ServerAdmin feifei0658@sina.com
ServerName www.shundabanjia.com
DocumentRoot "G:\wuyubing\www"
DirectoryIndex index.html index.htm index.asp
ProxyPass / http://www.shundabanjia.com:88/
ProxyPassReverse / http://www.shundabanjia.com:88/
</VirtualHost>
LoadModule jk_module modules/mod_jk.so
JkWorkersFile "D:\tomcat5.0.28\conf\workers.properties"
<VirtualHost *:80>
ServerAdmin feifei0658@sina.com
ServerName www.openria.cn
DirectoryIndex index.html index.htm index.jsp
JkMount /* ajp13
JkAutoAlias "D:\tomcat-5.0.28\webapps\ria"
<Directory "D:\tomcat-5.0.28\webapps\ria">
Options Indexes FollowSymLinks
allow from all
</Directory>
</VirtualHost>
經(jīng)過反復(fù)看文檔,這回終于搞定了,原來是沒有吧"Use name-based virtual hosting."打開,去掉NameVirtualHost *:80前面的#號(hào)就可以了,真是暈啊。
總算成功了,看來有問題還需要看官方資料,網(wǎng)友的資料還是不完整啊,通過自己的努力,發(fā)現(xiàn)新的線索:
ProxyPreserveHost On
NameVirtualHost *:80
這也是自己的收獲啊,希望這些經(jīng)歷能幫助需要他的人。
再次慶祝一下,自己的網(wǎng)站終于要開通了,歡迎訪問:www.openria.cn
該文章轉(zhuǎn)載自1024k:http://www.1024k.cn/web/2007/200701/15431.html