Commit 07259995 authored by AlanGao's avatar AlanGao
Browse files

update

parent 458409b4
package com.jsh.util; package com.jsh.util;
import com.jsh.base.Log;
import java.io.File; import java.io.File;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import com.jsh.base.Log;
/** /**
* 获取应用系统路径 * 获取应用系统路径
* @author jishenghua *
* @author jishenghua
* @qq 7 5 2 7 1 8 9 2 0 * @qq 7 5 2 7 1 8 9 2 0
*/ */
public class PathTool public class PathTool {
{
/** /**
* 获取WEB-INF的绝对路径 * 获取WEB-INF的绝对路径
* @return *
* @return
*/ */
public static String getWebinfPath() public static String getWebinfPath() {
{
String webinfPath = ""; String webinfPath = "";
//获取URL对象 //获取URL对象
URL url = PathTool.class.getClassLoader().getResource(""); URL url = PathTool.class.getClassLoader().getResource("");
try try {
{
//获取路径 //获取路径
webinfPath = url.toURI().getPath(); webinfPath = url.toURI().getPath();
//截取路径到WEB-INF结束 //截取路径到WEB-INF结束
// webinfPath = path.substring(0, path.indexOf("/WEB-INF") + 8); // webinfPath = path.substring(0, path.indexOf("/WEB-INF") + 8);
} } catch (URISyntaxException e) {
catch (URISyntaxException e) Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e);
{
Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e);
} }
return webinfPath; return webinfPath;
} }
/** /**
* 获取webapp的绝对路径 * 获取webapp的绝对路径
* @return *
* @return
*/ */
public static String getWebappPath() public static String getWebappPath() {
{
//先获取工程路径 //先获取工程路径
String projectPath = getProjectPath(); String projectPath = getProjectPath();
//获取工程路径的上级路径 //获取工程路径的上级路径
File f = new File(projectPath); File f = new File(projectPath);
//路径不存在就返回 //路径不存在就返回
if (!f.exists()) if (!f.exists()) {
{
return projectPath; return projectPath;
} } else {
else
{
//返回webapp路径 //返回webapp路径
return f.getParent(); return f.getParent();
} }
} }
/** /**
* 获取工程的绝对路径 * 获取工程的绝对路径
* @return *
* @return
*/ */
public static String getProjectPath() public static String getProjectPath() {
{
String projectPath = ""; String projectPath = "";
//获取URL对象 //获取URL对象
URL url = PathTool.class.getClassLoader().getResource(""); URL url = PathTool.class.getClassLoader().getResource("");
String path = null; String path = null;
try try {
{
//获取路径 //获取路径
path = url.toURI().getPath(); path = url.toURI().getPath();
//截取webapp路径 //截取webapp路径
projectPath = path.substring(0, path.indexOf("/WEB-INF")); projectPath = path.substring(0, path.indexOf("/WEB-INF"));
} } catch (URISyntaxException e) {
catch (URISyntaxException e) Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e);
{
Log.errorFileSync(">>>>>>>>>>>>>>>>>>>>>>>路径获取异常", e);
} }
return projectPath; return projectPath;
} }
......
...@@ -6,61 +6,58 @@ import java.util.Set; ...@@ -6,61 +6,58 @@ import java.util.Set;
/** /**
* 根据搜索条件拼装成查询hql语句 * 根据搜索条件拼装成查询hql语句
*
* @author jishenghua qq:752718920 * @author jishenghua qq:752718920
*/ */
public class SearchConditionUtil public class SearchConditionUtil {
{
//拼接字符串的前缀空格字符串 //拼接字符串的前缀空格字符串
private static final String emptyPrefix = " and "; private static final String emptyPrefix = " and ";
/** /**
* 根据搜索条件自动拼接成hql搜索语句 * 根据搜索条件自动拼接成hql搜索语句
*
* @param condition 搜索条件 规则: * @param condition 搜索条件 规则:
* 1、类型 n--数字 s--字符串 * 1、类型 n--数字 s--字符串
* 2、属性 eq--等于 neq--不等于 like--像'%XX%' llike--左像'%XX' rlike--右像'XX%' in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于 * 2、属性 eq--等于 neq--不等于 like--像'%XX%' llike--左像'%XX' rlike--右像'XX%' in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于
* order--value desc asc gy-- group by * order--value desc asc gy-- group by
* 示例: * 示例:
* Map<String,Object> condition = new HashMap<String,Object>(); * Map<String,Object> condition = new HashMap<String,Object>();
* condition.put("supplier_s_like", "aaa"); * condition.put("supplier_s_like", "aaa");
* condition.put("contacts_s_llike", "186"); * condition.put("contacts_s_llike", "186");
* condition.put("contacts_s_rlike", "186"); * condition.put("contacts_s_rlike", "186");
* condition.put("phonenum_s_eq", null); * condition.put("phonenum_s_eq", null);
* condition.put("email_n_neq", 23); * condition.put("email_n_neq", 23);
* condition.put("description_s_order", "desc"); * condition.put("description_s_order", "desc");
* @return 封装后的字符串 * @return 封装后的字符串
*/ */
public static String getCondition(Map<String,Object> condition) public static String getCondition(Map<String, Object> condition) {
{
StringBuffer hql = new StringBuffer(); StringBuffer hql = new StringBuffer();
Set<String> key = condition.keySet(); Set<String> key = condition.keySet();
String groupbyInfo = ""; String groupbyInfo = "";
String orderInfo = ""; String orderInfo = "";
for(String keyInfo:key) for (String keyInfo : key) {
{
/* /*
* 1、数组为三个 第一个为对象实例的字段 第二个为字段类型 第三个为属性 * 1、数组为三个 第一个为对象实例的字段 第二个为字段类型 第三个为属性
* 2、根据分解后的数组拼接搜索条件 * 2、根据分解后的数组拼接搜索条件
*/ */
Object valueInfo = condition.get(keyInfo); Object valueInfo = condition.get(keyInfo);
if(null != valueInfo &&valueInfo.toString().length()>0) if (null != valueInfo && valueInfo.toString().length() > 0) {
{
String[] searchCondition = keyInfo.split("_"); String[] searchCondition = keyInfo.split("_");
if(searchCondition[1].equals("n")) if (searchCondition[1].equals("n"))
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + valueInfo); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + valueInfo);
else if(searchCondition[1].equals("s")) else if (searchCondition[1].equals("s")) {
{ if (searchCondition[2].equals("like"))
if(searchCondition[2].equals("like"))
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "%'"); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "%'");
else if(searchCondition[2].equals("llike")) else if (searchCondition[2].equals("llike"))
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "'"); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'%" + valueInfo + "'");
else if(searchCondition[2].equals("rlike")) else if (searchCondition[2].equals("rlike"))
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "%'"); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "%'");
else if(searchCondition[2].equals("in")) else if (searchCondition[2].equals("in"))
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "(" + valueInfo + ")"); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "(" + valueInfo + ")");
else if(searchCondition[2].equals("order")) else if (searchCondition[2].equals("order"))
orderInfo = " order by " + searchCondition[0] + " " + valueInfo; orderInfo = " order by " + searchCondition[0] + " " + valueInfo;
else if(searchCondition[2].equals("gb")) else if (searchCondition[2].equals("gb"))
groupbyInfo = " group by " + searchCondition[0]; groupbyInfo = " group by " + searchCondition[0];
else else
hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "'"); hql.append(emptyPrefix + searchCondition[0] + getType(searchCondition[2]) + "'" + valueInfo + "'");
} }
...@@ -68,51 +65,50 @@ public class SearchConditionUtil ...@@ -68,51 +65,50 @@ public class SearchConditionUtil
} }
return hql.append(groupbyInfo).append(orderInfo).toString(); return hql.append(groupbyInfo).append(orderInfo).toString();
} }
/** /**
* 获取指定类型的符号 * 获取指定类型的符号
* 属性 eq--等于 neq--不等于 like--像 in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于 order--value desc asc * 属性 eq--等于 neq--不等于 like--像 in--包含 gt--大于 gteq--大于等于 lt--小于 lteq--小于等于 order--value desc asc
*
* @param type * @param type
* @return 类型字符串 * @return 类型字符串
*/ */
private static String getType(String type) private static String getType(String type) {
{
String typeStr = ""; String typeStr = "";
if(type.equals("eq")) if (type.equals("eq"))
typeStr = " = "; typeStr = " = ";
else if(type.equals("neq")) else if (type.equals("neq"))
typeStr = " != "; typeStr = " != ";
else if(type.equals("like")) else if (type.equals("like"))
typeStr = " like ";
else if (type.equals("llike"))
typeStr = " like "; typeStr = " like ";
else if(type.equals("llike")) else if (type.equals("rlike"))
typeStr = " like "; typeStr = " like ";
else if(type.equals("rlike")) else if (type.equals("in"))
typeStr = " like "; typeStr = " in ";
else if(type.equals("in")) else if (type.equals("gt"))
typeStr = " in ";
else if(type.equals("gt"))
typeStr = " > "; typeStr = " > ";
else if(type.equals("gteq")) else if (type.equals("gteq"))
typeStr = " >= "; typeStr = " >= ";
else if(type.equals("lt")) else if (type.equals("lt"))
typeStr = " < "; typeStr = " < ";
else if(type.equals("lteq")) else if (type.equals("lteq"))
typeStr = " <= "; typeStr = " <= ";
else if(type.equals("order")) else if (type.equals("order"))
typeStr = " order "; typeStr = " order ";
else if(type.equals("gy")) else if (type.equals("gy"))
typeStr = " group by "; typeStr = " group by ";
else else
typeStr = "unknown"; typeStr = "unknown";
return typeStr; return typeStr;
} }
public static void main(String[] args) public static void main(String[] args) {
{
/** /**
* 拼接搜索条件 * 拼接搜索条件
*/ */
Map<String,Object> condition = new HashMap<String,Object>(); Map<String, Object> condition = new HashMap<String, Object>();
condition.put("supplier_s_like", "aaa"); condition.put("supplier_s_like", "aaa");
condition.put("contacts_s_llike", "186"); condition.put("contacts_s_llike", "186");
condition.put("contacts_s_rlike", "186"); condition.put("contacts_s_rlike", "186");
...@@ -120,7 +116,7 @@ public class SearchConditionUtil ...@@ -120,7 +116,7 @@ public class SearchConditionUtil
condition.put("email_n_neq", 23); condition.put("email_n_neq", 23);
condition.put("description_s_order", "desc"); condition.put("description_s_order", "desc");
condition.put("description_s_gb", "aaa"); condition.put("description_s_gb", "aaa");
//获取搜索条件拼接 //获取搜索条件拼接
System.out.println(getCondition(condition)); System.out.println(getCondition(condition));
} }
......
package com.jsh.util; package com.jsh.util;
import java.io.IOException; import javax.servlet.*;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.io.IOException;
/** /**
* 用户登录session处理类 * 用户登录session处理类
* 过滤session是否超时 * 过滤session是否超时
* @author jishenghua qq_752718920 *
* @version [版本号, 2012-3-6] * @author jishenghua qq_752718920
* @see [相关类/方法] * @version [版本号, 2012-3-6]
* @since * @see [相关类/方法]
* @since
*/ */
public class SessionFilter implements Filter public class SessionFilter implements Filter {
{
/** /**
* 初始化过滤器 暂不处理 * 初始化过滤器 暂不处理
* 重载方法 * 重载方法
*
* @param arg0 * @param arg0
* @throws ServletException * @throws ServletException
*/ */
public void init(FilterConfig arg0) public void init(FilterConfig arg0)
throws ServletException throws ServletException {
{
} }
/** /**
* 判断用户session是否存在 不存在则跳转到登录页面 * 判断用户session是否存在 不存在则跳转到登录页面
* 重载方法 * 重载方法
*
* @param srequest * @param srequest
* @param sresponse * @param sresponse
* @param chain * @param chain
...@@ -44,27 +40,25 @@ public class SessionFilter implements Filter ...@@ -44,27 +40,25 @@ public class SessionFilter implements Filter
* @throws ServletException * @throws ServletException
*/ */
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain) public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain)
throws IOException, ServletException throws IOException, ServletException {
{
HttpServletRequest request = (HttpServletRequest) srequest; HttpServletRequest request = (HttpServletRequest) srequest;
HttpServletResponse response = (HttpServletResponse) sresponse; HttpServletResponse response = (HttpServletResponse) sresponse;
HttpSession session = request.getSession(); HttpSession session = request.getSession();
//获取工程路径 //获取工程路径
String path = request.getContextPath(); String path = request.getContextPath();
String requestURl = request.getRequestURI(); String requestURl = request.getRequestURI();
if(requestURl.contains("/pages") &&null != session.getAttribute("user")) if (requestURl.contains("/pages") && null != session.getAttribute("user"))
chain.doFilter(request, response); chain.doFilter(request, response);
else else
response.sendRedirect(path + "/logout.jsp"); response.sendRedirect(path + "/logout.jsp");
} }
/** /**
* 销毁过滤器 * 销毁过滤器
*/ */
public void destroy() public void destroy() {
{
} }
} }
...@@ -2,25 +2,25 @@ package com.jsh.util; ...@@ -2,25 +2,25 @@ package com.jsh.util;
/** /**
* 定义供应商、客户管理常量 * 定义供应商、客户管理常量
*
* @author jishenghua * @author jishenghua
*/ */
public interface SupplierConstants public interface SupplierConstants {
{
/** /**
* 公共常量 * 公共常量
*
* @author jishenghua * @author jishenghua
*/ */
public class Common public class Common {
{
} }
/** /**
* 常量--导入导出excel表格业务相关 * 常量--导入导出excel表格业务相关
*
* @author jishenghua * @author jishenghua
*/ */
public class BusinessForExcel public class BusinessForExcel {
{
/** /**
* 名称 * 名称
*/ */
......
...@@ -3,6 +3,7 @@ package com.jsh.util; ...@@ -3,6 +3,7 @@ package com.jsh.util;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
...@@ -11,566 +12,545 @@ import java.security.MessageDigest; ...@@ -11,566 +12,545 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.math.BigInteger;
/** /**
* 工具类 * 工具类
*
* @author jishenghua qq:7-5-2-7-1-8-9-2-0 * @author jishenghua qq:7-5-2-7-1-8-9-2-0
*/ */
public class Tools public class Tools {
{ /**
/**
* 获得32位唯一序列号 * 获得32位唯一序列号
*
* @return 32为ID字符串 * @return 32为ID字符串
*/ */
public static String getUUID_32() public static String getUUID_32() {
{ return UUID.randomUUID().toString().replaceAll("-", "");
return UUID.randomUUID().toString().replaceAll("-", "");
} }
/** /**
* 获得当天时间,格式为yyyy-MM-dd * 获得当天时间,格式为yyyy-MM-dd
*
* @return 格式化后的日期格式 * @return 格式化后的日期格式
*/ */
public static String getNow() public static String getNow() {
{ return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
} }
/** /**
* 获取当前月 yyyy-MM * 获取当前月 yyyy-MM
*
* @return * @return
*/ */
public static String getCurrentMonth() public static String getCurrentMonth() {
{ return new SimpleDateFormat("yyyy-MM").format(new Date());
return new SimpleDateFormat("yyyy-MM").format(new Date());
} }
/** /**
* 获取指定日期格式 yyyy-MM-dd * 获取指定日期格式 yyyy-MM-dd
*
* @return * @return
*/ */
public static String getCurrentMonth(Date date) public static String getCurrentMonth(Date date) {
{
return new SimpleDateFormat("yyyy-MM-dd").format(date); return new SimpleDateFormat("yyyy-MM-dd").format(date);
} }
/** /**
* 获得当天时间,格式为yyyyMMddHHmmss * 获得当天时间,格式为yyyyMMddHHmmss
*
* @return 格式化后的日期格式 * @return 格式化后的日期格式
*/ */
public static String getNow2(Date date) public static String getNow2(Date date) {
{ return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
} }
/** /**
* 获得当天时间,格式为yyyy-MM-dd HH:mm:ss * 获得当天时间,格式为yyyy-MM-dd HH:mm:ss
*
* @return 格式化后的日期格式 * @return 格式化后的日期格式
*/ */
public static String getNow3() public static String getNow3() {
{ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
} }
/** /**
* 获得指定时间,格式为yyyy-MM-dd HH:mm:ss * 获得指定时间,格式为yyyy-MM-dd HH:mm:ss
*
* @return 格式化后的日期格式 * @return 格式化后的日期格式
*/ */
public static String getCenternTime(Date date) public static String getCenternTime(Date date) {
{ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
} }
/** /**
* 获得指定时间,格式为mm:ss * 获得指定时间,格式为mm:ss
*
* @return 格式化后的日期格式 * @return 格式化后的日期格式
*/ */
public static String getTimeInfo(Date date) public static String getTimeInfo(Date date) {
{ return new SimpleDateFormat("mm:ss").format(date);
return new SimpleDateFormat("mm:ss").format(date);
} }
/** /**
* 获取当前日期是星期几 * 获取当前日期是星期几
* return 星期几 * return 星期几
*/ */
public static String getWeekDay() public static String getWeekDay() {
{ Calendar c = Calendar.getInstance(Locale.CHINA);
Calendar c = Calendar.getInstance(Locale.CHINA); c.setTime(new Date());
c.setTime(new Date()); int day = c.get(Calendar.DAY_OF_WEEK);
int day=c.get(Calendar.DAY_OF_WEEK);
String weekDay = ""; String weekDay = "";
switch (day) switch (day) {
{ case 1:
case 1: weekDay = "星期日";
weekDay = "星期日"; break;
break; case 2:
case 2: weekDay = "星期一";
weekDay = "星期一"; break;
break; case 3:
case 3: weekDay = "星期二";
weekDay = "星期二"; break;
break; case 4:
case 4: weekDay = "星期三";
weekDay = "星期三"; break;
break; case 5:
case 5: weekDay = "星期四";
weekDay = "星期四"; break;
break; case 6:
case 6: weekDay = "星期五";
weekDay = "星期五"; break;
break; case 7:
case 7: weekDay = "星期六";
weekDay = "星期六"; break;
break; default:
default: break;
break; }
}
return weekDay; return weekDay;
} }
/** /**
* 判断字符串是否全部为数字 * 判断字符串是否全部为数字
*
* @param accountWaste * @param accountWaste
* @return boolean值 * @return boolean值
*/ */
public static boolean checkStrIsNum(String checkStr) public static boolean checkStrIsNum(String checkStr) {
{ if (checkStr == null || checkStr.length() == 0)
if(checkStr == null || checkStr.length() ==0) return false;
return false; return Pattern.compile("^[0-9]*.{1}[0-9]*$").matcher(checkStr).matches();
return Pattern.compile("^[0-9]*.{1}[0-9]*$").matcher(checkStr).matches();
// return Pattern.compile(":^[0-9]+(.[0-9])*$").matcher(checkStr).matches(); // return Pattern.compile(":^[0-9]+(.[0-9])*$").matcher(checkStr).matches();
} }
/** /**
* 获得前一天的时间 * 获得前一天的时间
*
* @return 前一天日期 * @return 前一天日期
*/ */
public static String getPreviousDate() public static String getPreviousDate() {
{ Calendar cal = Calendar.getInstance();
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1);
cal.add(Calendar.DATE, -1); return new SimpleDateFormat("yyyy-MM").format(cal.getTime());
return new SimpleDateFormat("yyyy-MM").format(cal.getTime()); }
}
/**
/** * 截取字符串长度
* 截取字符串长度 *
* @param beforeStr * @param beforeStr
* @param cutLeng * @param cutLeng
* @return 截取后的字符串 * @return 截取后的字符串
*/ */
public static String subStr(String beforeStr,int cutLeng){ public static String subStr(String beforeStr, int cutLeng) {
if(beforeStr.length()>cutLeng) if (beforeStr.length() > cutLeng)
return beforeStr.substring(0,cutLeng)+ "..." ; return beforeStr.substring(0, cutLeng) + "...";
return beforeStr ; return beforeStr;
} }
/**
* 生成随机字符串,字母和数字混合 /**
* @return 组合后的字符串 ^[0-9a-zA-Z] * 生成随机字符串,字母和数字混合
*/ *
public static String getRandomChar(){ * @return 组合后的字符串 ^[0-9a-zA-Z]
//生成一个0、1、2的随机数字 */
int rand = (int)Math.round(Math.random() * 1); public static String getRandomChar() {
long itmp = 0; //生成一个0、1、2的随机数字
char ctmp = '\u0000'; int rand = (int) Math.round(Math.random() * 1);
switch (rand) long itmp = 0;
{ char ctmp = '\u0000';
//生成大写字母 + 1000以内数字 switch (rand) {
case 1: //生成大写字母 + 1000以内数字
itmp = Math.round(Math.random() * 25 + 65); case 1:
ctmp = (char)itmp; itmp = Math.round(Math.random() * 25 + 65);
return String.valueOf(ctmp) + (int)Math.random()*1000; ctmp = (char) itmp;
//生成小写字母 return String.valueOf(ctmp) + (int) Math.random() * 1000;
case 2: //生成小写字母
itmp = Math.round(Math.random() * 25 + 97); case 2:
ctmp = (char)itmp; itmp = Math.round(Math.random() * 25 + 97);
return String.valueOf(ctmp)+ (int)Math.random()*1000; ctmp = (char) itmp;
//生成数字 return String.valueOf(ctmp) + (int) Math.random() * 1000;
default : //生成数字
itmp = Math.round(Math.random() * 1000); default:
return itmp + ""; itmp = Math.round(Math.random() * 1000);
} return itmp + "";
} }
}
/**
* 判断首字母以数字开头,字符串包括数字、字母%以及空格 /**
* @param str 检查字符串 * 判断首字母以数字开头,字符串包括数字、字母%以及空格
* @return 是否以数字开头 *
*/ * @param str 检查字符串
public static boolean CheckIsStartWithNum(String str) * @return 是否以数字开头
{ */
return Pattern.compile("^[0-9][a-zA-Z0-9%,\\s]*$").matcher(str).matches(); public static boolean CheckIsStartWithNum(String str) {
} return Pattern.compile("^[0-9][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
/** }
* 判断首字母以","开头,字符串包括数字、字母%以及空格
* @param str 检查字符串 /**
* @return 是否以数字开头 * 判断首字母以","开头,字符串包括数字、字母%以及空格
*/ *
public static boolean CheckIsStartWithSpec(String str) * @param str 检查字符串
{ * @return 是否以数字开头
return Pattern.compile("^[,][a-zA-Z0-9%,\\s]*$").matcher(str).matches(); */
} public static boolean CheckIsStartWithSpec(String str) {
/** return Pattern.compile("^[,][a-zA-Z0-9%,\\s]*$").matcher(str).matches();
* 字符转码 }
* @param aValue
* @return /**
* @see 转码后的字符串 * 字符转码
*/ *
public static String encodeValue(String aValue) * @param aValue
{ * @return
if(aValue.trim().length() ==0) * @see 转码后的字符串
{ */
public static String encodeValue(String aValue) {
if (aValue.trim().length() == 0) {
return ""; return "";
} }
String valueAfterTransCode = null; String valueAfterTransCode = null;
try try {
{
valueAfterTransCode = URLEncoder.encode(aValue, "UTF-8"); valueAfterTransCode = URLEncoder.encode(aValue, "UTF-8");
} } catch (UnsupportedEncodingException e) {
catch (UnsupportedEncodingException e)
{
e.getMessage(); e.getMessage();
} }
return valueAfterTransCode; return valueAfterTransCode;
} }
/** /**
* 字符转码 * 字符转码
* @param aValue *
* @return * @param aValue
* @see 转码后的字符串 * @return
*/ * @see 转码后的字符串
public static String decodeValue(String aValue) */
{ public static String decodeValue(String aValue) {
if(aValue.trim().length() ==0) if (aValue.trim().length() == 0) {
{ return "";
return ""; }
} String valueAfterTransCode = null;
String valueAfterTransCode = null; try {
try valueAfterTransCode = URLDecoder.decode(aValue, "UTF-8");
{ } catch (UnsupportedEncodingException e) {
valueAfterTransCode = URLDecoder.decode(aValue, "UTF-8"); e.getMessage();
} }
catch (UnsupportedEncodingException e) return valueAfterTransCode;
{ }
e.getMessage();
} /**
return valueAfterTransCode; * 去除str中的'
} *
* @param str
/** * @return 除去'后的字符串
* 去除str中的' * @see [类、类#方法、类#成员]
* @param str */
* @return 除去'后的字符串 public static String afterDealStr(String str) {
* @see [类、类#方法、类#成员] return str.replace("'", "");
*/ }
public static String afterDealStr(String str)
{ /**
return str.replace("'", ""); * 获取用户IP地址(停用)
} *
* @return 用户IP
/** * @see [类、类#方法、类#成员]
* 获取用户IP地址(停用) */
* @return 用户IP public static String getCurrentUserIP() {
* @see [类、类#方法、类#成员] try {
*/ return InetAddress.getLocalHost().getHostAddress();
public static String getCurrentUserIP() } catch (UnknownHostException e) {
{ e.printStackTrace();
try return "127.0.0.1";
{ }
return InetAddress.getLocalHost().getHostAddress(); }
}
catch (UnknownHostException e) /**
{ * 从Request对象中获得客户端IP,处理了HTTP代理服务器和Nginx的反向代理截取了ip
e.printStackTrace(); *
return "127.0.0.1"; * @param request
} * @return ip
} */
public static String getLocalIp(HttpServletRequest request) {
/** String remoteAddr = request.getRemoteAddr();
* 从Request对象中获得客户端IP,处理了HTTP代理服务器和Nginx的反向代理截取了ip String forwarded = request.getHeader("X-Forwarded-For");
* @param request String realIp = request.getHeader("X-Real-IP");
* @return ip
*/ String ip = null;
public static String getLocalIp(HttpServletRequest request) { if (realIp == null) {
String remoteAddr = request.getRemoteAddr(); if (forwarded == null) {
String forwarded = request.getHeader("X-Forwarded-For"); ip = remoteAddr;
String realIp = request.getHeader("X-Real-IP"); } else {
ip = remoteAddr + "/" + forwarded.split(",")[0];
String ip = null; }
if (realIp == null) { } else {
if (forwarded == null) { if (realIp.equals(forwarded)) {
ip = remoteAddr; ip = realIp;
} else { } else {
ip = remoteAddr + "/" + forwarded.split(",")[0]; if (forwarded != null) {
} forwarded = forwarded.split(",")[0];
} else { }
if (realIp.equals(forwarded)) { ip = realIp + "/" + forwarded;
ip = realIp; }
} else { }
if(forwarded != null){ return ip;
forwarded = forwarded.split(",")[0]; }
}
ip = realIp + "/" + forwarded; /**
} * 转化前台批量传入的ID值
} *
return ip; * @param data
} * @return 转化后的ID值数组
*/
/** public static int[] changeDataForm(String data) {
* 转化前台批量传入的ID值 String[] dataStr = data.split(",");
* @param data int[] dataInt = new int[dataStr.length];
* @return 转化后的ID值数组 for (int i = 0; i < dataStr.length; i++)
*/ dataInt[i] = Integer.parseInt(dataStr[i]);
public static int[] changeDataForm(String data) return dataInt;
{ }
String[] dataStr = data.split(",");
int[] dataInt = new int[dataStr.length]; /**
for(int i = 0 ;i < dataStr.length;i ++) * 解决导出文件中文乱码问题firefox和ie下中文乱码
dataInt[i] = Integer.parseInt(dataStr[i]); */
return dataInt; public static String changeUnicode(String fileName, String browserType) {
} String returnFileName = "";
try {
/** if (browserType.equalsIgnoreCase("MSIE")) {
* 解决导出文件中文乱码问题firefox和ie下中文乱码 returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
*/ returnFileName = returnFileName.replace(" ", "%20");
public static String changeUnicode(String fileName,String browserType) if (returnFileName.length() > 150) {
{ returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
String returnFileName = ""; returnFileName = returnFileName.replace(" ", "%20");
try }
{ } else if (browserType.equalsIgnoreCase("Firefox")) {
if(browserType.equalsIgnoreCase("MSIE")) returnFileName = new String(fileName.getBytes("ISO8859-1"), "ISO8859-1");
{ returnFileName = returnFileName.replace(" ", "%20");
returnFileName = URLEncoder.encode(fileName, "ISO8859-1"); } else {
returnFileName = returnFileName.replace(" ","%20"); returnFileName = URLEncoder.encode(fileName, "ISO8859-1");
if (returnFileName.length() > 150) returnFileName = returnFileName.replace(" ", "%20");
{ if (returnFileName.length() > 150) {
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
returnFileName = returnFileName.replace(" ", "%20"); returnFileName = new String(returnFileName.getBytes("GB2312"), "ISO8859-1");
} returnFileName = returnFileName.replace(" ", "%20");
} }
else if(browserType.equalsIgnoreCase("Firefox")) }
{ } catch (UnsupportedEncodingException e) {
returnFileName = new String(fileName.getBytes("ISO8859-1"),"ISO8859-1"); e.printStackTrace();
returnFileName = returnFileName.replace(" ", "%20"); }
} return returnFileName;
else }
{
returnFileName = URLEncoder.encode(fileName, "ISO8859-1"); /**
returnFileName = returnFileName.replace(" ","%20"); * 写理财日志内容转化特殊字符
if (returnFileName.length() > 150) *
{ * @param str 需要转化的字符
* @return 转化后的字符
returnFileName = new String(returnFileName.getBytes("GB2312"), "ISO8859-1"); */
returnFileName = returnFileName.replace(" ", "%20"); public static String htmlspecialchars(String str) {
} str = str.replaceAll("&", "&amp;");
} str = str.replaceAll("<", "&lt;");
} str = str.replaceAll(">", "&gt;");
catch (UnsupportedEncodingException e) str = str.replaceAll("\"", "&quot;");
{ return str;
e.printStackTrace(); }
}
return returnFileName; /**
} * 根据消费日期获取消费月
*
/** * @param consumeDate 消费日期
* 写理财日志内容转化特殊字符 * @return 返回消费月信息
* @param str 需要转化的字符 */
* @return 转化后的字符 public static String getConsumeMonth(String consumeDate) {
*/ return consumeDate.substring(0, 7);
public static String htmlspecialchars(String str) }
{
str = str.replaceAll("&", "&amp;"); /**
str = str.replaceAll("<", "&lt;"); * 获取当前日期的前XX个月
str = str.replaceAll(">", "&gt;"); *
str = str.replaceAll("\"", "&quot;"); * @param 之前的第几个月
return str; * @return 前XX个月字符串
} */
public static String getBeforeMonth(int beforeMonth) {
/** Calendar c = Calendar.getInstance();
* 根据消费日期获取消费月 c.add(Calendar.MONTH, -beforeMonth);
* @param consumeDate 消费日期 return new SimpleDateFormat("yyyy-MM").format(c.getTime());
* @return 返回消费月信息 }
*/
public static String getConsumeMonth(String consumeDate)
{ /**
return consumeDate.substring(0,7); * 获取email用户姓名
} *
* @param args
/** */
* 获取当前日期的前XX个月 public static String getEmailUserName(String emailAddress) {
* @param 之前的第几个月 return emailAddress.substring(0, emailAddress.lastIndexOf("@"));
* @return 前XX个月字符串 }
*/
public static String getBeforeMonth(int beforeMonth) /**
{ * 获取中文编码,邮件附件乱码问题解决
Calendar c = Calendar.getInstance(); *
c.add(Calendar.MONTH, -beforeMonth); * @param str
return new SimpleDateFormat("yyyy-MM").format(c.getTime()); * @return
} */
public static String getChineseString(String emailAttchmentTitle) {
if (emailAttchmentTitle != null && !emailAttchmentTitle.equals("")) {
/** try {
* 获取email用户姓名 return new String(emailAttchmentTitle.getBytes(), "ISO-8859-1");
* @param args } catch (UnsupportedEncodingException e) {
*/ e.printStackTrace();
public static String getEmailUserName(String emailAddress) }
{ }
return emailAddress.substring(0,emailAddress.lastIndexOf("@")); return emailAttchmentTitle;
} }
/** /**
* 获取中文编码,邮件附件乱码问题解决 * 判断userTel是否合法,userTel只能是数字
* @param str *
* @return * @param userTel
*/ * @return true 合法 false不合法
public static String getChineseString(String emailAttchmentTitle) */
{ public static boolean isTelNumber(String userTel) {
if(emailAttchmentTitle!=null&&!emailAttchmentTitle.equals("")) String reg_phone = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$";
{ String reg_tel = "^(1[0-9][0-9]|1[0-9][0|3|6|8|9])\\d{8}$";
try boolean b_phpne = Pattern.compile(reg_phone).matcher(userTel).matches();
{ boolean b_tel = Pattern.compile(reg_tel).matcher(userTel).matches();
return new String(emailAttchmentTitle.getBytes(),"ISO-8859-1"); return (b_phpne || b_tel);
} }
catch (UnsupportedEncodingException e)
{ /**
e.printStackTrace(); * 模糊判断电话号码是否合法,只能是数字
} *
} * @param macAddress
return emailAttchmentTitle; * @return
} */
public static boolean isTelNumberBySlur(String userTel) {
/** return Pattern.compile("^([\\s0-9]{0,12}$)").matcher(userTel).matches();
* 判断userTel是否合法,userTel只能是数字 }
* @param userTel
* @return true 合法 false不合法 /**
*/ * 获取当前时间的字符串类型
public static boolean isTelNumber(String userTel) *
{ * @return 处理后的字符串类型
String reg_phone="^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$"; */
String reg_tel="^(1[0-9][0-9]|1[0-9][0|3|6|8|9])\\d{8}$"; public static String getNowTime() {
boolean b_phpne=Pattern.compile(reg_phone).matcher(userTel).matches(); return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
boolean b_tel=Pattern.compile(reg_tel).matcher(userTel).matches(); }
return (b_phpne || b_tel);
} /**
* 开打指定文件
/** *
* 模糊判断电话号码是否合法,只能是数字 * @param filePath 文件的绝对路径
* @param macAddress */
* @return public static void openFile(String filePath) {
*/ String viewFilePath = filePath.replace("\\", "/");
public static boolean isTelNumberBySlur(String userTel) // Runtime.getRuntime().exec("cmd /c start "+filePath);
{ // 解决路径中带空格问题
return Pattern.compile("^([\\s0-9]{0,12}$)").matcher(userTel).matches(); Runtime r = Runtime.getRuntime();
} String[] cmdArray = new String[]{"cmd.exe", "/c", viewFilePath};
try {
/** r.exec(cmdArray);
* 获取当前时间的字符串类型 } catch (IOException e) {
* @return 处理后的字符串类型 e.printStackTrace();
*/ }
public static String getNowTime() }
{
return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()); /**
}
/**
* 开打指定文件
* @param filePath 文件的绝对路径
*/
public static void openFile(String filePath)
{
String viewFilePath = filePath.replace("\\", "/");
// Runtime.getRuntime().exec("cmd /c start "+filePath);
// 解决路径中带空格问题
Runtime r = Runtime.getRuntime();
String[] cmdArray = new String[] { "cmd.exe", "/c", viewFilePath };
try
{
r.exec(cmdArray);
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 判断字符串中是否含有中文 * 判断字符串中是否含有中文
* @author jishenghua *
* @param str * @param str
* @return * @return
* @author jishenghua
*/ */
public static boolean isContainsChinese(String str) public static boolean isContainsChinese(String str) {
{ return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).matches();
return Pattern.compile("[\u4e00-\u9fa5]").matcher(str).matches(); }
}
/**
/** * 过滤html文件中的文本
* 过滤html文件中的文本 *
* @param content * @param content
* @return过滤后的文本 * @return过滤后的文本
*/ */
public static String filterText(String content) public static String filterText(String content) {
{ return content.replace("/<(?:.|\\s)*?>/g", "");
return content.replace("/<(?:.|\\s)*?>/g",""); }
}
/**
/**
* 去掉字符串中所有符号,不论是全角,还是半角的,或是货币符号或者空格等 * 去掉字符串中所有符号,不论是全角,还是半角的,或是货币符号或者空格等
* @author jishenghua
* @param s
* @return
* *
* @param s
* @return
* @author jishenghua
*/ */
public static String removeSymbolForString(String s) public static String removeSymbolForString(String s) {
{
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
char[] chars = s.toCharArray(); char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) for (int i = 0; i < chars.length; i++) {
{ if ((chars[i] >= 19968 && chars[i] <= 40869) || (chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 65 && chars[i] <= 90)) {
if ((chars[i] >= 19968 && chars[i] <= 40869) || (chars[i] >= 97 && chars[i] <= 122) || (chars[i] >= 65 && chars[i] <= 90))
{
buffer.append(chars[i]); buffer.append(chars[i]);
} }
} }
return buffer.toString(); return buffer.toString();
} }
/** /**
* 获取一个字符串的MD5 * 获取一个字符串的MD5
*
* @param msg * @param msg
* @return 加密后的MD5字符串 * @return 加密后的MD5字符串
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
public static String md5Encryp(String msg) throws NoSuchAlgorithmException public static String md5Encryp(String msg) throws NoSuchAlgorithmException {
{
// 生成一个MD5加密计算摘要 // 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数 // 计算md5函数
md.update(msg.getBytes()); md.update(msg.getBytes());
return new BigInteger(1, md.digest()).toString(16); return new BigInteger(1, md.digest()).toString(16);
} }
/** /**
* 处理字符串null值 * 处理字符串null值
*
* @param beforeStr 处理前字符串 * @param beforeStr 处理前字符串
* @return 处理后的字符串 * @return 处理后的字符串
*/ */
public static String dealNullStr(String beforeStr) public static String dealNullStr(String beforeStr) {
{ if (null == beforeStr || beforeStr.length() == 0)
if(null == beforeStr || beforeStr.length()==0) return "";
return ""; return beforeStr;
return beforeStr;
} }
/** /**
* 使用参数Format将字符串转为Date * 使用参数Format将字符串转为Date
* @author jishenghua *
* @param strDate * @param strDate
* @param pattern * @param pattern
* @return * @return
* @throws ParseException * @throws ParseException
* * @author jishenghua
*/ */
public static Date parse(String strDate, String pattern) public static Date parse(String strDate, String pattern)
throws ParseException throws ParseException {
{
return new SimpleDateFormat(pattern).parse(strDate); return new SimpleDateFormat(pattern).parse(strDate);
} }
...@@ -583,34 +563,28 @@ public class Tools ...@@ -583,34 +563,28 @@ public class Tools
// { // {
// return content.matches("/<img(?:.|\\s)*?>/g"); // return content.matches("/<img(?:.|\\s)*?>/g");
// } // }
public static void main(String[] args) public static void main(String[] args) {
{ String aa = "的付的反对法的发的说法";
String aa = "的付的反对法的发的说法"; char[] bb = aa.toCharArray();
char[] bb = aa.toCharArray(); for (char c : bb) {
for(char c : bb) System.out.println(c);
{ }
System.out.println(c); System.out.println(getBeforeMonth(1));
}
System.out.println(getBeforeMonth(1)); try {
System.out.println(md5Encryp("guest"));
try System.out.println(md5Encryp("admin"));
{ } catch (NoSuchAlgorithmException e) {
System.out.println(md5Encryp("guest")); // TODO Auto-generated catch block
System.out.println(md5Encryp("admin")); e.printStackTrace();
} }
catch (NoSuchAlgorithmException e)
{ String value = "2333";
// TODO Auto-generated catch block System.out.println(checkStrIsNum(value));
e.printStackTrace();
} for (int i = 0; i < 100; i++) {
System.out.print(getRandomChar() + " || ");
String value = "2333"; }
System.out.println(checkStrIsNum(value)); }
for(int i = 0 ;i < 100;i ++)
{
System.out.print(getRandomChar() + " || ");
}
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment