package com.zxwl.common.util;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
public class StringUtils
{
public static boolean isEmpty(String str)
{
if (null == str || str.trim().length() == 0 || "".equals(str.trim()))
{
return true;
}
return false;
}
public static boolean isValue(String str)
{
return !isEmpty(str);
}
/**
* 日期加操作,返回计算后的日期
*/
public static String addDate(String dateTime, int n)
{
SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
try {
date = dd.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calstart = Calendar.getInstance();
calstart.setTime(date);
calstart.add(Calendar.DAY_OF_MONTH, n);
return dd.format(calstart.getTime());
}
/**
* 处理拼接sql时注入单引号的问题
*/
public static String inSql(String value) {
if (null == value) {
return "";
} else {
return value.replace("'", "''");
}
}
/**
* object转换string
* @param value
* @return
*/
public static String object2String(Object value)
{
if(null==value){
return null;
}else{
return value.toString();
}
}
/**
* 获取客户端的真实IP地址
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request)
{
String ipAddress = null;
ipAddress = request.getHeader("x-forwarded-for");
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
{
ipAddress = request.getHeader("Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
{
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress))
{
ipAddress = request.getRemoteAddr();
if(ipAddress.equals("0:0:0:0:0:0:0:1"))
{
ipAddress = "127.0.0.1";
}
if(ipAddress.equals("127.0.0.1"))
{
//根据网卡取本机配置的IP
InetAddress inet=null;
try
{
inet = InetAddress.getLocalHost();
}catch (UnknownHostException e)
{
e.printStackTrace();
}
ipAddress= inet.getHostAddress();
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if(ipAddress!=null && ipAddress.length()>15)
{ //"***.***.***.***".length() = 15
if(ipAddress.indexOf(",")>0)
{
ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
}
}
}
}
return ipAddress;
}
/**
* 日期加操作,返回计算后的日期
*/
public static Date formatDate(String dateTime)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = new Date();
try
{
startTime = sdf.parse(dateTime);
}
catch (ParseException e)
{
e.printStackTrace();
}
return startTime;
}
/**
* xml特殊字符串处理
* @param str
* @return
*/
public static String replaceSpecialChar(String str){
if(str==null || "".equals(str)){
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++)
{
char c = str.toCharArray()[i]; // str转换为字节数组
switch (c)
{
case '<' :
sb.append("<");
break;
case '>' :
sb.append(">");
break;
case '&' :
sb.append("&");
break;
case '"' :
sb.append(""");
break;
case '\'' :
sb.append("'");
break;
default :
sb.append(c);
break;
}
}
return sb.toString();
}
/**
* 是否数字
*/
public static boolean isNum(String value) {
try{
Integer.parseInt(value);
return true;
}catch(Exception e){
return false;
}
}
}
StringUtils
最新推荐文章于 2023-03-21 11:38:19 发布