`

java 常用小工具总结

 
阅读更多
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.misc.BASE64Decoder;

public class Util {
	static final Logger log = LoggerFactory.getLogger(Util.class);

	private static String emailAddress = null;

	private static final SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	static {
		getEmailAddress();
	}

	/**
	 * 在源字符串中从右开始对匹配串中的子串按右开始进行逐一查找,直到左边最后一个匹配项 在源字符串中的位置 相对于StartPos的第一次匹配
	 * 
	 * 
	 * @param SourceContent
	 *            源字符串
	 * @param StartPos
	 *            开始位置
	 * 
	 * @param MatchRule
	 *            匹配串或匹配表达式
	 * 
	 * @return
	 */
	public static int MoveToLastIndexOf(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = List.size() - 1; i > -1; i--) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.lastIndexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							LastPos = Pos;
							Pos -= param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.lastIndexOf(MatchRule, Pos);
			}
			return LastPos;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static int MoveToLastIndexOf_f(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = List.size() - 1; i > -1; i--) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.lastIndexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							if (LastPos < 0)
								LastPos = Pos;
							Pos -= param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.lastIndexOf(MatchRule, Pos);
			}
			return LastPos;
		} catch (Exception e) {

			e.printStackTrace();
			return -1;
		}
	}

	public static int MoveToLastIndexOf_l(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = List.size() - 1; i > -1; i--) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.lastIndexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							LastPos = Pos;
							Pos -= param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.lastIndexOf(MatchRule, Pos);
			}
			return LastPos;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 在源字符串中从左开始对匹配串中的子串按左开始进行逐一查找,直到右边最后一个匹配项 在源字符串中的位置, 相对于StartPos的第一次匹配
	 * 
	 * 
	 * @param SourceContent
	 *            源字符串
	 * @param StartPos
	 *            开始位置
	 * 
	 * @param MatchRule
	 *            匹配串或匹配表达式
	 * 
	 * @return
	 */
	public static int MoveToIndexOf(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = 0; i < List.size(); i++) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.indexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							LastPos = Pos;
							Pos += param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.indexOf(MatchRule, Pos);
			}
			return LastPos;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static int MoveToIndexOf_f(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = 0; i < List.size(); i++) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.indexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							if (LastPos < 0)
								LastPos = Pos;
							Pos += param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.indexOf(MatchRule, Pos);
			}
			return LastPos;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static int MoveToIndexOf_l(String SourceContent, int StartPos,
			String MatchRule) {
		try {
			if (SourceContent.length() < 1)
				return -1;
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			if (List.size() > 0) {
				for (int i = 0; i < List.size(); i++) {
					SubStrParam param = List.get(i);
					if (param.isIndex) {
						Pos = SourceContent.indexOf(param.Index, Pos);
						if (Pos < 0) {
							return -1;
						} else {
							param.Point = Pos;
							LastPos = Pos + param.Index.length();
							Pos += param.Index.length();
						}
					}
				}
			} else {
				LastPos = SourceContent.indexOf(MatchRule, Pos);
				if (LastPos > -1) {
					LastPos += MatchRule.length();
				}
			}
			return LastPos;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static SubStrResultList getSubStrings(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String[] SubMatchRules) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubString(SourceContent, pos,
						MatchRangeStart, MatchRangeEnd, SubMatchRules);
				pos = ssr.CurrentPos;
				if (pos > -1 && ssr.MatchStatus) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	/**
	 * 将SourceContent内以StartPos起点将最近的MatchRangeStart 和 MatchRangeEnd
	 * 之间的字符截取,截取后的内容使用集合SubMatchRules中的依次进行匹配,找到第一个满足条件的的正确匹配后,返回
	 * 
	 * 注意返回的SubStrResult.CurrentPos位置是相对与StartPos的MatchRangeEnd的位置
	 * 
	 * 
	 * @param SourceContent
	 * @param StartPos
	 * @param MatchRangeStart
	 * @param MatchRangeEnd
	 * @param SubMatchRules
	 * @return
	 */
	public static SubStrResult SubString(String SourceContent, int StartPos,
			String MatchRangeStart, String MatchRangeEnd, String[] SubMatchRules) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			if (MatchRangeStart.length() > 0 && MatchRangeEnd.length() > 0) {
				// int PosA = SourceContent.indexOf(MatchRangeStart, StartPos);
				int PosA = MoveToIndexOf_l(SourceContent, StartPos,
						MatchRangeStart);
				if (PosA > -1) {
					// PosA += MatchRangeStart.length();
					// int PosB = SourceContent.indexOf(MatchRangeEnd, PosA);
					int PosB = MoveToIndexOf_f(SourceContent, PosA,
							MatchRangeEnd);
					if (PosB > -1) {
						String TempContent = SourceContent
								.substring(PosA, PosB);
						for (int i = 0; i < SubMatchRules.length; i++) {
							SubStrResult subResult = SubString(TempContent, 0,
									SubMatchRules[i]);
							if (subResult.CurrentPos > -1) {
								subResult.CurrentPos = PosB;
								subResult.MatchStatus = true;
								return subResult;
							}

						}
						// 表示子匹配项没有成功,但是块能成功,可以继续查找下去
						result.CurrentPos = PosB;
						result.MatchStatus = false;
						return result;
					}
				}
				result.CurrentPos = -1;
				return result;
			} else {
				result.CurrentPos = -1;
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();

			result.CurrentPos = -1;
			return result;
		}
	}

	public static SubStrResultList getSubStringsForLast(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String[] SubMatchRules) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubStringForLast(SourceContent, pos,
						MatchRangeStart, MatchRangeEnd, SubMatchRules);
				pos = ssr.CurrentPos;
				if (pos > -1 && ssr.MatchStatus) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	public static SubStrResult SubStringForLast(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String[] SubMatchRules) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		int StartPos_ = StartPos;
		if (StartPos_ == 0) {
			StartPos_ = SourceContent.length();
		}
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			if (MatchRangeStart.length() > 0 && MatchRangeEnd.length() > 0) {
				// int PosA = SourceContent.indexOf(MatchRangeStart, StartPos);
				int PosA = MoveToLastIndexOf_l(SourceContent, StartPos_,
						MatchRangeEnd);
				if (PosA > -1) {
					// PosA += MatchRangeStart.length();
					// int PosB = SourceContent.indexOf(MatchRangeEnd, PosA);
					int PosB = MoveToLastIndexOf_f(SourceContent, PosA,
							MatchRangeStart);
					if (PosB > -1) {
						String TempContent = SourceContent
								.substring(PosB, PosA);
						for (int i = 0; i < SubMatchRules.length; i++) {
							SubStrResult subResult = SubString(TempContent, 0,
									SubMatchRules[i]);
							if (subResult.CurrentPos > -1) {
								subResult.CurrentPos = PosB;
								subResult.MatchStatus = true;
								return subResult;
							}

						}
						// 表示子匹配项没有成功,但是块能成功,可以继续查找下去
						result.CurrentPos = PosB;
						result.MatchStatus = false;
						return result;
					}
				}
				result.CurrentPos = -1;
				return result;
			} else {
				result.CurrentPos = -1;
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();

			result.CurrentPos = -1;
			return result;
		}
	}

	public static SubStrResultList getSubStrings(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String SubMatchRule) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubString(SourceContent, pos,
						MatchRangeStart, MatchRangeEnd, SubMatchRule);
				pos = ssr.CurrentPos;
				if (pos > -1 && ssr.MatchStatus) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	/**
	 * 将SourceContent内以StartPos起点将最近的MatchRangeStart 和 MatchRangeEnd
	 * 之间的字符截取,截取后的内容以SubMatchRule,进行匹配
	 * 
	 * 注意返回的SubStrResult.CurrentPos位置是相对与StartPos的MatchRangeEnd的位置
	 * 
	 * 
	 * @param SourceContent
	 * @param StartPos
	 * @param MatchRangeStart
	 * @param MatchRangeEnd
	 * @param SubMatchRule
	 * @return
	 */
	public static SubStrResult SubString(String SourceContent, int StartPos,
			String MatchRangeStart, String MatchRangeEnd, String SubMatchRule) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			if (MatchRangeStart.length() > 0 && MatchRangeEnd.length() > 0) {
				// int PosA = SourceContent.indexOf(MatchRangeStart, StartPos);
				int PosA = MoveToIndexOf_l(SourceContent, StartPos,
						MatchRangeStart);
				if (PosA > -1) {
					// PosA += MatchRangeStart.length();
					// int PosB = SourceContent.indexOf(MatchRangeEnd, PosA);
					int PosB = MoveToIndexOf_f(SourceContent, PosA,
							MatchRangeEnd);
					if (PosB > -1) {
						String TempContent = SourceContent
								.substring(PosA, PosB);

						SubStrResult subResult = SubString(TempContent, 0,
								SubMatchRule);
						if (subResult.CurrentPos > -1) {
							subResult.MatchStatus = true;
						}
						subResult.CurrentPos = PosB;
						return subResult;
					}
				}
				result.CurrentPos = -1;
				return result;
			} else {
				return SubString(SourceContent, StartPos, SubMatchRule);
			}
		} catch (Exception e) {
			e.printStackTrace();

			result.CurrentPos = -1;
			return result;
		}
	}

	public static SubStrResultList getSubStringsForLast(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String SubMatchRule) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubStringForLast(SourceContent, pos,
						MatchRangeStart, MatchRangeEnd, SubMatchRule);
				pos = ssr.CurrentPos;
				if (pos > -1 && ssr.MatchStatus) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	public static SubStrResult SubStringForLast(String SourceContent,
			int StartPos, String MatchRangeStart, String MatchRangeEnd,
			String SubMatchRule) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		int StartPos_ = StartPos;
		if (StartPos_ == 0) {
			StartPos_ = SourceContent.length();
		}
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			if (MatchRangeStart.length() > 0 && MatchRangeEnd.length() > 0) {
				// int PosA = SourceContent.indexOf(MatchRangeStart, StartPos);
				int PosA = MoveToLastIndexOf_l(SourceContent, StartPos_,
						MatchRangeEnd);
				if (PosA > -1) {
					// PosA += MatchRangeStart.length();
					// int PosB = SourceContent.indexOf(MatchRangeEnd, PosA);
					int PosB = MoveToLastIndexOf_f(SourceContent, PosA,
							MatchRangeStart);
					if (PosB > -1) {
						String TempContent = SourceContent
								.substring(PosB, PosA);

						SubStrResult subResult = SubString(TempContent, 0,
								SubMatchRule);
						if (subResult.CurrentPos > -1) {
							subResult.MatchStatus = true;
						}
						subResult.CurrentPos = PosB;
						return subResult;
					}
				}
				result.CurrentPos = -1;
				return result;
			} else {
				return SubString(SourceContent, StartPos, SubMatchRule);
			}
		} catch (Exception e) {
			e.printStackTrace();

			result.CurrentPos = -1;
			return result;
		}
	}

	public static SubStrResultList getSubStrings(String SourceContent,
			int StartPos, String MatchRule) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubString(SourceContent, pos, MatchRule);
				pos = ssr.CurrentPos;
				if (pos > -1) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	/**
	 * 在SourceContent中以StartPos为起点,找到最近满足MatchRule的值。
	 * 
	 * 
	 * @param SourceContent
	 * @param StartPos
	 * @param MatchRule
	 * @return
	 */
	public static SubStrResult SubString(String SourceContent, int StartPos,
			String MatchRule) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			int LastPos = -1;
			for (int i = 0; i < List.size(); i++) {
				SubStrParam param = List.get(i);
				if (param.isIndex) {
					Pos = SourceContent.indexOf(param.Index, Pos);
					if (Pos < 0) {
						result.CurrentPos = -1;
						result.ErrorInfo = "indexof :" + param.Index;
						return result;
					} else {
						param.Point = Pos;
						LastPos = Pos;
						Pos += param.Index.length();
					}
				}
			}
			for (int i = 0; i < List.size(); i++) {
				SubStrParam param = List.get(i);
				if (!param.isIndex && !param.Index.equals("*")) {
					SubStrParam paramA = List.get(i - 1);
					SubStrParam paramB = List.get(i + 1);
					int PosA = paramA.Point + paramA.Index.length();
					int PosB = paramB.Point;
					String Data = "";
					try {
						Data = SourceContent.substring(PosA, PosB);
					} catch (Exception e) {
						result.CurrentPos = -1;
						return result;
					}
					result.Add(param.Index, Data.trim());
				}
			}
			result.CurrentPos = LastPos;
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.CurrentPos = -1;
			return result;
		}
	}

	public static SubStrResultList getSubStringsForLast(String SourceContent,
			int StartPos, String MatchRule) {
		SubStrResultList ssrl = new SubStrResultList();
		try {
			int pos = StartPos;
			int Count = 0;
			while (pos > -1 && Count < 10000) {
				SubStrResult ssr = SubStringForLast(SourceContent, pos,
						MatchRule);
				pos = ssr.CurrentPos;
				if (pos > -1) {
					ssrl.addItem(ssr);
				}
				Count++;
			}
			return ssrl;
		} catch (Exception e) {
			ssrl.setErrorInfo(e.getMessage());
			return ssrl;
		}
	}

	public static SubStrResult SubStringForLast(String SourceContent,
			int StartPos, String MatchRule) {
		SubStrResult result = new SubStrResult();
		result.CurrentPos = -1;
		if (SourceContent == null || SourceContent.length() < 1)
			return result;
		try {
			ArrayList<SubStrParam> List = GetParamList(MatchRule);
			int Pos = StartPos;
			if (Pos == 0) {
				Pos = SourceContent.length();
			}
			int LastPos = -1;
			for (int i = List.size() - 1; i >= 0; i--) {
				SubStrParam param = List.get(i);
				if (param.isIndex) {
					Pos = SourceContent.lastIndexOf(param.Index, Pos);
					if (Pos < 0) {
						result.CurrentPos = -1;
						result.ErrorInfo = "indexof :" + param.Index;
						return result;
					} else {
						param.Point = Pos;
						LastPos = Pos - param.Index.length();
						Pos -= param.Index.length();
					}
				}
			}
			for (int i = 0; i < List.size(); i++) {
				SubStrParam param = List.get(i);
				if (!param.isIndex && !param.Index.equals("*")) {
					SubStrParam paramA = List.get(i - 1);
					SubStrParam paramB = List.get(i + 1);
					int PosA = paramA.Point + paramA.Index.length();
					int PosB = paramB.Point;
					String Data = "";
					try {
						Data = SourceContent.substring(PosA, PosB);
					} catch (Exception e) {
						result.CurrentPos = -1;
						return result;
					}
					result.Add(param.Index, Data.trim());
				}
			}
			result.CurrentPos = LastPos;
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.CurrentPos = -1;
			return result;
		}
	}

	/**
	 * 解析匹配表达式
	 * 
	 * 
	 * @param vMatchRule
	 * @return
	 */
	private static ArrayList<SubStrParam> GetParamList(String vMatchRule) {
		ArrayList<SubStrParam> List = new ArrayList<SubStrParam>();
		if (vMatchRule.length() < 1)
			return List;
		try {
			String StrIndexA = "{@{";
			String StrIndexB = "}";
			int PosA = vMatchRule.indexOf(StrIndexA);
			int PosB = 0;
			String IndexA = "";
			if (PosA > -1) {
				IndexA = vMatchRule.substring(0, PosA);
				SubStrParam param = new SubStrParam();
				param.isIndex = true;
				param.Index = IndexA;
				param.Point = -1;
				List.add(param);
			} else {
				List.clear();
				return List;
			}
			while (PosA > -1) {
				PosA += StrIndexA.length();
				PosB = vMatchRule.indexOf(StrIndexB, PosA);
				if (PosB > -1) {
					String VarName = vMatchRule.substring(PosA, PosB);
					SubStrParam param = new SubStrParam();
					param.isIndex = false;
					param.Index = VarName;
					param.Point = -1;
					List.add(param);
					PosB += StrIndexB.length();
					PosA = vMatchRule.indexOf(StrIndexA, PosB);
					if (PosA < 0) {
						IndexA = vMatchRule
								.substring(PosB, vMatchRule.length());
						SubStrParam param1 = new SubStrParam();
						param1.isIndex = true;
						param1.Index = IndexA;
						param1.Point = -1;
						List.add(param1);
						break;
					} else {
						IndexA = vMatchRule.substring(PosB, PosA);
						SubStrParam param1 = new SubStrParam();
						param1.isIndex = true;
						param1.Index = IndexA;
						param1.Point = -1;
						List.add(param1);
					}
				} else {
					List.clear();
					return List;
				}
			}
			return List;
		} catch (Exception e) {
			e.printStackTrace();
			return List;
		}
	}

	public static String ReadFile(String FileName) {
		try {
			FileReader fr = new FileReader(FileName);
			BufferedReader br = new BufferedReader(fr);
			try {
				StringBuffer buff = new StringBuffer();
				String record = new String();
				while (((record = br.readLine()) != null)) {
					buff.append(record);
				}
				return buff.toString();
			} finally {
				br.close();
				fr.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static void SaveFile(String FileName, String Content) {
		try {
			FileWriter fw = new FileWriter(FileName);
			PrintWriter out = new PrintWriter(fw);
			try {
				out.println(Content);
			} finally {
				out.close();
				fw.close();
			}

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	public static String GetSubmit(String Content) {
		try {
			int Pos = 0;
			while (Pos > -1) {
				Pos = Content.indexOf("<", Pos);
				if (Pos > -1) {
					Pos++;
					String Temp = "";
					while (Pos < Content.length()
							&& (Temp = Content.substring(Pos, Pos + 1)) == " ") {
						Pos++;
					}
					if (Pos < (Content.length() - "input".length())) {
						Temp = Content.substring(Pos, Pos + "input".length());
						if (Temp.equalsIgnoreCase("input")) {
							int PosB = Content.indexOf(">", Pos);
							if (PosB > -1) {
								Temp = Content.substring(Pos, PosB);
								System.out.println(Temp);
							}
						}
					}

				}
			}
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String stripNonValidXMLCharacters(String instr) {
		StringBuffer outstr = new StringBuffer(); // Used to hold the output.
		char current; // Used to reference the current character.

		if (instr == null || ("".equals(instr)))
			return ""; // vacancy test.
		for (int i = 0; i < instr.length(); i++) {
			current = instr.charAt(i); // NOTE: No IndexOutOfBoundsException
			// caught
			// here; it should not happen.
			if ((current == 0x9) || (current == 0xA) || (current == 0xD)
					|| ((current >= 0x20) && (current <= 0xD7FF))
					|| ((current >= 0xE000) && (current <= 0xFFFD))
					|| ((current >= 0x10000) && (current <= 0x10FFFF)))
				outstr.append(current);
		}
		return outstr.toString();
	}

	public static int parseInt(String str) {
		int res = 0;
		try {
			res = Integer.parseInt(str);
		} catch (Exception e) {
			// System.out.println("parseInt error : =0");
		}
		return res;
	}

	public static int parseInt(String str, int defaultValue) {
		int res = 0;
		try {
			res = Integer.parseInt(str);
		} catch (Exception e) {
			res = defaultValue;
			// System.out.println("parseInt error : ="+defaultValue);
		}
		return res;
	}

	public static double parseDouble(String str) {
		double res = 0.0;
		try {
			res = Double.parseDouble(str);
		} catch (Exception e) {
			// System.out.println("parseDouble error : =0.0");
		}
		return res;
	}

	public static double parseDouble(String str, double defaultValue) {
		double res = 0.0;
		try {
			res = Double.parseDouble(str);
		} catch (Exception e) {
			res = defaultValue;
			// System.out.println("parseDouble error : ="+defaultValue);
		}
		return res;
	}

	public static String eraseHtmlTags(String str, String space) {
		String res = null;
		int pos1 = str.indexOf("<");
		int pos2 = str.indexOf(">") + 1;
		int pose = str.length();
		if ((pos1 > -1) && (pos2 > -1) && (pos1 < pos2)) {
			str = str.substring(0, pos1).trim() + space
					+ str.substring(pos2, pose).trim();
			res = eraseHtmlTags(str, space);
		} else {
			res = str;
		}
		return res;
	}

	public static String eraseHtmlTags(String str) {
		String space = " ";
		String res = null;
		int pos1 = str.indexOf("<");
		int pos2 = str.indexOf(">");
		int pose = str.length();
		if ((pos1 > -1) && (pos2 > -1) && (pos1 < pos2)) {
			if (pos1 == 0) {
				str = str.substring(pos2 + 1, pose).trim();
			} else if (pos2 == pose - 1) {
				str = str.substring(0, pos1).trim();
			} else {
				str = str.substring(0, pos1).trim()
						+ str.substring(pos2 + 1).trim();
			}
			res = eraseHtmlTags(str);
		} else {
			res = str;
		}
		return res;
	}

	public static String str2Number(String szFloat) {
		String result = szFloat.replace(",", "");
		if (result.trim().length() < 1)
			return "";
		String retSzFloat = "";
		int nFind = 0;
		boolean ndot = false;
		for (int i = 0; i < result.length(); i++) {
			if ((result.charAt(i) <= '9' && result.charAt(i) >= '0')
					|| (result.charAt(i) == '.' && !ndot)) {
				if (result.charAt(i) == '.')
					ndot = true;
				nFind = 1;
				Character tt = result.charAt(i);
				retSzFloat += tt.toString();
			} else if (nFind == 1)
				break;
		}
		if (retSzFloat.length() > 0 && retSzFloat.charAt(0) == '.')
			retSzFloat = "0" + retSzFloat;
		if (retSzFloat.length() > 0
				&& retSzFloat.charAt(retSzFloat.length() - 1) == '.')
			retSzFloat = retSzFloat.substring(0, retSzFloat.length() - 1);
		if (retSzFloat.matches("^(-?\\d+)(\\.\\d+)?$")) {
			return retSzFloat;
		}
		return "";
	}

	private static int FNVHash1(String data) {
		final int p = 16777619;
		int hash = (int) 2166136261L;
		for (int i = 0; i < data.length(); i++)
			hash = (hash ^ data.charAt(i)) * p;
		hash += hash << 13;
		hash ^= hash >> 7;
		hash += hash << 3;
		hash ^= hash >> 17;
		hash += hash << 5;
		return hash;
	}

	public static long getHashCode(String data) {
		long hash = data.hashCode();
		long hash1 = hash << 32;
		int hash2 = FNVHash1(data);
		hash2 = Math.abs(hash2);
		long hash3 = hash1 | hash2;
		return hash3;
	}

	public static void logInfo(String msg) {
		System.out.println("util.loginfo: " + msg);
		log.info(msg);
	}

	public static void logError(Exception e) {
		if (e == null) {
			System.out.println("unknown error");
			log.error("unknown error");
			return;
		}
		try {
			StackTraceElement[] s = e.getStackTrace();
			StringBuffer em = new StringBuffer(e.getMessage());
			if (s != null) {
				for (int i = 0; i < s.length; i++) {
					StackTraceElement st = s[i];
					em.append("\t\t").append(st.toString()).append("\r\n");
				}
			}
			System.out.println(em.toString());
			log.error(em.toString());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static String map2PackageStr(HashMap<String, String> map)
			throws Exception {
		StringBuilder result = new StringBuilder();
		for (Map.Entry<String, String> m : map.entrySet()) {
			String key = m.getKey();
			String value = m.getValue();
			String ekey = encoder(key);
			String evalue = encoder(value);
			String add = ekey + ":" + evalue;
			if (result.length() > 0)
				result.append("," + add);
			else
				result.append(add);
		}
		return result.toString();
	}

	public static String encoder(String vstr) throws Exception {
		if (vstr == null)
			return "";
		String str = vstr.trim();
		if (str.trim().length() < 1)
			return "";
		String result = (new sun.misc.BASE64Encoder()).encode(str
				.getBytes("UTF-8"));
		result = result.replace("+", "_");
		result = result.replace("=", ".");
		return result;
	}

	public static String decoder(String vstr) throws Exception {
		if (vstr == null)
			return "";
		String str = vstr.trim();
		if (str.length() < 1)
			return "";
		str = str.replace("_", "+");
		str = str.replace(".", "=");
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] b = decoder.decodeBuffer(str);
		return new String(b, "UTF-8");
	}

	public static HashMap<String, String> packageStr2map(String vstr)
			throws Exception {
		HashMap<String, String> map = new HashMap<String, String>();
		if (vstr == null || vstr.trim().length() < 1)
			return map;
		String[] valueList = vstr.trim().split(",");
		for (int i = 0; i < valueList.length; i++) {
			String value = valueList[i].trim();
			if (value.length() > 0) {
				String[] values = value.split(":");
				if (values.length == 2) {
					map.put(decoder(values[0]), decoder(values[1]));
				} else if (values.length == 1) {
					map.put(decoder(values[0]), "");
				}
			}
		}
		return map;
	}

	public static String eraseStringTag(String str) {
		String contentHtml = "";
		String[] subContents = str.split(",");
		for (int t = 0; t < subContents.length; t++) {
			String tempStr = subContents[t];
			contentHtml += tempStr;
		}
		return contentHtml;
	}

	public static String eraseHtmlContent(String str) {
		String contentHtml = "";
		str = str.replace(":", ":").replace("<BR>", "BEEND");
		System.out.println(str);
		str = eraseHtmlTags(str).replace("BEEND", "<BR>");
		System.out.println(str);
		String[] subContents = str.split(":");
		for (int t = 0; t < subContents.length; t++) {
			String tempStr = subContents[t];
			if (tempStr.indexOf("<BR>") > -1) {
				while (tempStr.indexOf("<BR>") != tempStr.lastIndexOf("<BR>")) {
					tempStr = tempStr.substring(0, tempStr.indexOf("<BR>"))
							+ tempStr.substring(tempStr.indexOf("<BR>") + 4);
				}
			}
			contentHtml += tempStr;
			if (t < subContents.length - 1) {
				contentHtml += ":";
			}
		}
		return contentHtml;
	}

	public static String eraseHtmlScript(String str, String[] regs) {
		for (int t = 0; t < regs.length; t++) {
			String beginStr = "<" + regs[t];
			String endStr = "</" + regs[t] + ">";
			if (str.indexOf(beginStr) > -1 && str.indexOf(endStr) > -1) {
				str = str.substring(0, str.indexOf(beginStr))
						+ str.substring(str.indexOf(endStr)
								+ (regs[t].length() + 3));
				while (str.indexOf(beginStr) != str.lastIndexOf(beginStr)) {
					str = str.substring(0, str.indexOf(beginStr))
							+ str.substring(str.indexOf(endStr)
									+ (regs[t].length() + 3));
				}
			}
		}
		return eraseHtmlTags(str);
	}

	/***
	 * 发送异常邮件
	 * 
	 * @param owner
	 *            异常产生地---类或脚本
	 * @param content
	 *            异常内容
	 * @param ip
	 *            异常产生ip
	 * @param description
	 *            异常附件描述信息
	 */
	public static void sendErrorMail(String owner, Exception ex,
			String description) {

		String date = sdf.format(new Date());
		String content = getExceptionInfo(ex);
		String message = "";
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress();
			message = "<html><body><b>产生异常的类或脚本:</b><br>" + owner
					+ "<br><br><b>异常内容:</b><br>" + content
					+ "<br><br><b>异常描述:</b><br>" + description
					+ "<br><br><b>产生异常的IP地址:</b><br>" + ip
					+ "<br><br><b>异常产生时间:</b><br>" + date + "</body></html>";
			message = URLEncoder.encode(encoder(message), "UTF-8");

			if (null == emailAddress) {
				getEmailAddress();
			}
			log.error("emailAddress:" + emailAddress);
			if (null != emailAddress) {
				HttpUtils.postUrlContent(emailAddress,
						"title=CRAWLER ERROR&message=" + message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void getEmailAddress() {
		if (null == emailAddress) {
			File file = new File("./log4j.properties");
			if (!file.exists() && !file.isFile()) {
				file = new File("./conf/log4j.properties");
			}
			InputStream in = null;
			try {
				in = new FileInputStream(file);
				Properties p = new Properties();
				p.load(in);
				emailAddress = p.getProperty("email.address");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (null != in) {
						in.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static String getExceptionInfo(Exception e) {
		if (e == null) {
			System.out.println("unknown error");
			return "unknown error";
		}
		try {
			StackTraceElement[] s = e.getStackTrace();
			StringBuffer em = new StringBuffer(e.getMessage());
			if (s != null) {
				for (int i = 0; i < s.length; i++) {
					StackTraceElement st = s[i];
					em.append("<br>").append(st.toString()).append("<br>");
				}
			}
			return em.toString();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return "=======error=======";
	}
	public static void main(String[] args) {

		String str = "<aa>dddd<!--this is aldo test!!!!--><ccc>bbb<rrr>tt<yy>";
		System.out.println(eraseHtmlScript3(str));
	}
	public static String eraseHTMLTags(String inStr) {
		if (inStr == null)
			return "";
		inStr = Util.eraseHtmlTags(inStr);
		inStr = inStr.replaceAll("&#146;", "'").replaceAll("&nbsp;", " ");
		inStr = inStr.replaceAll("&#153;", "").replaceAll("&#8482;", "")
				.replaceAll("&#174;", "").replaceAll("&#179;", "").replaceAll(
						"&reg;", "").replaceAll("&trade;", "");
		inStr = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(inStr)
				.trim();
		return inStr;
	}



	public static String eraseHtmlScript3(String str) {

		String beginStr = "<!--";
		String endStr = "-->";
		if (str.indexOf(beginStr) > -1 && str.indexOf(endStr) > -1) {
			str = str.substring(0, str.indexOf(beginStr))
					+ str.substring(str.indexOf(endStr) + 3);
			while (str.indexOf(beginStr) != str.lastIndexOf(beginStr)) {
				str = str.substring(0, str.indexOf(beginStr))
						+ str.substring(str.indexOf(endStr) + 3);
			}
		}

		return eraseHtmlScript3(str);
	}
}

 

分享到:
评论

相关推荐

    实验5 JAVA常用类.doc

    实验5的Java常用类主要涵盖了Java编程中的一些核心概念和常用工具类的使用。以下是这些知识点的详细说明: 1. **String、StringBuffer(StringBuilder)**: - **String** 类在Java中是不可变的,这意味着一旦创建了...

    java常用工具类集合(也有转自他人的)

    总结来说,"java常用工具类集合"是一个涵盖了多种实用工具类的资源,包括但不限于字符串处理、集合操作、日期时间、IO操作等多个方面。通过学习和应用这些工具类,开发者能够更高效地编写代码,同时也能从开源社区中...

    Java常用类总结

    总的来说,熟练掌握这些Java常用类对于提升编程效率和编写高质量代码至关重要。它们涵盖了数据存储、输入输出、并发控制、日期时间处理等多个方面,是Java程序员的基础技能。了解并精通这些类的使用,能帮助开发者更...

    java 常用工具类

    Java 常用工具类是Java开发中不可或缺的一部分,它们为开发者提供了许多便捷的功能,减少了重复的代码编写,提高了开发效率。"哈哈钱包工具"很可能是一个自定义的工具类库,它结合了多种编程概念,如设计模式、数据...

    (转)java 常用工具包

    标题中的"(转)java 常用工具包"可能是指一系列在Java开发中常用的库或者框架的集合。虽然描述中没有提供具体信息,但我们可以从标签“源码”和“工具”推测,这个话题可能涉及到对Java工具包的源码分析或讨论,...

    JasyptTool_java解密小工具_

    总结来说,"JasyptTool_java解密小工具_"是基于Java和Jasypt库开发的一个实用工具,旨在简化加密和解密任务,提高数据安全性。通过理解和使用这个工具,开发者可以更好地保护敏感信息,提升应用的安全防护能力。

    java常用工具类——个人总结

    以下是我个人对Java中常用工具类的总结,主要涉及了加密、文件上传和日期处理等核心领域。 1. **加密工具类**: - `java.security` 包下的 `MessageDigest` 类用于实现消息摘要算法,如MD5和SHA,常用于数据完整性...

    Java 程序员开发常用的工具.docx

    一些其他常用工具 - **Git** - **简介**:Git 是一个分布式版本控制系统,广泛应用于软件开发项目中。 - **特点**: - 分布式架构,支持离线工作。 - 提供了分支管理和合并功能。 - **适用场景**:适用于所有...

    java开发,30个常用工具类总结,全部来自项目实践中用到的工具类

    以下是对标题和描述中提到的几个常用工具类的详细说明: 1. **DateUtils**: `java.util.Date` 和 `java.time` 包含日期和时间的操作,但DateUtils通常是Apache Commons Lang库中的一个工具类,提供更方便的日期处理...

    编写java代码常用的工具代码

    总结了编写java代码常用的算法代码,如ucs2,ascii,进制转换,以及APN相关的管理代码

    java开发常用单词总结

    ### Java开发常用单词总结 #### 一、Java基础词汇 - **JDK(Java Development Kit)**:Java开发工具包,包含编译、运行Java程序所需的所有工具。 - **JVM(Java Virtual Machine)**:Java虚拟机,是执行Java字节码的...

    java utils 常用工具类 - 十年工作经验总结

    十年工作经验总结的java 常用工具类分享大家-ArrayUtil,CharsetUtil,CharUtil,CheckUtil,ChinesUtil,ClassUtil,ConfigUtil,ConvertUtil,DateUtil,EmailUtil,ExceptionUtil,FilePathUtil,FileUtil,...

    分享一些java常用的工具类(源码) 非常实用

    java开发工作多年总结的的一些工具类; 包括有: 1、常用的日期类操作 2、excel操作 3、xml操作 4、集成科大讯飞语言合成工具类; 5、集合类操作(list,map,array) 6、FTP工具类 7、csv操作 8、file文件类操作 9...

    Java常用架包

    总结起来,这个Java常用架包提供了处理XML文档的工具(dom4j)、实现了浏览器与服务器端直接通信的DWR框架,以及连接MySQL和Oracle数据库的JDBC驱动。这些组件在Java开发中有着广泛的应用,是构建企业级Web应用不可...

    java常用工具GitChat1

    - **标题**: “Java常用工具GitChat1” - **描述**: “Java常用工具GitChat1” 这两个部分的信息相对简单且重复,主要传达了一个概念:这是一个关于Java开发中常用工具介绍的内容。考虑到“GitChat1”可能是指一个...

    个人总结常用经典Utils工具类

    1、Utils工具类有String工具类、XmlNode节点工具类、BeanFactory相关、Common工具类、Cookie工具类、Date工具类、Http工具类、JDBC工具类、日志Log工具类、Servlet相关等。2、工具类省了重复造轮子的工作,可以直接...

    java常用API总结

    Java 常用 API 是开发过程中不可或缺的一部分,涵盖了多种核心概念和工具。下面将逐一解析这些知识点: 1. **Java 面向对象基本概念**: - **类(class)**: 类是创建对象的蓝图,包含数据成员(变量)和行为成员...

    java中常用设计模式总结心得

    以下是对Java中常用设计模式的详细解释: 1. **单例模式**:单例模式确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于配置管理、线程池或者数据库连接池等场景。实现方式有懒汉式(线程不安全)、...

    Java常用开源框架总结.docx

    Java 开源框架是开发者在构建应用程序时常用的工具,它们提供了许多功能,可以帮助简化开发过程,提高效率。Apache Commons 是一个著名的 Java 开源项目,由多个模块组成,提供了大量的实用工具类和组件。以下是对 ...

    java常用函数.doc

    本篇将深入讲解Java中涉及字符串、包装类、日期处理、基础数学操作以及其他实用工具类的相关函数,以及GUI组件如文本框、文本区、按钮和标签的使用。 首先,我们关注字符串处理。Java中的`String`类提供了丰富的...

Global site tag (gtag.js) - Google Analytics