1:Const.java
import java.io.File;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang3.StringUtils;
import com.szmsd.log.db.DBConn;
@SuppressWarnings("all")
public class Const {
public static String key = "";
public static String driverClassName = "";
public static String url = "";
public static String username = "";
public static String password = "";
public static String tempuser = "";
public static String systemid = "";
public static String initSize = "";
public static String maxSize="";
public static String minSize="";
public static String acquireIncrement="";
public static String idleConnectionTestPeriod="";
public static String charset="";
public static String logFilePath="";
public Const() {
PropertiesConfiguration pc = PropertiesUtils.getInstance();
key = pc.getString("key");
driverClassName = pc.getString("driverClassName");
url = pc.getString("url");
username = pc.getString("username");
password = pc.getString("password");
systemid = pc.getString("systemid");
tempuser = pc.getString("tempuser");
initSize=pc.getString("initSize");
maxSize=pc.getString("maxSize");
minSize=pc.getString("minSize");
acquireIncrement=pc.getString("acquireIncrement");
idleConnectionTestPeriod=pc.getString("idleConnectionTestPeriod");
charset=pc.getString("charset");
logFilePath=pc.getString("logFilePath");
}
/**
*
* @param path
* 文件夹路径
*/
public static void isExist(String path) {
File file = new File(path);
// 判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
file.mkdir();
}
}
/**
* 两个参数 是否为空,有一个为空都返回true
*
* @return
*/
public static boolean isEmpty(String... str) {
for (String s : str) {
if (StringUtils.isEmpty(s)) {
return true;
}
}
return false;
}
/**
* 功能:生成签名结果
*
* @param sArray
* 要签名的数组
* @param key
* 安全校验码
* @return 签名结果字符串
*/
public static String BuildMysign(Map sArray, String key) {
String prestr = CreateLinkString(sArray); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
prestr = prestr + key; // 把拼接后的字符串再与安全校验码直接连接起来
String mysign = Md5Encrypt.md5(prestr);
return mysign;
}
/**
* 功能:除去数组中的空值和签名参数
*
* @param sArray
* 签名参数组
* @return 去掉空值与签名参数后的新签名参数组
*/
public static Map ParaFilter(Map sArray) {
List keys = new ArrayList(sArray.keySet());
Map sArrayNew = new HashMap();
for (int i = 0; i < keys.size(); i++) {
String key = (String) keys.get(i);
String value = (String) sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign") || key.equalsIgnoreCase("sign_type")) {
continue;
}
sArrayNew.put(key, value);
}
return sArrayNew;
}
/**
* 功能:把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
*
* @param params
* 需要排序并参与字符拼接的参数组
* @return 拼接后字符串
*/
public static String CreateLinkString(Map params) {
List keys = new ArrayList(params.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = (String) keys.get(i);
String value = (String) params.get(key);
if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
prestr = prestr + key + "=" + value;
} else {
prestr = prestr + key + "=" + value + "&";
}
}
return prestr;
}
public static String getDateStr(Date date) {
date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = formatter.format(date);
return time;
}
public static String getStringToDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
public static String getStrAllToDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
public static String getStrAllToMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
return sdf.format(date);
}
public static String getStrHmsToDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(date);
}
public static String getStrHHToDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("HH");
return sdf.format(date);
}
public static String getStrMMToDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("mm");
return sdf.format(date);
}
/**
* 获取请求主路径
*
* @param request
* @return
*/
public static String getRequestBasePath(HttpServletRequest request) {
String path = request.getContextPath();
int post = request.getServerPort();
String basePath = "";
if (post == 80) {
basePath = request.getScheme() + "://" + request.getServerName() + path;
} else {
basePath = request.getScheme() + "://" + request.getServerName() + ":" + post + path;
}
return basePath;
}
/** 获取网站域名地址 */
public static String getHOST(HttpServletRequest request) {
String host = "";
host = request.getScheme() + "://" + request.getServerName();
if (host.indexOf("192.168") != -1 || host.indexOf("localhost") != -1) {
host = "http://www.gotofun.cn";
}
return host;
}
/** 获取请求IP */
public static String getRequestIP(HttpServletRequest request) {
String ip = "";
try {
ip = request.getHeader("X-Real-IP");
} catch (Exception ex) {
ip = "";
}
if (ip == null) {
ip = "";
}
if ("".equals(ip)) {
ip = request.getRemoteAddr();
}
if (ip == null) {
ip = "";
}
return ip;
}
/** 获取Cookie值 */
public static String getCookie(String cookieName, HttpServletRequest request) {
String val = "";
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return "";
}
Cookie cookie;
for (int i = 0, total = cookies.length; i < total; i++) {
cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
val = cookie.getValue();
break;
}
}
return val;
}
/*-- 字符串,数字处理 - BEGIN --*/
/** 处理空字符串 */
public static String getStr(String str) {
if (str != null) {
return str;
} else {
return "";
}
}
/** 替换字符串 */
public static String getReplaceStr(String str, String oldChar, String newChar) {
try {
if (str != null) {
return str.replace(oldChar, newChar);
} else {
return "";
}
} catch (Exception ex) {
return "";
}
}
/** iso-8859-1转码成GBK */
public static String getGBK(String str) {
try {
if (str != null) {
return new String(str.getBytes("iso-8859-1"), "gbk").trim();
} else {
return "";
}
} catch (Exception ex) {
return "";
}
}
/** gbk转码成iso-8859-1 */
public static String setGBK(String str) {
try {
if (str != null) {
return new String(str.getBytes("gbk"), "iso-8859-1").trim();
} else {
return "";
}
} catch (Exception ex) {
return "";
}
}
/** iso-8859-1转码成UTF-8 */
public static String getUTF8(String str) {
try {
if (str != null) {
return new String(str.getBytes("ISO-8859-1"), "UTF-8").trim();
} else {
return "";
}
} catch (Exception ex) {
return "";
}
}
/** UTF-8转码成iso-8859-1 */
public static String setUTF8(String str) {
try {
if (str != null) {
return new String(str.getBytes("utf-8"), "iso-8859-1").trim();
} else {
return "";
}
} catch (Exception ex) {
return "";
}
}
/** 字符串转换int类型 */
public static int getStrInt(String str, int defaultValue) {
if ("".equals(str)) {
return defaultValue;
}
int num = 0;
try {
num = Integer.parseInt(str);
} catch (Exception ex) {
num = defaultValue;
}
return num;
}
/** 字符串转换float类型 */
public static float getStrFloat(String str, float defaultValue) {
if ("".equals(str)) {
return defaultValue;
}
float num = 0f;
try {
num = Float.parseFloat(str);
} catch (Exception ex) {
num = defaultValue;
}
return num;
}
/** 字符串转换double类型 */
public static double getStrDouble(String str, double defaultValue) {
if ("".equals(str)) {
return defaultValue;
}
double num = 0;
try {
num = Double.parseDouble(str);
} catch (Exception ex) {
num = defaultValue;
}
return num;
}
/*-- 字符串,数字处理 - END --*/
/**
* request对象获取String值
*
* @param request
* Request对象
* @param key
* 所需要取的键值
* @param defaultValue
* 默认值
* @return value 返回request的String值
*/
public static String getPara(HttpServletRequest request, String key, String defaultValue) {
String value = "";
try {
value = request.getParameter(key);
} catch (Exception ex) {
value = null;
}
if (value == null) {
return defaultValue;
}
value = value.trim();
if ("".equals(value)) {
return defaultValue;
}
return value;
}
/** request对象获取int值 */
public static int getPara(HttpServletRequest request, String key, int defaultValue) {
String valueStr = "";
try {
valueStr = request.getParameter(key);
} catch (Exception ex) {
valueStr = null;
}
if (valueStr == null) {
return defaultValue;
}
valueStr = valueStr.trim();
int value = 0;
try {
value = Integer.parseInt(valueStr);
} catch (Exception ex) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/** request对象获取float值 */
public static float getPara(HttpServletRequest request, String key, float defaultValue) {
String valueStr = "";
try {
valueStr = request.getParameter(key);
} catch (Exception ex) {
valueStr = null;
}
if (valueStr == null) {
return defaultValue;
}
valueStr = valueStr.trim();
float value = 0;
try {
value = Float.parseFloat(valueStr);
} catch (Exception ex) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/** request对象获取double值 */
public static double getPara(HttpServletRequest request, String key, double defaultValue) {
String valueStr = "";
try {
valueStr = request.getParameter(key);
} catch (Exception ex) {
valueStr = null;
}
if (valueStr == null) {
return defaultValue;
}
valueStr = valueStr.trim();
double value = 0;
try {
value = Double.parseDouble(valueStr);
} catch (Exception ex) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/** session对象获取String值 */
public static String getPara(HttpSession session, String key, String defaultValue) {
String value = "";
try {
if (session.getAttribute(key) != null) {
value = session.getAttribute(key).toString().trim();
}
} catch (Exception e) {
value = "";
}
if ("".equals(value)) {
value = defaultValue;
}
return value;
}
/** session对象获取int值 */
public static int getPara(HttpSession session, String key, int defaultValue) {
int value = 0;
try {
if (session.getAttribute(key) != null) {
try {
value = Integer.parseInt(session.getAttribute(key).toString().trim());
} catch (Exception ex) {
value = 0;
}
}
} catch (Exception e) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/** session对象获取float值 */
public static float getPara(HttpSession session, String key, float defaultValue) {
float value = 0;
try {
if (session.getAttribute(key) != null) {
try {
value = Float.parseFloat(session.getAttribute(key).toString().trim());
} catch (Exception ex) {
value = 0;
}
}
} catch (Exception e) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/** session对象获取double值 */
public static double getPara(HttpSession session, String key, double defaultValue) {
double value = 0;
try {
if (session.getAttribute(key) != null) {
try {
value = Double.parseDouble(session.getAttribute(key).toString().trim());
} catch (Exception ex) {
value = 0;
}
}
} catch (Exception e) {
value = 0;
}
if (value <= 0) {
value = defaultValue;
}
return value;
}
/*-- 时间函数 - BEGIN --*/
/** 转换成指定日期格式 Date对象 --未用 */
public static Date getNewDate(String dateStr, String sftStr) {
SimpleDateFormat sdf = new SimpleDateFormat(sftStr);
try {
return (Date) sdf.parse(dateStr);
} catch (Exception e) {
return new Date();
}
}
/**
* 获取传入的字符串转换, 传入的指定日期时间格式
*
* @param dateStr
* 日期格式字符串
* @param sftStr
* 指定的日期时间格式编码, 例如:yyyy-MM-dd HH:mm:ss
* @return 如传入的字符串不是日期时间格式, 将返回系统的日期时间
*/
public static String getDate(String dateStr, String sftStr) {
SimpleDateFormat sdf = new SimpleDateFormat(sftStr);
if ("".equals(dateStr)) {
return sdf.format(new Date());
}
try {
return sdf.format((Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr));
} catch (Exception ex1) {
try {
return sdf.format((Date) new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
} catch (Exception ex2) {
return sdf.format(new Date());
}
}
}
/** 转换成日期格式yyyy-MM-dd */
public static String getDate(String dateStr) {
return Const.getDate(dateStr, "yyyy-MM-dd");
}
/** 转换成日期格式yyyy-MM-dd HH:mm:ss */
public static String getDateTime(String dateStr) {
return Const.getDate(dateStr, "yyyy-MM-dd HH:mm:ss");
}
/** 转换成日期格式HH:mm:ss */
public static String getTime(String dateStr) {
return Const.getDate(dateStr, "HH:mm:ss");
}
/** 转换成日期格式hh:mm */
public static String getTimeHM(String dateStr) {
return Const.getDate(dateStr, "HH:mm");
}
/** 转换成日期格式yyyy年MM月dd日 */
public static String getGBKDate(String dateStr) {
return Const.getDate(dateStr, "yyyy年MM月dd日");
}
/** 转换成日期格式yyyy年MM月dd日 HH:mm:ss */
public static String getGBKDateTime(String dateStr) {
return Const.getDate(dateStr, "yyyy年MM月dd日 HH:mm:ss");
}
/** 转换成日期格式yyyy年MM月dd日 HH:mm */
public static String getGBKDateHM(String dateStr) {
return Const.getDate(dateStr, "yyyy年MM月dd日 HH:mm");
}
/** 转换成日期格式yyyy年MM月dd日 HH点mm分ss秒 isSec 是否显示秒 (1:显示,0:不显示) */
public static String getGBKDateHHmm(String dateStr, int isSec) {
if (isSec == 1) {
return Const.getDate(dateStr, "yyyy年MM月dd日 HH点mm分ss秒");
} else {
return Const.getDate(dateStr, "yyyy年MM月dd日 HH点mm分");
}
}
/** 转换成日期格式MM月dd日 */
public static String getGBKMD(String dateStr) {
return Const.getDate(dateStr, "MM月dd日");
}
/** 转换成日期格式HH点mm分 */
public static String getGBKHM(String dateStr) {
return Const.getDate(dateStr, "HH点mm分");
}
/** 获取年yyyy */
public static String getYear(String dateStr) {
return Const.getDate(dateStr, "yyyy");
}
/** 获取月MM */
public static String getMonth(String dateStr) {
return Const.getDate(dateStr, "MM");
}
/** 获取日dd */
public static String getDay(String dateStr) {
return Const.getDate(dateStr, "dd");
}
/** 转换成日期格式yyyyMMddHHmmss */
public static String getDateTimeStr(String dateStr) {
return Const.getDate(dateStr, "yyyyMMddHHmmss");
}
/** 转换成日期格式yyyyMMddhhmmssSS */
public static String getDateTimeMSEL(String dateStr) {
return Const.getDate(dateStr, "yyyyMMddHHmmssSS");
}
/** 获取星期几WEEK */
public static String getWeek(String dateStr) {
String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = null;
try {
sdf.set2DigitYearStart(sdf.parse(dateStr));
c = sdf.getCalendar();
return weekDays[c.get(Calendar.DAY_OF_WEEK) - 1];
} catch (Exception e) {
sdf.set2DigitYearStart(new Date());
c = sdf.getCalendar();
return weekDays[c.get(Calendar.DAY_OF_WEEK) - 1];
}
}
/** 获取一年中的第几周WEEK */
public static String getWeekYear(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = null;
Calendar c = null;
try {
d = sdf.parse(dateStr);
c = Calendar.getInstance();
c.setTimeInMillis(d.getTime());
return Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
} catch (Exception e) {
d = new Date();
c = Calendar.getInstance();
c.setTimeInMillis(d.getTime());
return Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
}
}
/**
* 根据生日日期获取年龄
*
* @param birthday
* 生日日期
* @param unit
* 单位, 例如:岁
* @return 返回年龄, 如传入的是非法日期字符串, 则返回“保密”
*/
public static String getAgeNum(String birthday, String unit) {
if ("".equals(birthday)) {
return "保密";
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int birYear = 0;
int nowYear = 0;
int age = 0;
Calendar c = null;
try {
sdf.set2DigitYearStart(sdf.parse(birthday));
c = sdf.getCalendar();
birYear = c.get(Calendar.YEAR);
} catch (Exception e) {
sdf.set2DigitYearStart(new Date());
c = sdf.getCalendar();
birYear = c.get(Calendar.YEAR);
}
sdf.set2DigitYearStart(new Date());
c = sdf.getCalendar();
nowYear = c.get(Calendar.YEAR);
age = nowYear - birYear;
if (age > 0) {
return Integer.toString(age) + unit;
} else {
return "保密";
}
}
}
/**
* 获取传入的时间秒差,去除毫秒 传入的时间格式yyyy-MM-dd hh:mm:ss
*
* @param dateStr
* @return 如传入的字符串不是日期时间格式, 将返回系统的当前时间的秒差
*/
public static String getTimeLong(String dateStr) {
String tieml = "";
if (!"".equals(dateStr)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
tieml = Long.toString(sdf.parse(dateStr).getTime());
} catch (Exception e) {
tieml = Long.toString(System.currentTimeMillis());
}
} else {
tieml = Long.toString(System.currentTimeMillis());
}
if (tieml.length() > 10) {
tieml = tieml.substring(0, 10);
}
return tieml;
}
/**
* 获取传入的时间秒差,去除毫秒 传入的时间格式yyyy-MM-dd hh:mm:ss
*
* @param dateStr
* @return long 如传入的字符串不是日期时间格式, 将返回系统的当前时间的秒差
*/
public static Long getTimeLongLong(String dateStr) {
long tieml = 0;
if (!"".equals(dateStr)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
tieml = sdf.parse(dateStr).getTime();
} catch (Exception e) {
tieml = System.currentTimeMillis();
}
} else {
tieml = System.currentTimeMillis();
}
String tiemlStr = Long.toString(tieml);
if (tiemlStr.length() > 10) {
tiemlStr = tiemlStr.substring(0, 10);
}
try {
tieml = Long.parseLong(tiemlStr);
} catch (Exception ex) {
tieml = 0;
}
return tieml;
}
/**
* 获取传入的时间秒差 传入的时间格式yyyy-MM-dd hh:mm:ss
*
* @param dateStr
* @return
*/
public static String getTimeLong1(String dateStr) {
String tieml = "";
if (!"".equals(dateStr)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
tieml = Long.toString(sdf.parse(dateStr).getTime());
} catch (Exception e) {
tieml = Long.toString(System.currentTimeMillis());
}
} else {
tieml = Long.toString(System.currentTimeMillis());
}
return tieml;
}
/** 传入的时间增加N天 */
public static String getAddDay(String dateStr, int day) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(dateStr));
cd.add(Calendar.DATE, day);// 增加N天
return sdf.format(cd.getTime());
} catch (Exception ex) {
return "";
}
}
/**
* 时间戳转换成普通时间格式
*
* @param _timelong
* 时间戳
* @param isToSec
* 是否保留到秒戳
* @return
*/
public static String getTimeLongToDate(String _timelong, boolean isToSec) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (_timelong == null || "".equals(_timelong)) {
return sdf.format(new Date());
}
long timelong = 0;
try {
timelong = Long.parseLong(_timelong);
} catch (Exception ex) {
ex.printStackTrace();
}
if (isToSec) {
timelong = timelong * 1000;
}
try {
return sdf.format(new Date(timelong));
} catch (Exception e) {
return sdf.format(new Date());
}
}
/** 获取传入的两个时间秒差 */
public static long getTimeLongDiff(String dateStr1, String dateStr2) {
if (!"".equals(dateStr1) && !"".equals(dateStr2)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long tieml1 = 0, tieml2 = 0;
try {
tieml1 = sdf.parse(dateStr1).getTime();
} catch (Exception e) {
tieml1 = 0;
}
try {
tieml2 = sdf.parse(dateStr2).getTime();
} catch (Exception e) {
tieml2 = 0;
}
if (tieml1 > 0 && tieml2 > 0) {
return (tieml2 - tieml1) / 1000;
} else {
return 0;
}
} else {
return 0;
}
}
/**
* 获取剩余时间 天/小时/分钟/秒
*
* @param _sec
* 剩余秒数
* @param format
* @param fillStr
* @return
*/
public static String getResDateGBK(long _sec, String fillStr, int count) {
long day = 0, hour = 0, minute = 0, second = 0;
long sec = _sec;// 总秒数
if (sec >= 86400) {
day = sec / 86400;
sec = sec % 86400;
}
if (sec >= 3600) {
hour = sec / 3600;
sec = sec % 3600;
}
if (sec >= 60) {
minute = sec / 60;
sec = sec % 60;
}
if (sec > 0) {
second = sec;
}
String str = "";
int num = 0;
if (day > 0) {
if (hour <= 0 && minute <= 0 && second <= 0) {
str += day + "天";
} else {
str += day + "天" + fillStr;
}
num++;
}
if (hour > 0) {
if (minute <= 0 && second <= 0) {
str += hour + "小时";
} else {
str += hour + "小时" + fillStr;
}
num++;
}
if (minute > 0) {
if (num >= count - 1) {
str += minute + "分";
} else {
str += minute + "分" + fillStr;
}
num++;
}
if (num < count) {
str += second + "秒";
}
return str;
}
/**
* 获取剩余时间 天/小时/分钟/秒
*
* @param _sec
* 剩余秒数
* @param format
* @param fillStr
* @param isbold
* @return
*/
public static String getResDateGBK(long _sec, String fillStr, int count, boolean isbold) {
long day = 0, hour = 0, minute = 0, second = 0;
long sec = _sec;// 总秒数
if (sec >= 86400) {
day = sec / 86400;
sec = sec % 86400;
}
if (sec >= 3600) {
hour = sec / 3600;
sec = sec % 3600;
}
if (sec >= 60) {
minute = sec / 60;
sec = sec % 60;
}
if (sec > 0) {
second = sec;
}
String str = "";
int num = 0;
if (day > 0) {
if (hour <= 0 && minute <= 0 && second <= 0) {
if (isbold) {
str += "<strong>" + day + "</strong>天";
} else {
str += day + "天";
}
} else {
if (isbold) {
str += "<strong>" + day + "</strong>天" + fillStr;
} else {
str += day + "天" + fillStr;
}
}
num++;
}
if (hour > 0) {
if (minute <= 0 && second <= 0) {
if (isbold) {
str += "<strong>" + hour + "</strong>小时";
} else {
str += hour + "小时";
}
} else {
if (isbold) {
str += "<strong>" + hour + "</strong>小时" + fillStr;
} else {
str += hour + "小时" + fillStr;
}
}
num++;
}
if (minute > 0) {
if (num >= count - 1) {
if (isbold) {
str += "<strong>" + minute + "</strong>分";
} else {
str += minute + "分";
}
} else {
if (isbold) {
str += "<strong>" + minute + "</strong>分" + fillStr;
} else {
str += minute + "分" + fillStr;
}
}
num++;
}
if (num < count) {
if (isbold) {
str += "<strong>" + second + "</strong>秒";
} else {
str += second + "秒";
}
}
return str;
}
/**
* 比较两个日期的天数
*
* @param date1
* @param date2
* @return
*/
public static int getBetweenDays(String date1, String date2) {
try {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int betweenDays = 0;
Date d1 = format.parse(date1);
Date d2 = format.parse(date2);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
// 保证第二个时间一定大于第一个时间
if (c1.after(c2)) {
c1 = c2;
c2.setTime(d1);
}
int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
for (int i = 0; i < betweenYears; i++) {
c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
}
return betweenDays;
} catch (Exception ex) {
return -1;
}
}
/**
* 获取多久前 如:30秒前,5分钟前,昨天10:00,前天11:20等
*
* @param dateStr
* 时间
* @param istoday
* 是否显示"今天"两字
* @return
*/
public static String getDateForetime(String dateStr, int istoday) {
if ("".equals(dateStr)) {
return "";
}
int dayNum = Const.getBetweenDays(dateStr, Const.getDate(""));
if (dayNum > 2) {
return Const.getDate(dateStr, "MM-dd HH:mm");
} else if (dayNum == 2) {
return "前天" + Const.getDate(dateStr, "HH:mm");
} else if (dayNum == 1) {
return "昨天" + Const.getDate(dateStr, "HH:mm");
} else if (dayNum == -1) {
return "";
}
long sec = Const.getTimeLongLong("") - Const.getTimeLongLong(dateStr);
if (sec > 3600) {
if (istoday == 1) {
return "今天" + Const.getDate(dateStr, "HH:mm");
} else {
return Const.getDate(dateStr, "HH:mm");
}
}
if (sec > 60) {
return sec / 60 + "分钟前";
}
if (sec <= 0) {
sec = 1;
}
return sec + "秒前";
}
/**
* @param date1
* 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
* @param date2
* 被比较的时间 为空(null)则为当前时间
* @param stype
* 返回值类型 0为多少天, 1为多少个月, 2为多少年
* @return 举例: compareDate("2009-09-12", null, 0);//比较天
* compareDate("2009-09-12", null, 1);//比较月
* compareDate("2009-09-12", null, 2);//比较年
*/
public static int getCompareDate(String startDay, String endDay, int stype) {
int n = 0;
String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";
endDay = endDay == null ? getCurrentDate("yyyy-MM-dd") : endDay;
DateFormat df = new SimpleDateFormat(formatStyle);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
try {
c1.setTime(df.parse(startDay));
c2.setTime(df.parse(endDay));
} catch (Exception e3) {
System.out.println("wrong occured");
}
while (!c1.after(c2)) { // 循环对比,直到相等,n就是所要的结果
n++;
if (stype == 1) {
c1.add(Calendar.MONTH, 1);// 比较月份,月份+1
} else {
c1.add(Calendar.DATE, 1);// 比较天数,日期+1
}
}
n = n - 1;
if (stype == 2) {
n = (int) n / 365;
}
return n;
}
public static String getCurrentDate(String format) {
Calendar day = Calendar.getInstance();
day.add(Calendar.DATE, 0);
SimpleDateFormat sdf = new SimpleDateFormat(format);// "yyyy-MM-dd"
String date = sdf.format(day.getTime());
return date;
}
/*-- 时间函数 - END --*/
/**
* 获取随机码
*
* @param digit
* 位数(4,5,6位)
* @return valString 随机码
*/
public static String getRandomCode(int digit) {
String valString = "";
/** 不允许出现的随机码 */
String[] pingbi_val = null;
String[] pingbi_val4 = { "1234", "4321", "1111", "2222", "3333", "4444", "5555", "6666", "7777", "8888", "9999" };
String[] pingbi_val5 = { "12345", "54321", "11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999" };
String[] pingbi_val6 = { "123456", "654321", "111111", "222222", "333333", "444444", "555555", "666666", "777777", "888888", "999999" };
/** 随机码中不允许出现的字符 */
String[] pingbi_zifu = { "000", "111", "222", "333", "444", "555", "666", "777", "888", "999" };
int i = 0, total = 0;
Random r = new Random();
boolean bool = true;
/** while循环生成随机码 -- BEGIN */
while (true) {
bool = true;
if (digit == 4) {
pingbi_val = pingbi_val4;
valString = Integer.toString(r.nextInt(8999) + 1000).trim(); // 获取4位的随机码
} else if (digit == 5) {
pingbi_val = pingbi_val5;
valString = Integer.toString(r.nextInt(89999) + 10000).trim(); // 获取5位的随机码
} else {
pingbi_val = pingbi_val6;
valString = Integer.toString(r.nextInt(899999) + 100000).trim(); // 获取6位的随机码
}
/** 检查是否违反定好的屏蔽随机码数组 */
total = pingbi_val.length;
for (i = 0; i < total; i++) {
if (valString.equals(pingbi_val[i].trim())) {
bool = false;
}
}
/** 检查是否违反随机码规则, 随机码不能有超过三个以上相同的数字 */
total = pingbi_zifu.length;
for (i = 0; i < total; i++) {
if (valString.indexOf(pingbi_zifu[i]) >= 0) {
bool = false;
}
}
if (bool) {
break;
}
}
/** while循环生成随机码 -- END */
return valString;
}
/** 判断是否为整型数字 */
public static boolean isNum(String numStr) {
if ("".equals(numStr)) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(numStr);
if (!isNum.matches()) {
return false;
}
return true;
}
/** 判断是否为手机号码 */
public static boolean isMobile(String telStr) {
if ("".equals(telStr)) {
return false;
}
Pattern pattern = null;
Matcher isNum1 = null, isNum2 = null, isNum3 = null;
pattern = Pattern.compile("13\\d{9}");
isNum1 = pattern.matcher(telStr);
pattern = Pattern.compile("15\\d{9}");
isNum2 = pattern.matcher(telStr);
pattern = Pattern.compile("18\\d{9}");
isNum3 = pattern.matcher(telStr);
if (!isNum1.matches() && !isNum2.matches() && !isNum3.matches()) {
return false;
}
return true;
}
/** 判断是否为正确的EMAIL格式 */
public static boolean isEmail(String emailStr) {
if ("".equals(emailStr)) {
return false;
}
Pattern pattern = null;
Matcher isNum = null;
pattern = Pattern.compile("[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+");
isNum = pattern.matcher(emailStr);
if (!isNum.matches()) {
return false;
}
return true;
}
/** 返回URL编码后的字符串, 编码默认为GBK */
public static String getURLEncoder(String str) {
if (str == null || "".equals(str)) {
return "";
}
try {
return URLEncoder.encode(str, "gbk");
} catch (Exception ex) {
return str;
}
}
/** 返回URL编码后的字符串, 编码根据传入参数 */
public static String getURLEncoder(String str, String charstr) {
if (str == null || "".equals(str)) {
return "";
}
if (!"".equals(charstr)) {
try {
return URLEncoder.encode(str, charstr);
} catch (Exception ex) {
return str;
}
} else {
try {
return URLEncoder.encode(str);
} catch (Exception ex) {
return str;
}
}
}
/** 根据传入的EMAIL,获取相对应的官方登陆地址 */
public static String getEmailHTTP(String email1) {
String email = email1;
if (!Const.isEmail(email)) {
return "";
}
String e = email.substring(email.indexOf("@"));
String http = "http://www." + e.substring(1); // 默认
if (e.indexOf("@126.com") > -1) {
http = "http://www.126.com/";
} else if (e.indexOf("@163.com") > -1) {
http = "http://mail.163.com/";
} else if (e.indexOf("@vip.163.com") > -1) {
http = "http://mail.163.com/";
} else if (e.indexOf("@yeah.net") > -1) {
http = "http://www.yeah.net/";
} else if (e.indexOf("@188.com") > -1) {
http = "http://www.188.com/";
} else if (e.indexOf("@sina.com") > -1 || e.indexOf("@vip.sina.com") > -1) {
http = "http://mail.sina.com.cn/";
} else if (e.indexOf("@sohu.com") > -1 || e.indexOf("@vip.sohu.com") > -1) {
http = "http://mail.sohu.com/";
} else if (e.indexOf("@gmail.com") > -1) {
http = "http://www.gmail.com/";
} else if (e.indexOf("@139.com") > -1) {
http = "http://mail.139.com/";
} else if (e.indexOf("@qq.com") > -1 || e.indexOf("@vip.qq.com") > -1) {
http = "http://mail.qq.com/";
} else if (e.indexOf("@live.com") > -1 || e.indexOf("@hotmail.com") > -1) {
http = "http://www.hotmail.com/";
} else if (e.indexOf("@21cn.com") > -1) {
http = "http://mail.21cn.com/";
} else if (e.indexOf("@yahoo.com.cn") > -1 || e.indexOf("@yahoo.cn") > -1) {
http = "http://mail.cn.yahoo.com/";
} else if (e.indexOf("@foxmail.com") > -1) {
http = "http://www.foxmail.com/";
} else if (e.indexOf("@tom.com") > -1) {
http = "http://mail.tom.com/";
} else if (e.indexOf("@263.net") > -1 || e.indexOf("@263.net.cn") > -1 || e.indexOf("@x263.net") > -1) {
http = "http://www.263.net/";
} else if (e.indexOf("@sogou.com") > -1) {
http = "http://mail.sogou.com/";
}
return http;
}
/** 判断字符串的编码 */
public static String getEncoding(String str) {
String encode = "GB2312";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception ex) {
}
encode = "ISO-8859-1";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception ex) {
}
encode = "UTF-8";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception ex) {
}
encode = "GBK";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception ex) {
}
encode = "BIG5";
try {
if (str.equals(new String(str.getBytes(encode), encode))) {
return encode;
}
} catch (Exception ex) {
}
return "";
}
/**
* 获取总页数, 根据传入集合的总行数与每页显示行数
*
* @param totalCount
* 总行数
* @param num
* 每页显示行数
* @return 总页数
*/
public static int getTotalPage(int totalCount, int num) {
int totalPage = totalCount / num;
if (totalCount % num > 0) {
totalPage++;
}
return totalPage;
}
/**
* 获取合格的当前页码, 根据传入的总页数
*
* @param totalPage
* 总页数
* @return 当前页码
*/
public static int getPageNum(int totalPage, int pageNum) {
int page = pageNum;
if (pageNum >= totalPage) {
page = totalPage;
}
if (page <= 0) {
page = 1;
}
return page;
}
/**
* 获取开始页码 根据传入的展示数字页码个数, 当前页码, 总页码
*
* @param num
* 展示数字页码个数
* @param totalPage
* 总页码
* @param pageNum
* 当前页码
* @return 开始页码
*/
public static int getBeginNum(int num, int pageNum, int totalPage) {
int bnum = 1;
if (num > totalPage) {
bnum = 1;
} else {
int leftnum = 0;
if (num % 2 == 0) {
leftnum = num / 2 - 1;
} else {
leftnum = num / 2;
}
bnum = pageNum - leftnum;
if (bnum + num - 1 >= totalPage) {
bnum = totalPage - num + 1;
}
}
if (bnum <= 0) {
bnum = 1;
}
return bnum;
}
/**
* 传入Double类型的数值,返回指定小数点后位数
*
* @param val
* Double类型数值
* @param decimal
* 小数点后位数
* @param bool
* 是否在小于1大于0时,返回1
* @return
*/
public static String getFloatDecimal(double val, int beginDecimal, int endDecimal, boolean bool) {
String returnValStr = "";
String decimalStr = "", beginStr = "", endStr = "";
int i = 0;
for (i = 0; i < beginDecimal; i++) {
beginStr += "#";
}
for (i = 0; i < endDecimal; i++) {
endStr += "#";
}
decimalStr = beginStr;
if (!"".equals(endStr)) {
decimalStr = decimalStr + "." + endStr;
}
try {
returnValStr = new DecimalFormat(decimalStr).format(val).toString();
} catch (Exception ex) {
returnValStr = "0";
}
if (bool) {
double returnVal = 0;
try {
returnVal = Double.parseDouble(returnValStr);
} catch (Exception ex) {
returnVal = 0;
}
if (returnVal > 0 && returnVal < 1) {
return "1";
} else {
return Double.toString(returnVal);
}
} else {
return returnValStr;
}
}
/** 去除HTML标签 */
public static String getDeleteHTMLTab(String str) {
if ("".equals(str)) {
return "";
}
int i = 0, j = 0;
String s1 = str;
String s2 = "";
String returnString = "";
while (!"".equals(s1.trim())) {
i = s1.indexOf("<");
if (i > 0) {
s2 = s1.substring(0, i);
returnString = returnString + s2;
}
s1 = s1.substring(i + 1);
j = s1.indexOf(">");
if (i < 0) {
returnString += s1;
break;
}
s1 = s1.substring(j + 1);
}
return returnString.trim();
}
/** 过滤HTML标签 */
public static String getFilteHTML(String _str) {
if (_str == null || "".equals(_str)) {
return "";
}
String str = _str;
str = str.replace("<", "<");
str = str.replace(">", ">");
return str;
}
/** 替换常用的HTML字符 */
public static String getReplaceHTMLStr(String _str) {
if (_str == null || "".equals(_str)) {
return "";
}
String str = _str;
str = str.replace("\r\n", "<br/>");
str = str.replace("\n", "<br/>");
return str;
}
/** 过滤非法SQL字符 防止T-SQL语句注入 */
public static String getFiltNonStr(String _str) {
if (_str == null || "".equals(_str)) {
return "";
}
String str = _str;
str = str.replace("'", "''");
return str;
}
/**
* 执行传入的T-SQL语句, 用于插入,删除,修改等
*
* @param executeSql
* 执行SQL语句
* @return 返回影响行数
*/
public static int setDBExecuteSQL(String executeSql) {
Connection conn = null;
Statement st = null;
conn = DBConn.getConnection(); // 获取DB连接
try {
st = conn.createStatement();
return st.executeUpdate(executeSql);
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nException: AT com.szmsd.util.Const.setDBExecuteSQL()!"+e.getMessage());
return 0;
} finally {
try {
st.close();
} catch (Exception e) {
}
try {
conn.close();
} catch (Exception e) {
}
}
}
/**
* 获取数据库表的某一行单个值, 根据传入的查询语句与所需要的键
*
* @param selectSql
* 查询语句
* @param key
* 键
* @return 指定键的值
*/
public static String getOneRowValue(String selectSql, String key) {
String oneStrValue = "";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
conn = DBConn.getConnection(); // 获取DB连接
try {
st = conn.createStatement();
rs = st.executeQuery(selectSql);
if (rs.next()) {
oneStrValue = Const.getStr(rs.getString(key));
}
return oneStrValue;
} catch (Exception e) {
System.out.println("\nException: AT com.szmsd.util.Const.getOneRowValue()!");
return "";
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
st.close();
} catch (Exception e) {
}
try {
conn.close();
} catch (Exception e) {
}
}
}
/**
* 获取结果集的总行数
*
* @param rs
* ResultSet 结果集
* @return totalCount 总行数
*/
public static int getResultSetCount(ResultSet rs) {
int totalCount = 0;
try {
if (rs != null) {
rs.last();
totalCount = rs.getRow();
rs.beforeFirst();
} else {
return 0;
}
} catch (Exception e) {
System.out.println("\nException: AT com.szmsd.util.Const.getResultSetCount()!");
return 0;
} finally {
try {
rs.beforeFirst();
} catch (Exception e) {
System.out.println("\nException: AT com.szmsd.util.Const.getResultSetCount() rs.beforeFirst()>>!");
}
}
return totalCount;
}
/**
* 获取数据集的总行数 根据传入的表名与条件查询
*
* @param tableName
* 表名
* @param col
* count 字段
* @param where
* 条件
* @return totalCount 总行数
*/
public static int getResuleSetCount(String tableName, String col, String where) {
int totalCount = 0;
Connection conn = null;
ResultSet rs = null;
Statement st = null;
conn = DBConn.getConnection();// 获取DB连接
String sql = "";
try {
sql = "select count(" + col + ") AS c from " + tableName + " " + where;
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
totalCount = rs.getInt("c");
}
return totalCount;
} catch (Exception e) {
System.out.println(sql);
System.out.println("\nException: AT com.szmsd.util.Const.getResuleSetCount()!");
return 0;
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
st.close();
} catch (Exception e) {
}
try {
conn.close();
} catch (Exception e) {
}
}
}
/**
* 获取数据集的总行数 根据传入的表名与条件查询
*
* @param tableName
* 表名
* @param col
* count 字段
* @param where
* 条件
* @return totalCount 总行数
*/
public static int getResuleSetSum(String tableName, String col, String where) {
int totalCount = 0;
Connection conn = null;
ResultSet rs = null;
Statement st = null;
conn = DBConn.getConnection();// 获取DB连接
String sql = "";
try {
sql = "select sum(" + col + ") AS c from " + tableName + " " + where;
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
totalCount = rs.getInt("c");
}
return totalCount;
} catch (Exception e) {
System.out.println(sql);
System.out.println("\nException: AT com.szmsd.util.Const.getResuleSetSum()!");
return 0;
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
st.close();
} catch (Exception e) {
}
try {
conn.close();
} catch (Exception e) {
}
}
}
/**
* 获取数据集的总行数 根据传入的表名与条件查询, 带分组
*
* @param tableName
* 表名
* @param col
* count(字段)
* @param where
* 条件
* @param groupby
* 分组
* @return totalCount 总行数
*/
public static int getResuleSetCount(String tableName, String col, String where, String groupby) {
int totalCount = 0;
Connection conn = null;
ResultSet rs = null;
Statement st = null;
conn = DBConn.getConnection(); // 获取DB连接
String sql = "";
try {
sql = "select count(t.c) AS c from (select " + col + " AS c from " + tableName + " " + where + " " + groupby + ") t";
st = conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
totalCount = rs.getInt("c");
}
return totalCount;
} catch (Exception e) {
System.out.println("\nException: AT com.szmsd.util.Const.getResuleSetCount()!");
return 0;
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
st.close();
} catch (Exception e) {
}
try {
conn.close();
} catch (Exception e) {
}
}
}
/**
* 屏蔽非法关键字 从系统配置表 sys_config 取出 bad_keyword 键的值
*
* @param str
* @return
*/
public static String getScreenKey(String _str) {
if (_str == null || "".equals(_str)) {
return "";
}
String str = _str;
// String keyword=getOneRowValue("select cfg_value from sys_config where
// cfg_key='bad_keyword'","cfg_value");
String keyword = "77元;房租77元;钓鱼岛;法轮;falun;falundafa;zhuanfalu;六四;";
String[] key = keyword.split(";");
String xing = "";
int i = 0, j = 0, total1 = 0, total2 = 0;
for (i = 0, total1 = key.length; i < total1; i++) {
xing = "";
for (j = 0, total2 = key[i].length(); j < total2; j++) {
xing += "*";
}
str = str.replaceAll(key[i], xing);
}
return str;
}
// 获取当前网站访问的路径
public static String getBasePath(HttpServletRequest request) {
String basePath = request.getScheme() + "://";
basePath += request.getHeader("host");
basePath += request.getRequestURI();
if (request.getQueryString() != null)
basePath += "?" + request.getQueryString();
return basePath;
}
public static String getStrToGMT(String str) {
// String source = "Sat Mar 30 2013 08:45:00 GMT+0800 (中国标准时间)"; //Sat
// Mar 30 2013 08:45:00 GMT+0800 (中国标准时间)
String dest = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dest = format.format(Date.parse(str));
return dest;
}
public static long dateToLong(String in) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(in);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getTimeInMillis();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
/**
* 将timestamp转换成date
*
* @author hellostoy
* @param tt
* @return
*/
public static Date timestampToDate(Timestamp tt) {
return new Date(tt.getTime());
}
public static void main(String[] args) {
System.out.println(Const.dateToLong("2014-04-14 15:04:09")/1000);
Const util = new Const();
System.out.println(util.tempuser);
}
}
2:AlipayConfig.java
public class AlipayConfig {
private static AlipayConfig alconfig = null;
private AlipayConfig(){
}
public static AlipayConfig getInstance(){
if(alconfig==null){
alconfig = new AlipayConfig();
}
return alconfig;
}
// 如何获取安全校验码和合作身份者ID
// 1.访问支付宝商户服务中心(b.alipay.com),然后用您的签约支付宝账号登陆.
// 2.访问“技术服务”→“下载技术集成文档”(https://b.alipay.com/support/helperApply.htm?action=selfIntegration)
// 3.在“自助集成帮助”中,点击“合作者身份(Partner ID)查询”、“安全校验码(Key)查询”
// ↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 合作身份者ID,以2088开头由16位纯数字组成的字符串
public static String it_b_pay = "1h";
public static String partner = "2088601003079118";
public static String service = "create_direct_pay_by_user";
// 交易安全检验码,由数字和字母组成的32位字符串
public static String key = "zxcdvxgksaam2zjrmv5cv0p4jqesaioh";
// 签约支付宝账号或卖家收款支付宝帐户
public static String seller_email = "test@yahoo.com.cn";
// 读配置文件
// notify_url 交易过程中服务器通知的页面 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
public static String notify_url ="http:www.xxx.com/projectName/alipayTrade.action";
// 付完款后跳转的页面 要用 http://格式的完整路径,不允许加?id=123这类自定义参数
// return_url的域名不能写成http://localhost/js_jsp_utf8/return_url.jsp,否则会导致return_url执行无效
//public static String return_url = "http:www.xxx.com/projectName/alipayTrade.action";
// 网站商品的展示地址,不允许加?id=123这类自定义参数
public static String show_url = "http://www.alipay.com";
// 收款方名称,如:公司名称、网站名称、收款人姓名等
public static String mainname = "收款方名称";
// ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
// 字符编码格式 目前支持 gbk 或 utf-8
public static String input_charset = "UTF-8";
// 签名方式 不需修改
public static String sign_type = "MD5";
// 访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http
public static String transport = "http";
}
3:Md5Encrypt.java
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Encrypt {
/**
* Used building output as Hex
*/
private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 对字符串进行MD5加密
*
* @param text
* 明文
*
* @return 密文
*/
public static String md5(String text) {
MessageDigest msgDigest = null;
try {
msgDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"System doesn't support MD5 algorithm.");
}
try {
msgDigest.update(text.getBytes(AlipayConfig.input_charset)); // 注意改接口是按照指定编码形式签名
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(
"System doesn't support your EncodingException.");
}
byte[] bytes = msgDigest.digest();
String md5Str = new String(encodeHex(bytes));
return md5Str;
}
public static char[] encodeHex(byte[] data) {
int l = data.length;
char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
out[j++] = DIGITS[0x0F & data[i]];
}
return out;
}
public static void main(String[] args) {
System.out.println(md5("msd"));
}
}
4:PropertiesUtils.java
mport java.io.Serializable;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PropertiesUtils implements Serializable {
private static final long serialVersionUID = -3095271558987987812L;
private static final Log logger = LogFactory.getLog(PropertiesUtils.class);
private static final String CONFIG_FILE = "logistics.properties";
private static PropertiesConfiguration pc;
private PropertiesUtils() {
}
public synchronized static PropertiesConfiguration getInstance() {
if (pc != null) {
return pc;
}
try {
pc = new PropertiesConfiguration(CONFIG_FILE);
} catch (ConfigurationException cexception) {
logger.error("read properties file error.", cexception);
throw new RuntimeException(cexception);
}
return pc;
}
}