JAVA StringUitl
import
?java.io.IOException;
import ?java.io.UnsupportedEncodingException;
import ?java.text.ParseException;
import ?java.text.SimpleDateFormat;
import ?java.util.Calendar;
import ?java.util.Date;
import ?javax.servlet.ServletContext;
import ?javax.servlet.http.Cookie;
import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.jsp.JspWriter;
import ?javax.servlet.jsp.PageContext;
/**
?*?StringUtil,?字符串工具類,?一些方便的字符串工具方法.
?*
?*?Dependencies:?Servlet/JSP?API.
?*
?*? @author ?劉長炯
?*? @version ?1.3?2009-03-8
? */
public ? class ?StringUtil?{
????
???? /**
?????*?格式化小數保持最少和最多小數點.
?????*?
?????*? @param ?num
?????*? @param ?minFractionDigits
?????*? @param ?maxFractionDigits
?????*? @return
????? */
???? public ? static ?String?formatFraction( double ?num,? int ?minFractionDigits,? int ?maxFractionDigits)?{
???????? // ?輸出固定小數點位數
????????java.text.NumberFormat?nb? = ?java.text.NumberFormat.getInstance();
????????nb.setMaximumFractionDigits(maxFractionDigits);
????????nb.setMinimumFractionDigits(minFractionDigits);
????????nb.setGroupingUsed( false );
????????String?rate? = ?nb.format(num);
????????
???????? return ?rate;
????}
????
???? /**
?????*?從請求中獲取當前頁碼數.
?????*? @param ?request
?????*? @return ?頁碼數,?不小于1
????? */
???? public ? static ? int ?getCurrentPage(HttpServletRequest?request)?{
????????String?pname? = ? " cp " ;
???????? // ?獲取頁碼數
???????? if ?(request.getParameter(pname)? != ? null ? && ? ! "" .equals(request.getParameter(pname)))?{
???????????? return ?Integer.parseInt(request.getParameter(pname));
????????}? else ?{
???????????? return ? 1 ;
????????}
????}
????
???? /**
?????*?將給定時間移動相對月份.
?????*? @param ?beginDate?起始日期
?????*? @param ?amount?數量
?????*? @return ?結果日期
????? */
???? public ? static ?Date?moveMonth(Date?beginDate,? int ?amount)?{
????????Calendar?cal? = ?Calendar.getInstance();
????????cal.setTime(beginDate);
????????
????????cal.add(Calendar.MONTH,?amount);
????????
???????? return ?cal.getTime();
????}
????
???? /**
?????*?將給定字符串時間移動相對月份.
?????*? @param ?year?年字符串
?????*? @param ?month?月
?????*? @param ?amount?數量
?????*? @return ?int[?年,?月?]
????? */
???? public ? static ? int []??moveMonth(String?year,?String?month,? int ?amount)?{
????????Date?d? = ?moveMonth(?parseDate(year? + ? " / " ? + ?month,? " yyyy/MM " ),?amount);
????????
????????Calendar?cal? = ?Calendar.getInstance();
????????cal.setTime(d);
????????
???????? return ? new ? int []?{?cal.get(Calendar.YEAR),?cal.get(Calendar.MONTH)};
????}
????
???? /**
?????*?解析日期.
?????*? @param ?input?輸入字符串
?????*? @param ?pattern?類型
?????*? @return ?Date?對象
????? */
???? public ? static ?Date?parseDate(String?input,?String?pattern)?{
???????? if (isEmpty(input))?{
???????????? return ? null ;
????????}
????????
????????SimpleDateFormat?df? = ? new ?SimpleDateFormat(pattern);
????????
???????? try ?{
???????????? return ?df.parse(input);
????????}? catch ?(ParseException?e)?{
???????????? // ?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}
????????
???????? return ? null ;
????}
????
???? /**
?????*?格式化日期為字符串.
?????*? @param ?date?日期字符串
?????*? @param ?pattern?類型
?????*? @return ?結果字符串
????? */
???? public ? static ?String?formatDate(Date?date,?String?pattern)?{
???????? if (date? == ? null ? || ?pattern? == ? null )?{? return ? null ;?}
???????? return ? new ?SimpleDateFormat(pattern).format(date);
????}
????
???? /**
?????*?Same?function?as?javascript's?escape().
?????*? @param ?src
?????*? @return
????? */
???? public ? static ?String?escape(String?src)?{
???????? int ?i;
???????? char ?j;
????????StringBuffer?tmp? = ? new ?StringBuffer();
????????tmp.ensureCapacity(src.length()? * ? 6 );
???????? for ?(i? = ? 0 ;?i? < ?src.length();?i ++ )?{
????????????j? = ?src.charAt(i);
???????????? if ?(Character.isDigit(j)? || ?Character.isLowerCase(j)
???????????????????? || ?Character.isUpperCase(j))
????????????????tmp.append(j);
???????????? else ? if ?(j? < ? 256 )?{
????????????????tmp.append( " % " );
???????????????? if ?(j? < ? 16 )
????????????????????tmp.append( " 0 " );
????????????????tmp.append(Integer.toString(j,? 16 ));
????????????}? else ?{
????????????????tmp.append( " %u " );
????????????????tmp.append(Integer.toString(j,? 16 ));
????????????}
????????}
???????? return ?tmp.toString();
????}
???? /**
?????*?Same?function?as?javascript's?unsecape().
?????*? @param ?src
?????*? @return
????? */
???? public ? static ?String?unescape(String?src)?{
????????StringBuffer?tmp? = ? new ?StringBuffer();
????????tmp.ensureCapacity(src.length());
???????? int ?lastPos? = ? 0 ,?pos? = ? 0 ;
???????? char ?ch;
???????? while ?(lastPos? < ?src.length())?{
????????????pos? = ?src.indexOf( " % " ,?lastPos);
???????????? if ?(pos? == ?lastPos)?{
???????????????? if ?(src.charAt(pos? + ? 1 )? == ? ' u ' )?{
????????????????????ch? = ?( char )?Integer.parseInt(src
????????????????????????????.substring(pos? + ? 2 ,?pos? + ? 6 ),? 16 );
????????????????????tmp.append(ch);
????????????????????lastPos? = ?pos? + ? 6 ;
????????????????}? else ?{
????????????????????ch? = ?( char )?Integer.parseInt(src
????????????????????????????.substring(pos? + ? 1 ,?pos? + ? 3 ),? 16 );
????????????????????tmp.append(ch);
????????????????????lastPos? = ?pos? + ? 3 ;
????????????????}
????????????}? else ?{
???????????????? if ?(pos? == ? - 1 )?{
????????????????????tmp.append(src.substring(lastPos));
????????????????????lastPos? = ?src.length();
????????????????}? else ?{
????????????????????tmp.append(src.substring(lastPos,?pos));
????????????????????lastPos? = ?pos;
????????????????}
????????????}
????????}
???????? return ?tmp.toString();
????}
???? /**
?????*?獲取類路徑中的資源文件的物理文件路徑.
?????*?NOTE:?僅在?Win32?平臺下測試通過開發.
?????*?@date?2005.10.16
?????*? @param ?resourcePath?資源路徑
?????*? @return ?配置文件路徑
????? */
???? public ? static ?String?getRealFilePath(String?resourcePath)?{
????????java.net.URL?inputURL? = ?StringUtil. class
????????????????????????????.getResource(resourcePath);
????????String?filePath? = ?inputURL.getFile();
????????
???????? // ?2007-02-08??Fixed?by?K.D.?to?solve?the?space?char?problem,?also?with?some
???????? // ?other?special?chars?in?path?problem
???????? try {
????????????filePath? = ?java.net.URLDecoder.decode(filePath,? " utf-8 " );
????????} catch (Exception?e){
????????????e.printStackTrace();
????????}
????????
???????? // ?For?windows?platform,?the?filePath?will?like?this:
???????? // ?/E:/Push/web/WEB-INF/classes/studio/beansoft/smtp/MailSender.ini
???????? // ?So?must?remove?the?first?/
// ????????if(OS.isWindows()?&&?filePath.startsWith("/"))?{
// ????????????filePath?=?filePath.substring(1);
// ????????}
???????? return ?filePath;
????}
???? /**
?????*?將字符串轉換為?int.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*?@date?2005-07-29
?????*? @return ?結果數字
????? */
???? public ? static ? int ?parseInt(String?input)?{
???????? try ?{
???????????? return ?Integer.parseInt(input);
????????}? catch ?(Exception?e)?{
???????????? // ?TODO:?handle?exception
????????}
???????? return ? 0 ;
????}
????
???? /**
?????*?將字符串轉換為?long.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*? @return ?結果數字
????? */
???? public ? static ? long ?parseLong(String?input)?{
???????? try ?{
???????????? return ?Long.parseLong(input);
????????}? catch ?(Exception?e)?{
????????}
???????? return ? 0 ;
????}
????
???? /**
?????*?將字符串轉換為?long.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*? @return ?結果數字
????? */
???? public ? static ? double ?parseDouble(String?input)?{
???????? try ?{
???????????? return ?Double.parseDouble(input);
????????}? catch ?(Exception?e)?{
????????}
???????? return ? 0 ;
????}
???? /**
?????*?格式化日期到日時分秒時間格式的顯示.?d日?HH:mm:ss
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToDHMSString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " d日?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?格式化日期到時分秒時間格式的顯示.
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToHMSString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將時分秒時間格式的字符串轉換為日期.
?????*
?????*? @param ?input
?????*? @return
????? */
???? public ? static ?Date?parseHMSStringToDate(String?input)?{
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " HH:mm:ss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(ParseException?e)?{
????????????e.printStackTrace();
????????}
???????? return ? null ;
????}
???? /**
?????*?格式化日期到?Mysql?數據庫日期格式字符串的顯示.
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToMysqlString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy-MM-dd?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將?Mysql?數據庫日期格式字符串轉換為日期.
?????*
?????*? @param ?input
?????*? @return
????? */
???? public ? static ?Date?parseStringToMysqlDate(String?input)?{
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy-MM-dd?HH:mm:ss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(ParseException?e)?{
????????????e.printStackTrace();
????????}
???????? return ? null ;
????}
???? /**
?????*?返回時間字符串,?可讀形式的,?M月d日?HH:mm?格式.?2004-09-22,?LiuChangjiong
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToMMddHHmm(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " M月d日?HH:mm " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?返回時間字符串,?可讀形式的,?yy年M月d日HH:mm?格式.?2004-10-04,?LiuChangjiong
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToyyMMddHHmm(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yy年M月d日HH:mm " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?返回?HTTP?請求的?Referer,?如果沒有,?就返回默認頁面值.
?????*
?????*?僅用于移動博客開發頁面命名風格:?//?Added?at?2004-10-12?//?如果前一頁面的地址包含?_action.jsp?,
?????*?為了避免鏈接出錯,?就返回默認頁面
?????*
?????*?2006-08-02?增加從?url?參數?referer?的判斷?
?????*?
?????*? @param ?request?-
?????*????????????HttpServletRequest?對象
?????*? @param ?defaultPage?-
?????*????????????String,?默認頁面
?????*? @return ?String?-?Referfer
????? */
???? public ? static ?String?getReferer(HttpServletRequest?request,
????????????String?defaultPage)?{
????????String?referer? = ?request.getHeader( " Referer " ); // ?前一頁面的地址,?提交結束后返回此頁面
???????? // ?獲取URL中的referer參數
????????String?refererParam? = ?request.getParameter( " referer " );
????????
???????? if ( ! isEmpty(refererParam))?{
????????????referer? = ?refererParam;
????????}
????????
???????? // ?Added?at?2004-10-12
???????? // ?如果前一頁面的地址包含?_action.jsp?,?為了避免鏈接出錯,?就返回默認頁面
???????? if ?(isEmpty(referer)? || ?referer.indexOf( " _action.jsp " )? != ? - 1 )?{
????????????referer? = ?defaultPage;
????????}
???????? return ?referer;
????}
???? /**
?????*?生成一個?18?位的?yyyyMMddHHmmss.SSS?格式的日期字符串.
?????*
?????*? @param ?date
?????*????????????Date
?????*? @return ?String
????? */
???? public ? static ?String?genTimeStampString(Date?date)?{
????????java.text.SimpleDateFormat?df? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss.SSS " );
???????? return ?df.format(date);
????}
???? /**
?????*?Write?the?HTML?base?tag?to?support?servlet?forward?calling?relative?path
?????*?changed?problems.
?????*
?????*?Base?is?used?to?ensure?that?your?document's?relative?links?are?associated
?????*?with?the?proper?document?path.?The?href?specifies?the?document's
?????*?reference?URL?for?associating?relative?URLs?with?the?proper?document
?????*?path.?This?element?may?only?be?used?within?the?HEAD?tag.?Example:?<BASE
?????*?HREF=" http://www.sample.com/hello.htm ">
?????*
?????*? @param ?pageContext
?????*????????????the?PageContext?of?the?jsp?page?object
????? */
???? public ? static ? void ?writeHtmlBase(PageContext?pageContext)?{
????????HttpServletRequest?request? = ?(HttpServletRequest)?pageContext
????????????????.getRequest();
????????StringBuffer?buf? = ? new ?StringBuffer( " <base?href=\ "" );
????????buf.append(request.getScheme());
????????buf.append( " :// " );
????????buf.append(request.getServerName());
????????buf.append( " : " );
????????buf.append(request.getServerPort());
????????buf.append(request.getRequestURI());
????????buf.append( " \ " > " );
????????JspWriter?out? = ?pageContext.getOut();
???????? try ?{
????????????out.write(buf.toString());
????????}? catch ?(java.io.IOException?e)?{
????????}
????}
???? /**
?????*?Get?the?base?path?of?this?request.
?????*
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?base?path,?eg:? http://www.abc.com :8000/someApp/
????? */
???? public ? static ?String?getBasePath(HttpServletRequest?request)?{
????????String?path? = ?request.getContextPath();
????????String?basePath? = ?request.getScheme()? + ? " :// " ? + ?request.getServerName()
???????????????? + ? " : " ? + ?request.getServerPort()? + ?path? + ? " / " ;
???????? return ?basePath;
????}
???? /**
?????*?Get?the?current?page's?full?path?of?this?request.?獲取當前頁的完整訪問?URL?路徑.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?full?url?path,?eg:
?????*????????? http://www.abc.com :8000/someApp/index.jsp?param=abc
????? */
???? public ? static ?String?getFullRequestURL(HttpServletRequest?request)?{
????????StringBuffer?url? = ?request.getRequestURL();
????????String?qString? = ?request.getQueryString();
???????? if ?(qString? != ? null )?{
????????????url.append( ' ? ' );
????????????url.append(qString);
????????}
???????? return ?url.toString();
????}
???? /**
?????*?Get?the?current?page's?full?path?of?this?request.?獲取當前頁的完整訪問?URI?路徑.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?full?uri?path,?eg:?/someApp/index.jsp?param=abc
????? */
???? public ? static ?String?getFullRequestURI(HttpServletRequest?request)?{
????????StringBuffer?url? = ? new ?StringBuffer(request.getRequestURI());
????????String?qString? = ?request.getQueryString();
???????? if ?(qString? != ? null )?{
????????????url.append( ' ? ' );
????????????url.append(qString);
????????}
???????? return ?url.toString();
????}
???? // ?------------------------------------?字符串處理方法
???? // ?----------------------------------------------
???? /**
?????*?將字符串?source?中的?oldStr?替換為?newStr,?并以大小寫敏感方式進行查找
?????*
?????*? @param ?source
?????*????????????需要替換的源字符串
?????*? @param ?oldStr
?????*????????????需要被替換的老字符串
?????*? @param ?newStr
?????*????????????替換為的新字符串
????? */
???? public ? static ?String?replace(String?source,?String?oldStr,?String?newStr)?{
???????? return ?replace(source,?oldStr,?newStr,? true );
????}
???? /**
?????*?將字符串?source?中的?oldStr?替換為?newStr,?matchCase?為是否設置大小寫敏感查找
?????*
?????*? @param ?source
?????*????????????需要替換的源字符串
?????*? @param ?oldStr
?????*????????????需要被替換的老字符串
?????*? @param ?newStr
?????*????????????替換為的新字符串
?????*? @param ?matchCase
?????*????????????是否需要按照大小寫敏感方式查找
????? */
???? public ? static ?String?replace(String?source,?String?oldStr,?String?newStr,
???????????? boolean ?matchCase)?{
???????? if ?(source? == ? null )?{
???????????? return ? null ;
????????}
???????? // ?首先檢查舊字符串是否存在,?不存在就不進行替換
???????? if ?(source.toLowerCase().indexOf(oldStr.toLowerCase())? == ? - 1 )?{
???????????? return ?source;
????????}
???????? int ?findStartPos? = ? 0 ;
???????? int ?a? = ? 0 ;
???????? while ?(a? > ? - 1 )?{
???????????? int ?b? = ? 0 ;
????????????String?str1,?str2,?str3,?str4,?strA,?strB;
????????????str1? = ?source;
????????????str2? = ?str1.toLowerCase();
????????????str3? = ?oldStr;
????????????str4? = ?str3.toLowerCase();
???????????? if ?(matchCase)?{
????????????????strA? = ?str1;
????????????????strB? = ?str3;
????????????}? else ?{
????????????????strA? = ?str2;
????????????????strB? = ?str4;
????????????}
????????????a? = ?strA.indexOf(strB,?findStartPos);
???????????? if ?(a? > ? - 1 )?{
????????????????b? = ?oldStr.length();
????????????????findStartPos? = ?a? + ?b;
????????????????StringBuffer?bbuf? = ? new ?StringBuffer(source);
????????????????source? = ?bbuf.replace(a,?a? + ?b,?newStr)? + ? "" ;
???????????????? // ?新的查找開始點位于替換后的字符串的結尾
????????????????findStartPos? = ?findStartPos? + ?newStr.length()? - ?b;
????????????}
????????}
???????? return ?source;
????}
???? /**
?????*?清除字符串結尾的空格.
?????*
?????*? @param ?input
?????*????????????String?輸入的字符串
?????*? @return ?轉換結果
????? */
???? public ? static ?String?trimTailSpaces(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
????????String?trimedString? = ?input.trim();
???????? if ?(trimedString.length()? == ?input.length())?{
???????????? return ?input;
????????}
???????? return ?input.substring( 0 ,?input.indexOf(trimedString)
???????????????? + ?trimedString.length());
????}
???? /**
?????*?Change?the?null?string?value?to?"",?if?not?null,?then?return?it?self,?use
?????*?this?to?avoid?display?a?null?string?to?"null".
?????*
?????*? @param ?input
?????*????????????the?string?to?clear
?????*? @return ?the?result
????? */
???? public ? static ?String?clearNull(String?input)?{
???????? return ?isEmpty(input)? ? ? "" ?:?input;
????}
???? /**
?????*?Return?the?limited?length?string?of?the?input?string?(added?at:April?10,
?????*?2004).
?????*
?????*? @param ?input
?????*????????????String
?????*? @param ?maxLength
?????*????????????int
?????*? @return ?String?processed?result
????? */
???? public ? static ?String?limitStringLength(String?input,? int ?maxLength)?{
???????? if ?(isEmpty(input))
???????????? return ? "" ;
???????? if ?(input.length()? <= ?maxLength)?{
???????????? return ?input;
????????}? else ?{
???????????? return ?input.substring( 0 ,?maxLength? - ? 3 )? + ? "
"
;
????????}
????}
???? /**
?????*?將字符串轉換為一個?JavaScript?的?alert?調用.?eg:?htmlAlert("What?");?returns
?????*?<SCRIPT?language="JavaScript">alert("What?")</SCRIPT>
?????*
?????*? @param ?message
?????*????????????需要顯示的信息
?????*? @return ?轉換結果
????? */
???? public ? static ?String?scriptAlert(String?message)?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >alert(\ "" ?+?message
???????????????? + ? " \ " ); </ SCRIPT > " ;
????}
???? /**
?????*?將字符串轉換為一個?JavaScript?的?document.location?改變調用.?eg:?htmlAlert("a.jsp");
?????*?returns?<SCRIPT
?????*?language="JavaScript">document.location="a.jsp";</SCRIPT>
?????*
?????*? @param ?url
?????*????????????需要顯示的?URL?字符串
?????*? @return ?轉換結果
????? */
???? public ? static ?String?scriptRedirect(String?url)?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >document.location=\ "" ?+?url
???????????????? + ? " \ " ; </ SCRIPT > " ;
????}
???? /**
?????*?返回腳本語句?<SCRIPT?language="JavaScript">history.back();</SCRIPT>
?????*
?????*? @return ?腳本語句
????? */
???? public ? static ?String?scriptHistoryBack()?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >history.back();</SCRIPT> " ;
????}
???? /**
?????*?濾除帖子中的危險?HTML?代碼,?主要是腳本代碼,?滾動字幕代碼以及腳本事件處理代碼
?????*
?????*? @param ?content
?????*????????????需要濾除的字符串
?????*? @return ?過濾的結果
????? */
???? public ? static ?String?replaceHtmlCode(String?content)?{
???????? if ?(isEmpty(content))?{
???????????? return ? "" ;
????????}
???????? // ?需要濾除的腳本事件關鍵字
????????String[]?eventKeywords? = ?{? " onmouseover " ,? " onmouseout " ,? " onmousedown " ,
???????????????? " onmouseup " ,? " onmousemove " ,? " onclick " ,? " ondblclick " ,
???????????????? " onkeypress " ,? " onkeydown " ,? " onkeyup " ,? " ondragstart " ,
???????????????? " onerrorupdate " ,? " onhelp " ,? " onreadystatechange " ,? " onrowenter " ,
???????????????? " onrowexit " ,? " onselectstart " ,? " onload " ,? " onunload " ,
???????????????? " onbeforeunload " ,? " onblur " ,? " onerror " ,? " onfocus " ,? " onresize " ,
???????????????? " onscroll " ,? " oncontextmenu " ?};
????????content? = ?replace(content,? " <script " ,? " <script " ,? false );
????????content? = ?replace(content,? " </script " ,? " </script " ,? false );
????????content? = ?replace(content,? " <marquee " ,? " <marquee " ,? false );
????????content? = ?replace(content,? " </marquee " ,? " </marquee " ,? false );
???????? // ?FIXME?加這個過濾換行到?BR?的功能會把原始?HTML?代碼搞亂?2006-07-30
// ????????content?=?replace(content,?"\r\n",?"<BR>");
???????? // ?濾除腳本事件代碼
???????? for ?( int ?i? = ? 0 ;?i? < ?eventKeywords.length;?i ++ )?{
????????????content? = ?replace(content,?eventKeywords[i],
???????????????????? " _ " ? + ?eventKeywords[i],? false );? // ?添加一個"_",?使事件代碼無效
????????}
???????? return ?content;
????}
???? /**
?????*?濾除?HTML?代碼?為文本代碼.
????? */
???? public ? static ?String?replaceHtmlToText(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
???????? return ?setBr(setTag(input));
????}
???? /**
?????*?濾除?HTML?標記.
?????*?因為?XML?中轉義字符依然有效,?因此把特殊字符過濾成中文的全角字符.
?????*? @author ?beansoft
?????*? @param ?s?輸入的字串
?????*? @return ?過濾后的字串
????? */
???? public ? static ?String?setTag(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? char ?ch;
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
????????????ch? = ?s.charAt(i);
???????????? if ?(ch? == ? ' < ' )?{
????????????????stringbuffer.append( " < " );
// ????????????????stringbuffer.append("〈");
????????????}? else ? if ?(ch? == ? ' > ' )?{
????????????????stringbuffer.append( " > " );
// ????????????????stringbuffer.append("〉");
????????????}? else ? if ?(ch? == ? ' & ' )?{
????????????????stringbuffer.append( " & " );
// ????????????????stringbuffer.append("〃");
????????????}? else ? if ?(ch? == ? ' % ' )?{
????????????????stringbuffer.append( " %% " );
// ????????????????stringbuffer.append("※");
????????????}? else ?{
????????????????stringbuffer.append(ch);
????????????}
????????}
???????? return ?stringbuffer.toString();
????}
???? /** ?濾除?BR?代碼? */
???? public ? static ?String?setBr(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
???????????? if ?(s.charAt(i)? == ? ' \n ' ? || ?s.charAt(i)? == ? ' \r ' )?{
???????????????? continue ;
????????????}
????????????stringbuffer.append(s.charAt(i));
????????}
???????? return ?stringbuffer.toString();
????}
???? /** ?濾除空格? */
???? public ? static ?String?setNbsp(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
???????????? if ?(s.charAt(i)? == ? ' ? ' )?{
????????????????stringbuffer.append( " " );
????????????}? else ?{
????????????????stringbuffer.append(s.charAt(i)? + ? "" );
????????????}
????????}
???????? return ?stringbuffer.toString();
????}
???? /**
?????*?判斷字符串是否全是數字字符或者點號.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?判斷結果,?true?為全數字,?false?為還有非數字字符
????? */
???? public ? static ? boolean ?isNumeric(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? false ;
????????}
???????? for ?( int ?i? = ? 0 ;?i? < ?input.length();?i ++ )?{
???????????? char ?charAt? = ?input.charAt(i);
???????????? if ?( ! Character.isDigit(charAt)? && ??(charAt? != ? ' . ' ?)?)?{
???????????????? return ? false ;
????????????}
????????}
???????? return ? true ;
????}
???? /**
?????*?轉換由表單讀取的數據的內碼(從?ISO8859?轉換到?gb2312).
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?toChi(String?input)?{
???????? try ?{
???????????? byte []?bytes? = ?input.getBytes( " ISO8859-1 " );
???????????? return ? new ?String(bytes,? " GBK " );
????????}? catch ?(Exception?ex)?{
????????}
???????? return ?input;
????}
???? /**
?????*?轉換由表單讀取的數據的內碼到?ISO(從?GBK?轉換到ISO8859-1).
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?toISO(String?input)?{
???????? return ?changeEncoding(input,? " GBK " ,? " ISO8859-1 " );
????}
???? /**
?????*?轉換字符串的內碼.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @param ?sourceEncoding
?????*????????????源字符集名稱
?????*? @param ?targetEncoding
?????*????????????目標字符集名稱
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?changeEncoding(String?input,?String?sourceEncoding,
????????????String?targetEncoding)?{
???????? if ?(input? == ? null ? || ?input.equals( "" ))?{
???????????? return ?input;
????????}
???????? try ?{
???????????? byte []?bytes? = ?input.getBytes(sourceEncoding);
???????????? return ? new ?String(bytes,?targetEncoding);
????????}? catch ?(Exception?ex)?{
????????}
???????? return ?input;
????}
???? /**
?????*?將單個的?'?換成?'';?SQL?規則:如果單引號中的字符串包含一個嵌入的引號,可以使用兩個單引號表示嵌入的單引號.
????? */
???? public ? static ?String?replaceSql(String?input)?{
???????? return ?replace(input,? " ' " ,? " '' " );
????}
???? /**
?????*?對給定字符進行?URL?編碼
????? */
???? public ? static ?String?encode(String?value)?{
???????? if ?(isEmpty(value))?{
???????????? return ? "" ;
????????}
???????? try ?{
????????????value? = ?java.net.URLEncoder.encode(value,? " GB2312 " );
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ?value;
????}
???? /**
?????*?對給定字符進行?URL?解碼
?????*
?????*? @param ?value
?????*????????????解碼前的字符串
?????*? @return ?解碼后的字符串
????? */
???? public ? static ?String?decode(String?value)?{
???????? if ?(isEmpty(value))?{
???????????? return ? "" ;
????????}
???????? try ?{
???????????? return ?java.net.URLDecoder.decode(value,? " GB2312 " );
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ?value;
????}
???? /**
?????*?判斷字符串是否未空,?如果為?null?或者長度為0,?均返回?true.
????? */
???? public ? static ? boolean ?isEmpty(String?input)?{
???????? return ?(input? == ? null ? || ?input.length()? == ? 0 );
????}
???? /**
?????*?獲得輸入字符串的字節長度(即二進制字節數),?用于發送短信時判斷是否超出長度.
?????*
?????*? @param ?input
?????*????????????輸入字符串
?????*? @return ?字符串的字節長度(不是?Unicode?長度)
????? */
???? public ? static ? int ?getBytesLength(String?input)?{
???????? if ?(input? == ? null )?{
???????????? return ? 0 ;
????????}
???????? int ?bytesLength? = ?input.getBytes().length;
???????? // System.out.println("bytes?length?is:"?+?bytesLength);
???????? return ?bytesLength;
????}
???? /**
?????*?檢驗字符串是否未空,?如果是,?則返回給定的出錯信息.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @param ?errorMsg
?????*????????????出錯信息
?????*? @return ?空串返回出錯信息
????? */
???? public ? static ?String?isEmpty(String?input,?String?errorMsg)?{
???????? if ?(isEmpty(input))?{
???????????? return ?errorMsg;
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的擴展名.
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?extension?portion?of?the?file's?name.
????? */
???? public ? static ?String?getExtension(String?fileName)?{
???????? if ?(fileName? != ? null )?{
???????????? int ?i? = ?fileName.lastIndexOf( ' . ' );
???????????? if ?(i? > ? 0 ? && ?i? < ?fileName.length()? - ? 1 )?{
???????????????? return ?fileName.substring(i? + ? 1 ).toLowerCase();
????????????}
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的前綴名.
?????*?@date?2005-10-18
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?prefix?portion?of?the?file's?name.
????? */
???? public ? static ?String?getPrefix(String?fileName)?{
???????? if ?(fileName? != ? null )?{
????????????fileName? = ?fileName.replace( ' \\ ' ,? ' / ' );
???????????? if (fileName.lastIndexOf( " / " )? > ? 0 )?{
????????????????fileName? = ?fileName.substring(fileName.lastIndexOf( " / " )? + ? 1 ,?fileName.length());
????????????}
???????????? int ?i? = ?fileName.lastIndexOf( ' . ' );
???????????? if ?(i? > ? 0 ? && ?i? < ?fileName.length()? - ? 1 )?{
???????????????? return ?fileName.substring( 0 ,?i);
????????????}
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的短路徑,?不保護目錄.
?????*?@date?2005-10-18
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?short?version?of?the?file's?name.
????? */
???? public ? static ?String?getShortFileName(String?fileName)?{
???????? if ?(fileName? != ? null )?{
????????????String?oldFileName? = ? new ?String(fileName);
????????????fileName? = ?fileName.replace( ' \\ ' ,? ' / ' );
????????????
???????????? // ?Handle?dir
???????????? if (fileName.endsWith( " / " ))?{
???????????????? int ?idx? = ?fileName.indexOf( ' / ' );
???????????????? if (idx? == ? - 1 ? || ?idx? == ?fileName.length()? - ? 1 )?{
???????????????????? return ?oldFileName;
????????????????}? else ?{
???????????????????? return ??oldFileName.substring(idx? + ? 1 ,?fileName.length()? - ? 1 );
????????????????}
????????????}
???????????? if (fileName.lastIndexOf( " / " )? > ? 0 )?{
????????????????fileName? = ?fileName.substring(fileName.lastIndexOf( " / " )? + ? 1 ,?fileName.length());
????????????}
???????????? return ?fileName;
????????}
???????? return ? "" ;
????}
???? /**
?????*?獲取表單參數并做默認轉碼,?從?ISO8859-1?轉換到?GBK.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*
?????*? @param ?request
?????*????????????HttpServletRequest?對象
?????*? @param ?fieldName
?????*????????????參數名
?????*? @return ?取得的表單值
????? */
???? public ? static ?String?getParameter(HttpServletRequest?request,
????????????String?fieldName)?{
???????????? // ???????? // ?判斷編碼是否已經指定
// ????????String?encoding?=?request.getCharacterEncoding();
//
// ????????if("GBK".equalsIgnoreCase(encoding)?||?"GB2312".equalsIgnoreCase(encoding))?{
// ????????????return?request.getParameter(fieldName);
// ????????}
//
// ????????return?request(request,?fieldName);
???????? // ?2005-08-01?臨時修改
// ????????try?{
// ????????????request.setCharacterEncoding("UTF-8");
// ????????}?catch?(UnsupportedEncodingException?e)?{
// ???????????? // ?TODO?auto?generated?try-catch
// ????????????e.printStackTrace();
// ????????}
???????? return ?request.getParameter(fieldName);
????}
???? // ?------------------------------------?JSP?參數處理方法
???? /**
?????*?根據?Cookie?名稱得到請求中的?Cookie?值,?需要事先給?_request?一個初始值;?如果?Cookie?值是?null,?則返回?""
????? */
???? public ? static ?String?getCookieValue(HttpServletRequest?request,?String?name)?{
????????Cookie[]?cookies? = ?request.getCookies();
???????? if ?(cookies? == ? null )?{
???????????? return ? "" ;
????????}
???????? for ?( int ?i? = ? 0 ;?i? < ?cookies.length;?i ++ )?{
????????????Cookie?cookie? = ?cookies[i];
???????????? if ?(cookie.getName().equals(name))?{
???????????????? // ?需要對?Cookie?中的漢字進行?URL?反編碼,?適用版本:?Tomcat?4.0
???????????????? return ?decode(cookie.getValue());
???????????????? // ?不需要反編碼,?適用版本:?JSWDK?1.0.1
???????????????? // return?cookie.getValue();
????????????}
????????}
???????? // ?A?cookie?may?not?return?a?null?value,?may?return?a?""
???????? return ? "" ;
????}
???? // ?返回指定表單名的數組
???? public ?String[]?getParameterValues(HttpServletRequest?request,?String?name)?{
???????? // ?POST?方法的參數沒有編碼錯誤
???????? // if?(request.getMethod().equalsIgnoreCase("POST"))?{
???????? // ?文件上傳模式
???????? // if(isUploadMode)?{
???????? // ????return?request.getParameterValues(name);
???????? // }
???????? // ?--?For?Tomcat?4.0
???????? // return?request.getParameterValues(name);
???????? // ?--?For?JSWDK?1.0.1
???????? /*
?????????*?String?values[]?=?_request.getParameterValues(name);?if(values?!=
?????????*?null)?{?for(int?i?=?0;?i?<?values.length;?i++)?{?values[i]?=
?????????*?toChi(values[i]);?}?}?return?values;
????????? */
???????? // }
???????? // else?{
???????? // ?將通過?GET?方式發送的中文字符解碼(但是必須使用?java.net.URLEncoder?進行中文字符參數的編碼)
???????? // ?解碼時需使用內碼轉換,?也可使用反編碼,?即:?return?decode(_request.getParameter(name));
???????? // ?問題:?decode()?僅適用于?JDK?1.3?+?Tomcat?4.0
????????String?encoding? = ?request.getCharacterEncoding();
???????? if ( " GBK " .equalsIgnoreCase(encoding)? || ? " GB2312 " .equalsIgnoreCase(encoding))?{
???????????? return ?request.getParameterValues(name);
????????}
????????String?values[]? = ?request.getParameterValues(name);
???????? if ?(values? != ? null )?{
???????????? for ?( int ?i? = ? 0 ;?i? < ?values.length;?i ++ )?{
????????????????values[i]? = ?toChi(values[i]);
????????????}
????????}
???????? return ?values;
???????? // }
????}
???? /**
?????*?刪除指定的?Web?應用程序目錄下所上傳的文件
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? void ?deleteFile(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
????????????????file.delete();
????????????}
????????}
????}
???? /**
?????*?在指定的?Web?應用程序目錄下以指定路徑創建文件
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? boolean ?createFile(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
???????????????? try ?{
???????????????????? // ?創建文件
???????????????????? return ?file.createNewFile();
????????????????}? catch ?(IOException?e)?{
????????????????????System.err.println( " Unable?to?create?file? " ? + ?filePath);
????????????????}
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?在指定的?Web?應用程序目錄下以指定路徑創建目錄.
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? boolean ?createDir(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
???????????????? try ?{
???????????????????? // ?創建目錄
????????????????????java.io.File?dir? = ? new ?java.io.File(application
????????????????????????????.getRealPath(filePath));
???????????????????? return ?dir.mkdirs();
????????????????}? catch ?(Exception?e)?{
????????????????????System.err
????????????????????????????.println( " Unable?to?create?directory? " ? + ?filePath);
????????????????}
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?檢查指定的?Web?應用程序目錄下的文件是否存在.
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
?????*? @return ?boolean?-?文件是否存在
????? */
???? public ? static ? boolean ?checkFileExists(ServletContext?application,
????????????String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
???????????????? return ?file.exists();
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?獲取文件圖標名.
?????*?Date:?2005-10
?????*? @param ?application?JSP/Servlet?的?ServletContext
?????*? @param ?iconDirPath?圖標文件夾的路徑
?????*? @param ?fileName?需要處理的文件名
?????*? @return ?圖標文件相對路徑
????? */
???? public ? static ?String?getFileIcon(ServletContext?application,
????????????String?iconDirPath,?String?fileName)?{
????????String?ext? = ?getExtension(fileName);
????????String?filePath? = ?iconDirPath? + ?ext? + ? " .gif " ;
// ????????return?filePath;
???????? if (checkFileExists(application,?filePath))?{
???????????? return ?filePath;
????????}
???????? return ?iconDirPath? + ? " file.gif " ;
????}
???? /**
?????*?Gets?the?absolute?pathname?of?the?class?or?resource?file?containing?the
?????*?specified?class?or?resource?name,?as?prescribed?by?the?current?classpath.
?????*
?????*? @param ?resourceName
?????*????????????Name?of?the?class?or?resource?name.
?????*? @return ?the?absolute?pathname?of?the?given?resource
????? */
???? public ? static ?String?getPath(String?resourceName)?{
???????? if ?( ! resourceName.startsWith( " / " ))?{
????????????resourceName? = ? " / " ? + ?resourceName;
????????}
???????? // resourceName?=?resourceName.replace('.',?'/');
????????java.net.URL?classUrl? = ? new ?StringUtil().getClass().getResource(
????????????????resourceName);
???????? if ?(classUrl? != ? null )?{
???????????? // System.out.println("\nClass?'"?+?className?+
???????????? // "'?found?in?\n'"?+?classUrl.getFile()?+?"'");
???????????? // System.out.println("\n資源?'"?+?resourceName?+
???????????? // "'?在文件?\n'"?+?classUrl.getFile()?+?"'?中找到.");
???????????? return ?classUrl.getFile();
????????}
???????? // System.out.println("\nClass?'"?+?className?+
???????? // "'?not?found?in?\n'"?+
???????? // System.getProperty("java.class.path")?+?"'");
???????? // System.out.println("\n資源?'"?+?resourceName?+
???????? // "'?沒有在類路徑?\n'"?+
???????? // System.getProperty("java.class.path")?+?"'?中找到");
???????? return ? null ;
????}
???? /**
?????*?將日期轉換為中文表示方式的字符串(格式為?yyyy年MM月dd日?HH:mm:ss).
????? */
???? public ? static ?String?dateToChineseString(Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy年MM月dd日?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將日期轉換為?14?位的字符串(格式為yyyyMMddHHmmss).
????? */
???? public ? static ?String?dateTo14String(Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? null ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將?14?位的字符串(格式為yyyyMMddHHmmss)轉換為日期.
????? */
???? public ? static ?Date?string14ToDate(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? null ;
????????}
???????? if ?(input.length()? != ? 14 )?{
???????????? return ? null ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ? null ;
????}
???? // ?-----------------------------------------------------------
???? // ?----------?字符串和數字轉換工具方法,?2004.03.27?添加?--------
???? // ------------------------------------------------------------
???? public ? static ? byte ?getByte(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?Byte.parseByte(httpservletrequest.getParameter(s));
????}
????
???? /**
?????*?獲取?boolean?參數從ServletRequest中.
?????*? @param ?request
?????*? @param ?name
?????*? @return
????? */
???? public ? static ? boolean ?getBoolean(HttpServletRequest?request,?String?name)?{
???????? return ?Boolean.valueOf(request.getParameter(name));
????}
???? /**
?????*從請求對象中讀取參數返回為整數.
?????*
????? */
???? public ? static ? int ?getInt(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?parseInt(httpservletrequest.getParameter(s));
????}
???? public ? static ? long ?getLong(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0L ;
????????}
???????? return ?parseLong(httpservletrequest.getParameter(s));
????}
???? public ? static ? double ?getDouble(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?parseDouble(httpservletrequest.getParameter(s));
????}
???? /**
?????*?將?TEXT?文本轉換為?HTML?代碼,?已便于網頁正確的顯示出來.
?????*
?????*? @param ?input
?????*????????????輸入的文本字符串
?????*? @return ?轉換后的?HTML?代碼
????? */
???? public ? static ?String?textToHtml(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
????????input? = ?replace(input,? " < " ,? " < " );
????????input? = ?replace(input,? " > " ,? " > " );
????????input? = ?replace(input,? " \n " ,? " <br>\n " );
????????input? = ?replace(input,? " \t " ,? " " );
????????input? = ?replace(input,? " ?? " ,? " " );
???????? return ?input;
????}
???? public ? static ?String?toQuoteMark(String?s)?{
????????s? = ?replaceString(s,? " ' " ,? " ' " );
????????s? = ?replaceString(s,? " \ "" ,? " & # 34 ; " );
????????s? = ?replaceString(s,? " \r\n " ,? " \n " );
???????? return ?s;
????}
???? public ? static ?String?replaceChar(String?s,? char ?c,? char ?c1)?{
???????? if ?(s? == ? null )?{
???????????? return ? "" ;
????????}
???????? return ?s.replace(c,?c1);
????}
???? public ? static ?String?replaceString(String?s,?String?s1,?String?s2)?{
???????? if ?(s? == ? null ? || ?s1? == ? null ? || ?s2? == ? null )?{
???????????? return ? "" ;
????????}
???????? return ?s.replaceAll(s1,?s2);
????}
???? public ? static ?String?toHtml(String?s)?{
????????s? = ?replaceString(s,? " < " ,? " < " );
????????s? = ?replaceString(s,? " > " ,? " > " );
???????? return ?s;
????}
???? public ? static ?String?toBR(String?s)?{
????????s? = ?replaceString(s,? " \n " ,? " <br>\n " );
????????s? = ?replaceString(s,? " \t " ,? " " );
????????s? = ?replaceString(s,? " ?? " ,? " " );
???????? return ?s;
????}
???? public ? static ?String?toSQL(String?s)?{
????????s? = ?replaceString(s,? " \r\n " ,? " \n " );
???????? return ?s;
????}
???? public ? static ?String?replaceEnter(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " \n " ,? " <br> " );
????}
???? public ? static ?String?replacebr(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " <br> " ,? " \n " );
????}
???? public ? static ?String?replaceQuote(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " ' " ,? " '' " );
????}
???? // ?Test?only.
???? public ? static ? void ?main(String[]?args)? throws ?Exception?{
????????System.out.println(formatFraction( 0.693431014112044 ,? 1 ,? 3 ));
????????
???????? // System.out.println(textToHtml("1<2\r\n<b>Bold</b>"));
???????? // System.out.println(scriptAlert("oh!"));
???????? // System.out.println(scriptRedirect(" http://localhost/ "));
???????? // ????System.out.println(StringUtil.getPath("/databaseconfig.properties"));
???????? // ????????java.io.File?file?=?new?java.io.File("e:\\Moblog\\abcd\\");
???????? //
???????? // ????????file.mkdir();
// ????????Date?time?=?(parseHMSStringToDate("12:23:00"));
// ????????System.out.println(time.toLocaleString());
// ????????Date?nowTime?=?parseHMSStringToDate(formatDateToHMSString(new?Date()));
// ????????System.out.println(nowTime.toLocaleString());
???????? // ????????GregorianCalendar?cal?=?new?GregorianCalendar();
???????? // ????????cal.setTime(new?Date());
???????? // ????????cal.add(cal.YEAR,?-cal.get(cal.YEAR)?+?1970);
???????? // ????????cal.add(cal.MONTH,?-cal.get(cal.MONTH));
???????? // ????????cal.add(cal.DATE,?-cal.get(cal.DATE)?+?1);
???????? //
???????? // ????????System.out.println(cal.getTime().toLocaleString());
????}
import ?java.io.UnsupportedEncodingException;
import ?java.text.ParseException;
import ?java.text.SimpleDateFormat;
import ?java.util.Calendar;
import ?java.util.Date;
import ?javax.servlet.ServletContext;
import ?javax.servlet.http.Cookie;
import ?javax.servlet.http.HttpServletRequest;
import ?javax.servlet.jsp.JspWriter;
import ?javax.servlet.jsp.PageContext;
/**
?*?StringUtil,?字符串工具類,?一些方便的字符串工具方法.
?*
?*?Dependencies:?Servlet/JSP?API.
?*
?*? @author ?劉長炯
?*? @version ?1.3?2009-03-8
? */
public ? class ?StringUtil?{
????
???? /**
?????*?格式化小數保持最少和最多小數點.
?????*?
?????*? @param ?num
?????*? @param ?minFractionDigits
?????*? @param ?maxFractionDigits
?????*? @return
????? */
???? public ? static ?String?formatFraction( double ?num,? int ?minFractionDigits,? int ?maxFractionDigits)?{
???????? // ?輸出固定小數點位數
????????java.text.NumberFormat?nb? = ?java.text.NumberFormat.getInstance();
????????nb.setMaximumFractionDigits(maxFractionDigits);
????????nb.setMinimumFractionDigits(minFractionDigits);
????????nb.setGroupingUsed( false );
????????String?rate? = ?nb.format(num);
????????
???????? return ?rate;
????}
????
???? /**
?????*?從請求中獲取當前頁碼數.
?????*? @param ?request
?????*? @return ?頁碼數,?不小于1
????? */
???? public ? static ? int ?getCurrentPage(HttpServletRequest?request)?{
????????String?pname? = ? " cp " ;
???????? // ?獲取頁碼數
???????? if ?(request.getParameter(pname)? != ? null ? && ? ! "" .equals(request.getParameter(pname)))?{
???????????? return ?Integer.parseInt(request.getParameter(pname));
????????}? else ?{
???????????? return ? 1 ;
????????}
????}
????
???? /**
?????*?將給定時間移動相對月份.
?????*? @param ?beginDate?起始日期
?????*? @param ?amount?數量
?????*? @return ?結果日期
????? */
???? public ? static ?Date?moveMonth(Date?beginDate,? int ?amount)?{
????????Calendar?cal? = ?Calendar.getInstance();
????????cal.setTime(beginDate);
????????
????????cal.add(Calendar.MONTH,?amount);
????????
???????? return ?cal.getTime();
????}
????
???? /**
?????*?將給定字符串時間移動相對月份.
?????*? @param ?year?年字符串
?????*? @param ?month?月
?????*? @param ?amount?數量
?????*? @return ?int[?年,?月?]
????? */
???? public ? static ? int []??moveMonth(String?year,?String?month,? int ?amount)?{
????????Date?d? = ?moveMonth(?parseDate(year? + ? " / " ? + ?month,? " yyyy/MM " ),?amount);
????????
????????Calendar?cal? = ?Calendar.getInstance();
????????cal.setTime(d);
????????
???????? return ? new ? int []?{?cal.get(Calendar.YEAR),?cal.get(Calendar.MONTH)};
????}
????
???? /**
?????*?解析日期.
?????*? @param ?input?輸入字符串
?????*? @param ?pattern?類型
?????*? @return ?Date?對象
????? */
???? public ? static ?Date?parseDate(String?input,?String?pattern)?{
???????? if (isEmpty(input))?{
???????????? return ? null ;
????????}
????????
????????SimpleDateFormat?df? = ? new ?SimpleDateFormat(pattern);
????????
???????? try ?{
???????????? return ?df.parse(input);
????????}? catch ?(ParseException?e)?{
???????????? // ?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}
????????
???????? return ? null ;
????}
????
???? /**
?????*?格式化日期為字符串.
?????*? @param ?date?日期字符串
?????*? @param ?pattern?類型
?????*? @return ?結果字符串
????? */
???? public ? static ?String?formatDate(Date?date,?String?pattern)?{
???????? if (date? == ? null ? || ?pattern? == ? null )?{? return ? null ;?}
???????? return ? new ?SimpleDateFormat(pattern).format(date);
????}
????
???? /**
?????*?Same?function?as?javascript's?escape().
?????*? @param ?src
?????*? @return
????? */
???? public ? static ?String?escape(String?src)?{
???????? int ?i;
???????? char ?j;
????????StringBuffer?tmp? = ? new ?StringBuffer();
????????tmp.ensureCapacity(src.length()? * ? 6 );
???????? for ?(i? = ? 0 ;?i? < ?src.length();?i ++ )?{
????????????j? = ?src.charAt(i);
???????????? if ?(Character.isDigit(j)? || ?Character.isLowerCase(j)
???????????????????? || ?Character.isUpperCase(j))
????????????????tmp.append(j);
???????????? else ? if ?(j? < ? 256 )?{
????????????????tmp.append( " % " );
???????????????? if ?(j? < ? 16 )
????????????????????tmp.append( " 0 " );
????????????????tmp.append(Integer.toString(j,? 16 ));
????????????}? else ?{
????????????????tmp.append( " %u " );
????????????????tmp.append(Integer.toString(j,? 16 ));
????????????}
????????}
???????? return ?tmp.toString();
????}
???? /**
?????*?Same?function?as?javascript's?unsecape().
?????*? @param ?src
?????*? @return
????? */
???? public ? static ?String?unescape(String?src)?{
????????StringBuffer?tmp? = ? new ?StringBuffer();
????????tmp.ensureCapacity(src.length());
???????? int ?lastPos? = ? 0 ,?pos? = ? 0 ;
???????? char ?ch;
???????? while ?(lastPos? < ?src.length())?{
????????????pos? = ?src.indexOf( " % " ,?lastPos);
???????????? if ?(pos? == ?lastPos)?{
???????????????? if ?(src.charAt(pos? + ? 1 )? == ? ' u ' )?{
????????????????????ch? = ?( char )?Integer.parseInt(src
????????????????????????????.substring(pos? + ? 2 ,?pos? + ? 6 ),? 16 );
????????????????????tmp.append(ch);
????????????????????lastPos? = ?pos? + ? 6 ;
????????????????}? else ?{
????????????????????ch? = ?( char )?Integer.parseInt(src
????????????????????????????.substring(pos? + ? 1 ,?pos? + ? 3 ),? 16 );
????????????????????tmp.append(ch);
????????????????????lastPos? = ?pos? + ? 3 ;
????????????????}
????????????}? else ?{
???????????????? if ?(pos? == ? - 1 )?{
????????????????????tmp.append(src.substring(lastPos));
????????????????????lastPos? = ?src.length();
????????????????}? else ?{
????????????????????tmp.append(src.substring(lastPos,?pos));
????????????????????lastPos? = ?pos;
????????????????}
????????????}
????????}
???????? return ?tmp.toString();
????}
???? /**
?????*?獲取類路徑中的資源文件的物理文件路徑.
?????*?NOTE:?僅在?Win32?平臺下測試通過開發.
?????*?@date?2005.10.16
?????*? @param ?resourcePath?資源路徑
?????*? @return ?配置文件路徑
????? */
???? public ? static ?String?getRealFilePath(String?resourcePath)?{
????????java.net.URL?inputURL? = ?StringUtil. class
????????????????????????????.getResource(resourcePath);
????????String?filePath? = ?inputURL.getFile();
????????
???????? // ?2007-02-08??Fixed?by?K.D.?to?solve?the?space?char?problem,?also?with?some
???????? // ?other?special?chars?in?path?problem
???????? try {
????????????filePath? = ?java.net.URLDecoder.decode(filePath,? " utf-8 " );
????????} catch (Exception?e){
????????????e.printStackTrace();
????????}
????????
???????? // ?For?windows?platform,?the?filePath?will?like?this:
???????? // ?/E:/Push/web/WEB-INF/classes/studio/beansoft/smtp/MailSender.ini
???????? // ?So?must?remove?the?first?/
// ????????if(OS.isWindows()?&&?filePath.startsWith("/"))?{
// ????????????filePath?=?filePath.substring(1);
// ????????}
???????? return ?filePath;
????}
???? /**
?????*?將字符串轉換為?int.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*?@date?2005-07-29
?????*? @return ?結果數字
????? */
???? public ? static ? int ?parseInt(String?input)?{
???????? try ?{
???????????? return ?Integer.parseInt(input);
????????}? catch ?(Exception?e)?{
???????????? // ?TODO:?handle?exception
????????}
???????? return ? 0 ;
????}
????
???? /**
?????*?將字符串轉換為?long.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*? @return ?結果數字
????? */
???? public ? static ? long ?parseLong(String?input)?{
???????? try ?{
???????????? return ?Long.parseLong(input);
????????}? catch ?(Exception?e)?{
????????}
???????? return ? 0 ;
????}
????
???? /**
?????*?將字符串轉換為?long.
?????*
?????*? @param ?input
?????*????????????輸入的字串
?????*? @return ?結果數字
????? */
???? public ? static ? double ?parseDouble(String?input)?{
???????? try ?{
???????????? return ?Double.parseDouble(input);
????????}? catch ?(Exception?e)?{
????????}
???????? return ? 0 ;
????}
???? /**
?????*?格式化日期到日時分秒時間格式的顯示.?d日?HH:mm:ss
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToDHMSString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " d日?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?格式化日期到時分秒時間格式的顯示.
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToHMSString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將時分秒時間格式的字符串轉換為日期.
?????*
?????*? @param ?input
?????*? @return
????? */
???? public ? static ?Date?parseHMSStringToDate(String?input)?{
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " HH:mm:ss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(ParseException?e)?{
????????????e.printStackTrace();
????????}
???????? return ? null ;
????}
???? /**
?????*?格式化日期到?Mysql?數據庫日期格式字符串的顯示.
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToMysqlString(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy-MM-dd?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將?Mysql?數據庫日期格式字符串轉換為日期.
?????*
?????*? @param ?input
?????*? @return
????? */
???? public ? static ?Date?parseStringToMysqlDate(String?input)?{
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy-MM-dd?HH:mm:ss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(ParseException?e)?{
????????????e.printStackTrace();
????????}
???????? return ? null ;
????}
???? /**
?????*?返回時間字符串,?可讀形式的,?M月d日?HH:mm?格式.?2004-09-22,?LiuChangjiong
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToMMddHHmm(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " M月d日?HH:mm " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?返回時間字符串,?可讀形式的,?yy年M月d日HH:mm?格式.?2004-10-04,?LiuChangjiong
?????*
?????*? @return ?-?String?格式化后的時間
????? */
???? public ? static ?String?formatDateToyyMMddHHmm(java.util.Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yy年M月d日HH:mm " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?返回?HTTP?請求的?Referer,?如果沒有,?就返回默認頁面值.
?????*
?????*?僅用于移動博客開發頁面命名風格:?//?Added?at?2004-10-12?//?如果前一頁面的地址包含?_action.jsp?,
?????*?為了避免鏈接出錯,?就返回默認頁面
?????*
?????*?2006-08-02?增加從?url?參數?referer?的判斷?
?????*?
?????*? @param ?request?-
?????*????????????HttpServletRequest?對象
?????*? @param ?defaultPage?-
?????*????????????String,?默認頁面
?????*? @return ?String?-?Referfer
????? */
???? public ? static ?String?getReferer(HttpServletRequest?request,
????????????String?defaultPage)?{
????????String?referer? = ?request.getHeader( " Referer " ); // ?前一頁面的地址,?提交結束后返回此頁面
???????? // ?獲取URL中的referer參數
????????String?refererParam? = ?request.getParameter( " referer " );
????????
???????? if ( ! isEmpty(refererParam))?{
????????????referer? = ?refererParam;
????????}
????????
???????? // ?Added?at?2004-10-12
???????? // ?如果前一頁面的地址包含?_action.jsp?,?為了避免鏈接出錯,?就返回默認頁面
???????? if ?(isEmpty(referer)? || ?referer.indexOf( " _action.jsp " )? != ? - 1 )?{
????????????referer? = ?defaultPage;
????????}
???????? return ?referer;
????}
???? /**
?????*?生成一個?18?位的?yyyyMMddHHmmss.SSS?格式的日期字符串.
?????*
?????*? @param ?date
?????*????????????Date
?????*? @return ?String
????? */
???? public ? static ?String?genTimeStampString(Date?date)?{
????????java.text.SimpleDateFormat?df? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss.SSS " );
???????? return ?df.format(date);
????}
???? /**
?????*?Write?the?HTML?base?tag?to?support?servlet?forward?calling?relative?path
?????*?changed?problems.
?????*
?????*?Base?is?used?to?ensure?that?your?document's?relative?links?are?associated
?????*?with?the?proper?document?path.?The?href?specifies?the?document's
?????*?reference?URL?for?associating?relative?URLs?with?the?proper?document
?????*?path.?This?element?may?only?be?used?within?the?HEAD?tag.?Example:?<BASE
?????*?HREF=" http://www.sample.com/hello.htm ">
?????*
?????*? @param ?pageContext
?????*????????????the?PageContext?of?the?jsp?page?object
????? */
???? public ? static ? void ?writeHtmlBase(PageContext?pageContext)?{
????????HttpServletRequest?request? = ?(HttpServletRequest)?pageContext
????????????????.getRequest();
????????StringBuffer?buf? = ? new ?StringBuffer( " <base?href=\ "" );
????????buf.append(request.getScheme());
????????buf.append( " :// " );
????????buf.append(request.getServerName());
????????buf.append( " : " );
????????buf.append(request.getServerPort());
????????buf.append(request.getRequestURI());
????????buf.append( " \ " > " );
????????JspWriter?out? = ?pageContext.getOut();
???????? try ?{
????????????out.write(buf.toString());
????????}? catch ?(java.io.IOException?e)?{
????????}
????}
???? /**
?????*?Get?the?base?path?of?this?request.
?????*
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?base?path,?eg:? http://www.abc.com :8000/someApp/
????? */
???? public ? static ?String?getBasePath(HttpServletRequest?request)?{
????????String?path? = ?request.getContextPath();
????????String?basePath? = ?request.getScheme()? + ? " :// " ? + ?request.getServerName()
???????????????? + ? " : " ? + ?request.getServerPort()? + ?path? + ? " / " ;
???????? return ?basePath;
????}
???? /**
?????*?Get?the?current?page's?full?path?of?this?request.?獲取當前頁的完整訪問?URL?路徑.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?full?url?path,?eg:
?????*????????? http://www.abc.com :8000/someApp/index.jsp?param=abc
????? */
???? public ? static ?String?getFullRequestURL(HttpServletRequest?request)?{
????????StringBuffer?url? = ?request.getRequestURL();
????????String?qString? = ?request.getQueryString();
???????? if ?(qString? != ? null )?{
????????????url.append( ' ? ' );
????????????url.append(qString);
????????}
???????? return ?url.toString();
????}
???? /**
?????*?Get?the?current?page's?full?path?of?this?request.?獲取當前頁的完整訪問?URI?路徑.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*? @param ?request?-
?????*????????????HttpServletRequest
?????*? @return ?String?-?the?full?uri?path,?eg:?/someApp/index.jsp?param=abc
????? */
???? public ? static ?String?getFullRequestURI(HttpServletRequest?request)?{
????????StringBuffer?url? = ? new ?StringBuffer(request.getRequestURI());
????????String?qString? = ?request.getQueryString();
???????? if ?(qString? != ? null )?{
????????????url.append( ' ? ' );
????????????url.append(qString);
????????}
???????? return ?url.toString();
????}
???? // ?------------------------------------?字符串處理方法
???? // ?----------------------------------------------
???? /**
?????*?將字符串?source?中的?oldStr?替換為?newStr,?并以大小寫敏感方式進行查找
?????*
?????*? @param ?source
?????*????????????需要替換的源字符串
?????*? @param ?oldStr
?????*????????????需要被替換的老字符串
?????*? @param ?newStr
?????*????????????替換為的新字符串
????? */
???? public ? static ?String?replace(String?source,?String?oldStr,?String?newStr)?{
???????? return ?replace(source,?oldStr,?newStr,? true );
????}
???? /**
?????*?將字符串?source?中的?oldStr?替換為?newStr,?matchCase?為是否設置大小寫敏感查找
?????*
?????*? @param ?source
?????*????????????需要替換的源字符串
?????*? @param ?oldStr
?????*????????????需要被替換的老字符串
?????*? @param ?newStr
?????*????????????替換為的新字符串
?????*? @param ?matchCase
?????*????????????是否需要按照大小寫敏感方式查找
????? */
???? public ? static ?String?replace(String?source,?String?oldStr,?String?newStr,
???????????? boolean ?matchCase)?{
???????? if ?(source? == ? null )?{
???????????? return ? null ;
????????}
???????? // ?首先檢查舊字符串是否存在,?不存在就不進行替換
???????? if ?(source.toLowerCase().indexOf(oldStr.toLowerCase())? == ? - 1 )?{
???????????? return ?source;
????????}
???????? int ?findStartPos? = ? 0 ;
???????? int ?a? = ? 0 ;
???????? while ?(a? > ? - 1 )?{
???????????? int ?b? = ? 0 ;
????????????String?str1,?str2,?str3,?str4,?strA,?strB;
????????????str1? = ?source;
????????????str2? = ?str1.toLowerCase();
????????????str3? = ?oldStr;
????????????str4? = ?str3.toLowerCase();
???????????? if ?(matchCase)?{
????????????????strA? = ?str1;
????????????????strB? = ?str3;
????????????}? else ?{
????????????????strA? = ?str2;
????????????????strB? = ?str4;
????????????}
????????????a? = ?strA.indexOf(strB,?findStartPos);
???????????? if ?(a? > ? - 1 )?{
????????????????b? = ?oldStr.length();
????????????????findStartPos? = ?a? + ?b;
????????????????StringBuffer?bbuf? = ? new ?StringBuffer(source);
????????????????source? = ?bbuf.replace(a,?a? + ?b,?newStr)? + ? "" ;
???????????????? // ?新的查找開始點位于替換后的字符串的結尾
????????????????findStartPos? = ?findStartPos? + ?newStr.length()? - ?b;
????????????}
????????}
???????? return ?source;
????}
???? /**
?????*?清除字符串結尾的空格.
?????*
?????*? @param ?input
?????*????????????String?輸入的字符串
?????*? @return ?轉換結果
????? */
???? public ? static ?String?trimTailSpaces(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
????????String?trimedString? = ?input.trim();
???????? if ?(trimedString.length()? == ?input.length())?{
???????????? return ?input;
????????}
???????? return ?input.substring( 0 ,?input.indexOf(trimedString)
???????????????? + ?trimedString.length());
????}
???? /**
?????*?Change?the?null?string?value?to?"",?if?not?null,?then?return?it?self,?use
?????*?this?to?avoid?display?a?null?string?to?"null".
?????*
?????*? @param ?input
?????*????????????the?string?to?clear
?????*? @return ?the?result
????? */
???? public ? static ?String?clearNull(String?input)?{
???????? return ?isEmpty(input)? ? ? "" ?:?input;
????}
???? /**
?????*?Return?the?limited?length?string?of?the?input?string?(added?at:April?10,
?????*?2004).
?????*
?????*? @param ?input
?????*????????????String
?????*? @param ?maxLength
?????*????????????int
?????*? @return ?String?processed?result
????? */
???? public ? static ?String?limitStringLength(String?input,? int ?maxLength)?{
???????? if ?(isEmpty(input))
???????????? return ? "" ;
???????? if ?(input.length()? <= ?maxLength)?{
???????????? return ?input;
????????}? else ?{
???????????? return ?input.substring( 0 ,?maxLength? - ? 3 )? + ? "

????????}
????}
???? /**
?????*?將字符串轉換為一個?JavaScript?的?alert?調用.?eg:?htmlAlert("What?");?returns
?????*?<SCRIPT?language="JavaScript">alert("What?")</SCRIPT>
?????*
?????*? @param ?message
?????*????????????需要顯示的信息
?????*? @return ?轉換結果
????? */
???? public ? static ?String?scriptAlert(String?message)?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >alert(\ "" ?+?message
???????????????? + ? " \ " ); </ SCRIPT > " ;
????}
???? /**
?????*?將字符串轉換為一個?JavaScript?的?document.location?改變調用.?eg:?htmlAlert("a.jsp");
?????*?returns?<SCRIPT
?????*?language="JavaScript">document.location="a.jsp";</SCRIPT>
?????*
?????*? @param ?url
?????*????????????需要顯示的?URL?字符串
?????*? @return ?轉換結果
????? */
???? public ? static ?String?scriptRedirect(String?url)?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >document.location=\ "" ?+?url
???????????????? + ? " \ " ; </ SCRIPT > " ;
????}
???? /**
?????*?返回腳本語句?<SCRIPT?language="JavaScript">history.back();</SCRIPT>
?????*
?????*? @return ?腳本語句
????? */
???? public ? static ?String?scriptHistoryBack()?{
???????? return ? " <SCRIPT?language=\ " JavaScript\ " >history.back();</SCRIPT> " ;
????}
???? /**
?????*?濾除帖子中的危險?HTML?代碼,?主要是腳本代碼,?滾動字幕代碼以及腳本事件處理代碼
?????*
?????*? @param ?content
?????*????????????需要濾除的字符串
?????*? @return ?過濾的結果
????? */
???? public ? static ?String?replaceHtmlCode(String?content)?{
???????? if ?(isEmpty(content))?{
???????????? return ? "" ;
????????}
???????? // ?需要濾除的腳本事件關鍵字
????????String[]?eventKeywords? = ?{? " onmouseover " ,? " onmouseout " ,? " onmousedown " ,
???????????????? " onmouseup " ,? " onmousemove " ,? " onclick " ,? " ondblclick " ,
???????????????? " onkeypress " ,? " onkeydown " ,? " onkeyup " ,? " ondragstart " ,
???????????????? " onerrorupdate " ,? " onhelp " ,? " onreadystatechange " ,? " onrowenter " ,
???????????????? " onrowexit " ,? " onselectstart " ,? " onload " ,? " onunload " ,
???????????????? " onbeforeunload " ,? " onblur " ,? " onerror " ,? " onfocus " ,? " onresize " ,
???????????????? " onscroll " ,? " oncontextmenu " ?};
????????content? = ?replace(content,? " <script " ,? " <script " ,? false );
????????content? = ?replace(content,? " </script " ,? " </script " ,? false );
????????content? = ?replace(content,? " <marquee " ,? " <marquee " ,? false );
????????content? = ?replace(content,? " </marquee " ,? " </marquee " ,? false );
???????? // ?FIXME?加這個過濾換行到?BR?的功能會把原始?HTML?代碼搞亂?2006-07-30
// ????????content?=?replace(content,?"\r\n",?"<BR>");
???????? // ?濾除腳本事件代碼
???????? for ?( int ?i? = ? 0 ;?i? < ?eventKeywords.length;?i ++ )?{
????????????content? = ?replace(content,?eventKeywords[i],
???????????????????? " _ " ? + ?eventKeywords[i],? false );? // ?添加一個"_",?使事件代碼無效
????????}
???????? return ?content;
????}
???? /**
?????*?濾除?HTML?代碼?為文本代碼.
????? */
???? public ? static ?String?replaceHtmlToText(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
???????? return ?setBr(setTag(input));
????}
???? /**
?????*?濾除?HTML?標記.
?????*?因為?XML?中轉義字符依然有效,?因此把特殊字符過濾成中文的全角字符.
?????*? @author ?beansoft
?????*? @param ?s?輸入的字串
?????*? @return ?過濾后的字串
????? */
???? public ? static ?String?setTag(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? char ?ch;
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
????????????ch? = ?s.charAt(i);
???????????? if ?(ch? == ? ' < ' )?{
????????????????stringbuffer.append( " < " );
// ????????????????stringbuffer.append("〈");
????????????}? else ? if ?(ch? == ? ' > ' )?{
????????????????stringbuffer.append( " > " );
// ????????????????stringbuffer.append("〉");
????????????}? else ? if ?(ch? == ? ' & ' )?{
????????????????stringbuffer.append( " & " );
// ????????????????stringbuffer.append("〃");
????????????}? else ? if ?(ch? == ? ' % ' )?{
????????????????stringbuffer.append( " %% " );
// ????????????????stringbuffer.append("※");
????????????}? else ?{
????????????????stringbuffer.append(ch);
????????????}
????????}
???????? return ?stringbuffer.toString();
????}
???? /** ?濾除?BR?代碼? */
???? public ? static ?String?setBr(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
???????????? if ?(s.charAt(i)? == ? ' \n ' ? || ?s.charAt(i)? == ? ' \r ' )?{
???????????????? continue ;
????????????}
????????????stringbuffer.append(s.charAt(i));
????????}
???????? return ?stringbuffer.toString();
????}
???? /** ?濾除空格? */
???? public ? static ?String?setNbsp(String?s)?{
???????? int ?j? = ?s.length();
????????StringBuffer?stringbuffer? = ? new ?StringBuffer(j? + ? 500 );
???????? for ?( int ?i? = ? 0 ;?i? < ?j;?i ++ )?{
???????????? if ?(s.charAt(i)? == ? ' ? ' )?{
????????????????stringbuffer.append( " " );
????????????}? else ?{
????????????????stringbuffer.append(s.charAt(i)? + ? "" );
????????????}
????????}
???????? return ?stringbuffer.toString();
????}
???? /**
?????*?判斷字符串是否全是數字字符或者點號.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?判斷結果,?true?為全數字,?false?為還有非數字字符
????? */
???? public ? static ? boolean ?isNumeric(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? false ;
????????}
???????? for ?( int ?i? = ? 0 ;?i? < ?input.length();?i ++ )?{
???????????? char ?charAt? = ?input.charAt(i);
???????????? if ?( ! Character.isDigit(charAt)? && ??(charAt? != ? ' . ' ?)?)?{
???????????????? return ? false ;
????????????}
????????}
???????? return ? true ;
????}
???? /**
?????*?轉換由表單讀取的數據的內碼(從?ISO8859?轉換到?gb2312).
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?toChi(String?input)?{
???????? try ?{
???????????? byte []?bytes? = ?input.getBytes( " ISO8859-1 " );
???????????? return ? new ?String(bytes,? " GBK " );
????????}? catch ?(Exception?ex)?{
????????}
???????? return ?input;
????}
???? /**
?????*?轉換由表單讀取的數據的內碼到?ISO(從?GBK?轉換到ISO8859-1).
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?toISO(String?input)?{
???????? return ?changeEncoding(input,? " GBK " ,? " ISO8859-1 " );
????}
???? /**
?????*?轉換字符串的內碼.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @param ?sourceEncoding
?????*????????????源字符集名稱
?????*? @param ?targetEncoding
?????*????????????目標字符集名稱
?????*? @return ?轉換結果,?如果有錯誤發生,?則返回原來的值
????? */
???? public ? static ?String?changeEncoding(String?input,?String?sourceEncoding,
????????????String?targetEncoding)?{
???????? if ?(input? == ? null ? || ?input.equals( "" ))?{
???????????? return ?input;
????????}
???????? try ?{
???????????? byte []?bytes? = ?input.getBytes(sourceEncoding);
???????????? return ? new ?String(bytes,?targetEncoding);
????????}? catch ?(Exception?ex)?{
????????}
???????? return ?input;
????}
???? /**
?????*?將單個的?'?換成?'';?SQL?規則:如果單引號中的字符串包含一個嵌入的引號,可以使用兩個單引號表示嵌入的單引號.
????? */
???? public ? static ?String?replaceSql(String?input)?{
???????? return ?replace(input,? " ' " ,? " '' " );
????}
???? /**
?????*?對給定字符進行?URL?編碼
????? */
???? public ? static ?String?encode(String?value)?{
???????? if ?(isEmpty(value))?{
???????????? return ? "" ;
????????}
???????? try ?{
????????????value? = ?java.net.URLEncoder.encode(value,? " GB2312 " );
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ?value;
????}
???? /**
?????*?對給定字符進行?URL?解碼
?????*
?????*? @param ?value
?????*????????????解碼前的字符串
?????*? @return ?解碼后的字符串
????? */
???? public ? static ?String?decode(String?value)?{
???????? if ?(isEmpty(value))?{
???????????? return ? "" ;
????????}
???????? try ?{
???????????? return ?java.net.URLDecoder.decode(value,? " GB2312 " );
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ?value;
????}
???? /**
?????*?判斷字符串是否未空,?如果為?null?或者長度為0,?均返回?true.
????? */
???? public ? static ? boolean ?isEmpty(String?input)?{
???????? return ?(input? == ? null ? || ?input.length()? == ? 0 );
????}
???? /**
?????*?獲得輸入字符串的字節長度(即二進制字節數),?用于發送短信時判斷是否超出長度.
?????*
?????*? @param ?input
?????*????????????輸入字符串
?????*? @return ?字符串的字節長度(不是?Unicode?長度)
????? */
???? public ? static ? int ?getBytesLength(String?input)?{
???????? if ?(input? == ? null )?{
???????????? return ? 0 ;
????????}
???????? int ?bytesLength? = ?input.getBytes().length;
???????? // System.out.println("bytes?length?is:"?+?bytesLength);
???????? return ?bytesLength;
????}
???? /**
?????*?檢驗字符串是否未空,?如果是,?則返回給定的出錯信息.
?????*
?????*? @param ?input
?????*????????????輸入的字符串
?????*? @param ?errorMsg
?????*????????????出錯信息
?????*? @return ?空串返回出錯信息
????? */
???? public ? static ?String?isEmpty(String?input,?String?errorMsg)?{
???????? if ?(isEmpty(input))?{
???????????? return ?errorMsg;
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的擴展名.
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?extension?portion?of?the?file's?name.
????? */
???? public ? static ?String?getExtension(String?fileName)?{
???????? if ?(fileName? != ? null )?{
???????????? int ?i? = ?fileName.lastIndexOf( ' . ' );
???????????? if ?(i? > ? 0 ? && ?i? < ?fileName.length()? - ? 1 )?{
???????????????? return ?fileName.substring(i? + ? 1 ).toLowerCase();
????????????}
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的前綴名.
?????*?@date?2005-10-18
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?prefix?portion?of?the?file's?name.
????? */
???? public ? static ?String?getPrefix(String?fileName)?{
???????? if ?(fileName? != ? null )?{
????????????fileName? = ?fileName.replace( ' \\ ' ,? ' / ' );
???????????? if (fileName.lastIndexOf( " / " )? > ? 0 )?{
????????????????fileName? = ?fileName.substring(fileName.lastIndexOf( " / " )? + ? 1 ,?fileName.length());
????????????}
???????????? int ?i? = ?fileName.lastIndexOf( ' . ' );
???????????? if ?(i? > ? 0 ? && ?i? < ?fileName.length()? - ? 1 )?{
???????????????? return ?fileName.substring( 0 ,?i);
????????????}
????????}
???????? return ? "" ;
????}
???? /**
?????*?得到文件的短路徑,?不保護目錄.
?????*?@date?2005-10-18
?????*
?????*? @param ?fileName
?????*????????????需要處理的文件的名字.
?????*? @return ?the?short?version?of?the?file's?name.
????? */
???? public ? static ?String?getShortFileName(String?fileName)?{
???????? if ?(fileName? != ? null )?{
????????????String?oldFileName? = ? new ?String(fileName);
????????????fileName? = ?fileName.replace( ' \\ ' ,? ' / ' );
????????????
???????????? // ?Handle?dir
???????????? if (fileName.endsWith( " / " ))?{
???????????????? int ?idx? = ?fileName.indexOf( ' / ' );
???????????????? if (idx? == ? - 1 ? || ?idx? == ?fileName.length()? - ? 1 )?{
???????????????????? return ?oldFileName;
????????????????}? else ?{
???????????????????? return ??oldFileName.substring(idx? + ? 1 ,?fileName.length()? - ? 1 );
????????????????}
????????????}
???????????? if (fileName.lastIndexOf( " / " )? > ? 0 )?{
????????????????fileName? = ?fileName.substring(fileName.lastIndexOf( " / " )? + ? 1 ,?fileName.length());
????????????}
???????????? return ?fileName;
????????}
???????? return ? "" ;
????}
???? /**
?????*?獲取表單參數并做默認轉碼,?從?ISO8859-1?轉換到?GBK.
?????*
?????*? @author ?BeanSoft
?????*?@date?2005-08-01
?????*
?????*? @param ?request
?????*????????????HttpServletRequest?對象
?????*? @param ?fieldName
?????*????????????參數名
?????*? @return ?取得的表單值
????? */
???? public ? static ?String?getParameter(HttpServletRequest?request,
????????????String?fieldName)?{
???????????? // ???????? // ?判斷編碼是否已經指定
// ????????String?encoding?=?request.getCharacterEncoding();
//
// ????????if("GBK".equalsIgnoreCase(encoding)?||?"GB2312".equalsIgnoreCase(encoding))?{
// ????????????return?request.getParameter(fieldName);
// ????????}
//
// ????????return?request(request,?fieldName);
???????? // ?2005-08-01?臨時修改
// ????????try?{
// ????????????request.setCharacterEncoding("UTF-8");
// ????????}?catch?(UnsupportedEncodingException?e)?{
// ???????????? // ?TODO?auto?generated?try-catch
// ????????????e.printStackTrace();
// ????????}
???????? return ?request.getParameter(fieldName);
????}
???? // ?------------------------------------?JSP?參數處理方法
???? /**
?????*?根據?Cookie?名稱得到請求中的?Cookie?值,?需要事先給?_request?一個初始值;?如果?Cookie?值是?null,?則返回?""
????? */
???? public ? static ?String?getCookieValue(HttpServletRequest?request,?String?name)?{
????????Cookie[]?cookies? = ?request.getCookies();
???????? if ?(cookies? == ? null )?{
???????????? return ? "" ;
????????}
???????? for ?( int ?i? = ? 0 ;?i? < ?cookies.length;?i ++ )?{
????????????Cookie?cookie? = ?cookies[i];
???????????? if ?(cookie.getName().equals(name))?{
???????????????? // ?需要對?Cookie?中的漢字進行?URL?反編碼,?適用版本:?Tomcat?4.0
???????????????? return ?decode(cookie.getValue());
???????????????? // ?不需要反編碼,?適用版本:?JSWDK?1.0.1
???????????????? // return?cookie.getValue();
????????????}
????????}
???????? // ?A?cookie?may?not?return?a?null?value,?may?return?a?""
???????? return ? "" ;
????}
???? // ?返回指定表單名的數組
???? public ?String[]?getParameterValues(HttpServletRequest?request,?String?name)?{
???????? // ?POST?方法的參數沒有編碼錯誤
???????? // if?(request.getMethod().equalsIgnoreCase("POST"))?{
???????? // ?文件上傳模式
???????? // if(isUploadMode)?{
???????? // ????return?request.getParameterValues(name);
???????? // }
???????? // ?--?For?Tomcat?4.0
???????? // return?request.getParameterValues(name);
???????? // ?--?For?JSWDK?1.0.1
???????? /*
?????????*?String?values[]?=?_request.getParameterValues(name);?if(values?!=
?????????*?null)?{?for(int?i?=?0;?i?<?values.length;?i++)?{?values[i]?=
?????????*?toChi(values[i]);?}?}?return?values;
????????? */
???????? // }
???????? // else?{
???????? // ?將通過?GET?方式發送的中文字符解碼(但是必須使用?java.net.URLEncoder?進行中文字符參數的編碼)
???????? // ?解碼時需使用內碼轉換,?也可使用反編碼,?即:?return?decode(_request.getParameter(name));
???????? // ?問題:?decode()?僅適用于?JDK?1.3?+?Tomcat?4.0
????????String?encoding? = ?request.getCharacterEncoding();
???????? if ( " GBK " .equalsIgnoreCase(encoding)? || ? " GB2312 " .equalsIgnoreCase(encoding))?{
???????????? return ?request.getParameterValues(name);
????????}
????????String?values[]? = ?request.getParameterValues(name);
???????? if ?(values? != ? null )?{
???????????? for ?( int ?i? = ? 0 ;?i? < ?values.length;?i ++ )?{
????????????????values[i]? = ?toChi(values[i]);
????????????}
????????}
???????? return ?values;
???????? // }
????}
???? /**
?????*?刪除指定的?Web?應用程序目錄下所上傳的文件
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? void ?deleteFile(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
????????????????file.delete();
????????????}
????????}
????}
???? /**
?????*?在指定的?Web?應用程序目錄下以指定路徑創建文件
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? boolean ?createFile(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
???????????????? try ?{
???????????????????? // ?創建文件
???????????????????? return ?file.createNewFile();
????????????????}? catch ?(IOException?e)?{
????????????????????System.err.println( " Unable?to?create?file? " ? + ?filePath);
????????????????}
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?在指定的?Web?應用程序目錄下以指定路徑創建目錄.
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
????? */
???? public ? static ? boolean ?createDir(ServletContext?application,?String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
???????????????? try ?{
???????????????????? // ?創建目錄
????????????????????java.io.File?dir? = ? new ?java.io.File(application
????????????????????????????.getRealPath(filePath));
???????????????????? return ?dir.mkdirs();
????????????????}? catch ?(Exception?e)?{
????????????????????System.err
????????????????????????????.println( " Unable?to?create?directory? " ? + ?filePath);
????????????????}
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?檢查指定的?Web?應用程序目錄下的文件是否存在.
?????*
?????*? @param ?application
?????*????????????JSP/Servlet?的?ServletContext
?????*? @param ?filePath
?????*????????????相對文件路徑
?????*? @return ?boolean?-?文件是否存在
????? */
???? public ? static ? boolean ?checkFileExists(ServletContext?application,
????????????String?filePath)?{
???????? if ?( ! isEmpty(filePath))?{
????????????String?physicalFilePath? = ?application.getRealPath(filePath);
???????????? if ?( ! isEmpty(physicalFilePath))?{
????????????????java.io.File?file? = ? new ?java.io.File(physicalFilePath);
???????????????? return ?file.exists();
????????????}
????????}
???????? return ? false ;
????}
???? /**
?????*?獲取文件圖標名.
?????*?Date:?2005-10
?????*? @param ?application?JSP/Servlet?的?ServletContext
?????*? @param ?iconDirPath?圖標文件夾的路徑
?????*? @param ?fileName?需要處理的文件名
?????*? @return ?圖標文件相對路徑
????? */
???? public ? static ?String?getFileIcon(ServletContext?application,
????????????String?iconDirPath,?String?fileName)?{
????????String?ext? = ?getExtension(fileName);
????????String?filePath? = ?iconDirPath? + ?ext? + ? " .gif " ;
// ????????return?filePath;
???????? if (checkFileExists(application,?filePath))?{
???????????? return ?filePath;
????????}
???????? return ?iconDirPath? + ? " file.gif " ;
????}
???? /**
?????*?Gets?the?absolute?pathname?of?the?class?or?resource?file?containing?the
?????*?specified?class?or?resource?name,?as?prescribed?by?the?current?classpath.
?????*
?????*? @param ?resourceName
?????*????????????Name?of?the?class?or?resource?name.
?????*? @return ?the?absolute?pathname?of?the?given?resource
????? */
???? public ? static ?String?getPath(String?resourceName)?{
???????? if ?( ! resourceName.startsWith( " / " ))?{
????????????resourceName? = ? " / " ? + ?resourceName;
????????}
???????? // resourceName?=?resourceName.replace('.',?'/');
????????java.net.URL?classUrl? = ? new ?StringUtil().getClass().getResource(
????????????????resourceName);
???????? if ?(classUrl? != ? null )?{
???????????? // System.out.println("\nClass?'"?+?className?+
???????????? // "'?found?in?\n'"?+?classUrl.getFile()?+?"'");
???????????? // System.out.println("\n資源?'"?+?resourceName?+
???????????? // "'?在文件?\n'"?+?classUrl.getFile()?+?"'?中找到.");
???????????? return ?classUrl.getFile();
????????}
???????? // System.out.println("\nClass?'"?+?className?+
???????? // "'?not?found?in?\n'"?+
???????? // System.getProperty("java.class.path")?+?"'");
???????? // System.out.println("\n資源?'"?+?resourceName?+
???????? // "'?沒有在類路徑?\n'"?+
???????? // System.getProperty("java.class.path")?+?"'?中找到");
???????? return ? null ;
????}
???? /**
?????*?將日期轉換為中文表示方式的字符串(格式為?yyyy年MM月dd日?HH:mm:ss).
????? */
???? public ? static ?String?dateToChineseString(Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? "" ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyy年MM月dd日?HH:mm:ss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將日期轉換為?14?位的字符串(格式為yyyyMMddHHmmss).
????? */
???? public ? static ?String?dateTo14String(Date?date)?{
???????? if ?(date? == ? null )?{
???????????? return ? null ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss " );
???????? return ?dateFormat.format(date);
????}
???? /**
?????*?將?14?位的字符串(格式為yyyyMMddHHmmss)轉換為日期.
????? */
???? public ? static ?Date?string14ToDate(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? null ;
????????}
???????? if ?(input.length()? != ? 14 )?{
???????????? return ? null ;
????????}
????????java.text.SimpleDateFormat?dateFormat? = ? new ?java.text.SimpleDateFormat(
???????????????? " yyyyMMddHHmmss " );
???????? try ?{
???????????? return ?dateFormat.parse(input);
????????}? catch ?(Exception?ex)?{
????????????ex.printStackTrace();
????????}
???????? return ? null ;
????}
???? // ?-----------------------------------------------------------
???? // ?----------?字符串和數字轉換工具方法,?2004.03.27?添加?--------
???? // ------------------------------------------------------------
???? public ? static ? byte ?getByte(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?Byte.parseByte(httpservletrequest.getParameter(s));
????}
????
???? /**
?????*?獲取?boolean?參數從ServletRequest中.
?????*? @param ?request
?????*? @param ?name
?????*? @return
????? */
???? public ? static ? boolean ?getBoolean(HttpServletRequest?request,?String?name)?{
???????? return ?Boolean.valueOf(request.getParameter(name));
????}
???? /**
?????*從請求對象中讀取參數返回為整數.
?????*
????? */
???? public ? static ? int ?getInt(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?parseInt(httpservletrequest.getParameter(s));
????}
???? public ? static ? long ?getLong(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0L ;
????????}
???????? return ?parseLong(httpservletrequest.getParameter(s));
????}
???? public ? static ? double ?getDouble(HttpServletRequest?httpservletrequest,?String?s)?{
???????? if ?(httpservletrequest.getParameter(s)? == ? null
???????????????? || ?httpservletrequest.getParameter(s).equals( "" ))?{
???????????? return ? 0 ;
????????}
???????? return ?parseDouble(httpservletrequest.getParameter(s));
????}
???? /**
?????*?將?TEXT?文本轉換為?HTML?代碼,?已便于網頁正確的顯示出來.
?????*
?????*? @param ?input
?????*????????????輸入的文本字符串
?????*? @return ?轉換后的?HTML?代碼
????? */
???? public ? static ?String?textToHtml(String?input)?{
???????? if ?(isEmpty(input))?{
???????????? return ? "" ;
????????}
????????input? = ?replace(input,? " < " ,? " < " );
????????input? = ?replace(input,? " > " ,? " > " );
????????input? = ?replace(input,? " \n " ,? " <br>\n " );
????????input? = ?replace(input,? " \t " ,? " " );
????????input? = ?replace(input,? " ?? " ,? " " );
???????? return ?input;
????}
???? public ? static ?String?toQuoteMark(String?s)?{
????????s? = ?replaceString(s,? " ' " ,? " ' " );
????????s? = ?replaceString(s,? " \ "" ,? " & # 34 ; " );
????????s? = ?replaceString(s,? " \r\n " ,? " \n " );
???????? return ?s;
????}
???? public ? static ?String?replaceChar(String?s,? char ?c,? char ?c1)?{
???????? if ?(s? == ? null )?{
???????????? return ? "" ;
????????}
???????? return ?s.replace(c,?c1);
????}
???? public ? static ?String?replaceString(String?s,?String?s1,?String?s2)?{
???????? if ?(s? == ? null ? || ?s1? == ? null ? || ?s2? == ? null )?{
???????????? return ? "" ;
????????}
???????? return ?s.replaceAll(s1,?s2);
????}
???? public ? static ?String?toHtml(String?s)?{
????????s? = ?replaceString(s,? " < " ,? " < " );
????????s? = ?replaceString(s,? " > " ,? " > " );
???????? return ?s;
????}
???? public ? static ?String?toBR(String?s)?{
????????s? = ?replaceString(s,? " \n " ,? " <br>\n " );
????????s? = ?replaceString(s,? " \t " ,? " " );
????????s? = ?replaceString(s,? " ?? " ,? " " );
???????? return ?s;
????}
???? public ? static ?String?toSQL(String?s)?{
????????s? = ?replaceString(s,? " \r\n " ,? " \n " );
???????? return ?s;
????}
???? public ? static ?String?replaceEnter(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " \n " ,? " <br> " );
????}
???? public ? static ?String?replacebr(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " <br> " ,? " \n " );
????}
???? public ? static ?String?replaceQuote(String?s)? throws ?NullPointerException?{
???????? return ?s.replaceAll( " ' " ,? " '' " );
????}
???? // ?Test?only.
???? public ? static ? void ?main(String[]?args)? throws ?Exception?{
????????System.out.println(formatFraction( 0.693431014112044 ,? 1 ,? 3 ));
????????
???????? // System.out.println(textToHtml("1<2\r\n<b>Bold</b>"));
???????? // System.out.println(scriptAlert("oh!"));
???????? // System.out.println(scriptRedirect(" http://localhost/ "));
???????? // ????System.out.println(StringUtil.getPath("/databaseconfig.properties"));
???????? // ????????java.io.File?file?=?new?java.io.File("e:\\Moblog\\abcd\\");
???????? //
???????? // ????????file.mkdir();
// ????????Date?time?=?(parseHMSStringToDate("12:23:00"));
// ????????System.out.println(time.toLocaleString());
// ????????Date?nowTime?=?parseHMSStringToDate(formatDateToHMSString(new?Date()));
// ????????System.out.println(nowTime.toLocaleString());
???????? // ????????GregorianCalendar?cal?=?new?GregorianCalendar();
???????? // ????????cal.setTime(new?Date());
???????? // ????????cal.add(cal.YEAR,?-cal.get(cal.YEAR)?+?1970);
???????? // ????????cal.add(cal.MONTH,?-cal.get(cal.MONTH));
???????? // ????????cal.add(cal.DATE,?-cal.get(cal.DATE)?+?1);
???????? //
???????? // ????????System.out.println(cal.getTime().toLocaleString());
????}