TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一个字符串工具类
package studio.beansoft.jsp; import javax.Servlet.ServletContext;
import javax.servlet.http.*;
import java.io.*; /**
* <p>Title: 字符串工具类</p>
* <p>Description: 一些方便的字符串工具方法.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: 世纪东方</p>
* @author 刘长炯
* @version 1.0
*/
public class StringUtil {
public StringUtil() {
} // 声明一些方便内部方法 // ------------------------------------ 字符串处理方法 ----------------------------------------------
// 将字符串 source 中的 oldStr 替换为 newStr, 并以大小写敏感方式进行查找
public String replace(String source, String oldStr, String newStr) {
return replace(source, oldStr, newStr, true);
} // 将字符串 source 中的 oldStr 替换为 newStr, matchCase 为是否设置大小写敏感查找
public 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;
} // 滤除帖子中的危险 HTML 代码, 主要是脚本代码, 滚动字幕代码以及脚本事件处理代码
public 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);
content = replace(content, "
", "<BR>");
// 滤除脚本事件代码
for (int i = 0; i < eventKeywords.length; i++) {
content = replace(content, eventKeywords, "_" + eventKeywords, false); // 添加一个"_", 使事件代码无效
} return content;
} //******************************** 滤除 HTML 代码 为文本代码 *****************************
public String replaceHtmlToText(String input) {
if(isEmpty(input)) {
return "";
}
return setBr(setTag(input));
} // 滤除 HTML 标记
public String setTag(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 if(s.charAt(i) == ">")
stringbuffer.append(">");
else if(s.charAt(i) == "&")
stringbuffer.append("&");
else
stringbuffer.append(s.charAt(i)); return stringbuffer.toString();
} // 滤除 BR 代码 public 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) == "
")
stringbuffer.append("");
else if(s.charAt(i) == "
")
stringbuffer.append("");
else
stringbuffer.append(s.charAt(i)); return stringbuffer.toString();
} // 滤除空格
public 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();
} // 判断字符串是否全是数字字符
public boolean isNumeric(String input) {
if (isEmpty(input))
return false;
for (int i = 0; i < input.length(); i++) {
char charAt = input.charAt(i);
if (charAt < "0" || charAt > "9") {
return false;
}
}
return true;
} // 转换由表单读取的数据的内码
public String toChi(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes);
}
catch (Exception ex) {
}
return null;
} // 将单个的 " 换成 ""; SQL 规则:如果单引号中的字符串包含一个嵌入的引号,可以使用两个单引号表示嵌入的单引号。
public String replaceSql(String input) {
return replace(input, """, """");
} // 对给定字符进行 URL 编码
public String encode(String value) {
if (isEmpty(value))
return "";
return java.net.URLEncoder.encode(value);
} // 对给定字符进行 URL 解码
public String decode(String value) {
if (isEmpty(value))
return "";
return java.net.URLDecoder.decode(value);
} /**
* 判断字符串是否未空
*/
public boolean isEmpty(String input) {
if (input == null || input.length() <= 0)
return true;
return false;
} // 将心情符号修改为对应的图片 ------------- 请修改页面中相关代码!
public String smilies(String temp) {
if (isEmpty(temp))
return "";
// 判断是否有禁止表情字符的表单值
//if( isEmpty(request("smilies")) ) {
temp = replace(temp, "/:)",
"<IMG border=0 SRC=images/brow/regular_smile.gif>");
temp = replace(temp, "/:d",
"<IMG border=0 SRC=images/brow/teeth_smile.gif>");
temp = replace(temp, "/:o", "<IMG border=0 SRC=images/brow/omg_smile.gif>");
temp = replace(temp, "/:p",
"<IMG border=0 SRC=images/brow/tounge_smile.gif>");
temp = replace(temp, "/;)", "<IMG border=0 SRC=images/brow/wink_smile.gif>");
temp = replace(temp, "/:(", "<IMG border=0 SRC=images/brow/sad_smile.gif>");
temp = replace(temp, "/:s",
"<IMG border=0 SRC=images/brow/confused_smile.gif>");
temp = replace(temp, "/:|",
"<IMG border=0 SRC=images/brow/whatchutalkingabout_smile.gif>");
temp = replace(temp, "/:$",
"<IMG border=0 SRC=images/brow/embaressed_smile.gif>");
//}
return temp;
} /**
* 得到文件的扩展名.
* @param fileName 需要处理的文件的名字.
* @return the extension portion of the file"s name.
*/
public 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 "";
} /**
* 得到文件的前缀名.
* @param fileName 需要处理的文件的名字.
* @return the prefix portion of the file"s name.
*/
public String getPrefix(String fileName) {
if (fileName != null) {
int i = fileName.lastIndexOf(".");
if (i > 0 && i < fileName.length() - 1) {
return fileName.substring(0, i);
}
}
return "";
} // ------------------------------------ JSP 参数处理方法 ----------------------------------------------
// 一个与 ASP 类似的方法, 返回表单域的值, 并转换内码 -- 仅适用于 Tomcat 4.0
public String request(HttpServletRequest request, String fieldName) {
// POST 方法的参数没有编码错误
if (request.getMethod().equalsIgnoreCase("POST")) {
// 文件上传模式
//if(isUploadMode) {
// return request.getParameter(fieldName);
//}
// For Tomcat 4.0.6
return toChi(request.getParameter(fieldName));
}
else {
// 将通过 GET 方式发送的中文字符解码(但是必须使用 java.net.URLEncoder 进行中文字符参数的编码)
// 解码时需使用内码转换, 也可使用反编码, 即: return decode(request.getParameter(fieldName));
// 问题: decode() 仅适用于 JDK 1.3 + Tomcat 4.0
return toChi(request.getParameter(fieldName));
}
} // 如果表单的值是 null, 则返回 "", 避免 NullPointerException
public String request1(HttpServletRequest request, String fieldName) {
String s = request(request, fieldName);
if (s == null)
return "";
else
return s;
} // 获得指定表单域的值, 并将单个的 " 换成 ""; SQL 规则:如果单引号中的字符串包含一个嵌入的引号,
// 可以使用两个单引号表示嵌入的单引号。
public String requestSql(HttpServletRequest request, String fieldName) {
return replaceSql(request1(request, fieldName));
} // 根据 Cookie 名称得到请求中的 Cookie 值, 需要事先给 _request 一个初始值; 如果 Cookie 值是 null, 则返回 ""
public 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;
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 = toChi(values);
}
}
return values;*/
}
else {
// 将通过 GET 方式发送的中文字符解码(但是必须使用 java.net.URLEncoder 进行中文字符参数的编码)
// 解码时需使用内码转换, 也可使用反编码, 即: return decode(_request.getParameter(name));
// 问题: decode() 仅适用于 JDK 1.3 + Tomcat 4.0
String values[] = request.getParameterValues(name);
if (values != null) {
for (int i = 0; i < values.length; i++) {
values = toChi(values);
}
}
return values;
}
} // 删除指定的 Web 应用程序目录下所上传的文件
public 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();
}
}
} }
源码下载:http://file.javaxxz.com/2014/10/1/035112547.zip |
|