package com.book.utils;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Date;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author 张代浩
*
*/
public class oConvertUtils {
public static boolean isEmpty(Object object) {
if (object == null) {
return (true);
}
if (object.equals("")) {
return (true);
}
if (object.equals("null")) {
return (true);
}
return (false);
}
public static boolean isNotEmpty(Object object) {
if (object != null && !object.equals("") && !object.equals("null")) {
return (true);
}
return (false);
}
public static String decode(String strIn, String sourceCode, String targetCode) {
String temp = code2code(strIn, sourceCode, targetCode);
return temp;
}
public static String StrToUTF(String strIn, String sourceCode, String targetCode) {
strIn = "";
try {
strIn = new String(strIn.getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strIn;
}
private static String code2code(String strIn, String sourceCode, String targetCode) {
String strOut = null;
if (strIn == null || (strIn.trim()).equals(""))
return strIn;
try {
byte[] b = strIn.getBytes(sourceCode);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
strOut = new String(b, targetCode);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return strOut;
}
public static int getInt(String s, int defval) {
if (s == null || s == "") {
return (defval);
}
try {
return (Integer.parseInt(s));
} catch (NumberFormatException e) {
return (defval);
}
}
public static int getInt(String s) {
if (s == null || s == "") {
return 0;
}
try {
return (Integer.parseInt(s));
} catch (NumberFormatException e) {
return 0;
}
}
public static int getInt(String s, Integer df) {
if (s == null || s == "") {
return df;
}
try {
return (Integer.parseInt(s));
} catch (NumberFormatException e) {
return 0;
}
}
public static Integer[] getInts(String[] s) {
Integer[] integer = new Integer[s.length];
if (s == null) {
return null;
}
for (int i = 0; i < s.length; i++) {
integer[i] = Integer.parseInt(s[i]);
}
return integer;
}
public static double getDouble(String s, double defval) {
if (s == null || s == "") {
return (defval);
}
try {
return (Double.parseDouble(s));
} catch (NumberFormatException e) {
return (defval);
}
}
public static double getDou(Double s, double defval) {
if (s == null) {
return (defval);
}
return s;
}
public static int getInt(Object object, int defval) {
if (isEmpty(object)) {
return (defval);
}
try {
return (Integer.parseInt(object.toString()));
} catch (NumberFormatException e) {
return (defval);
}
}
public static int getInt(BigDecimal s, int defval) {
if (s == null) {
return (defval);
}
return s.intValue();
}
public static Integer[] getIntegerArry(String[] object) {
int len = object.length;
Integer[] result = new Integer[len];
try {
for (int i = 0; i < len; i++) {
result[i] = new Integer(object[i].trim());
}
return result;
} catch (NumberFormatException e) {
return null;
}
}
public static String getString(String s) {
return (getString(s, ""));
}
public static String getString(Object object) {
if (isEmpty(object)) {
return "";
}
return (object.toString().trim());
}
public static String getString(int i) {
return (String.valueOf(i));
}
public static String getString(float i) {
return (String.valueOf(i));
}
public static String getString(String s, String defval) {
if (isEmpty(s)) {
return (defval);
}
return (s.trim());
}
public static String getString(Object s, String defval) {
if (isEmpty(s)) {
return (defval);
}
return (s.toString().trim());
}
public static long stringToLong(String str) {
Long test = new Long(0);
try {
test = Long.valueOf(str);
} catch (Exception e) {
}
return test.longValue();
}
/**
* 获取本机IP
*/
public static String getIp() {
String ip = null;
try {
InetAddress address = InetAddress.getLocalHost();
ip = address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip;
}
/**
* 判断一个类是否为基本数据类型。
*
* @param clazz
* 要判断的类。
* @return true 表示为基本数据类型。
*/
private static boolean isBaseDataType(Class clazz) throws Exception {
return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive());
}
/**
* @param request
* IP
* @return IP Address
*/
public static String getIpAddrByRequest(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* @return 本机IP
* @throws SocketException
*/
public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
/**
* java去除字符串中的空格、回车、换行符、制表符
*
* @param str
* @return
*/
public static String replaceBlank(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 判断元素是否在数组内
*
* @param substring
* @param source
* @return
*/
public static boolean isIn(String substring, String[] source) {
if (source == null || source.length == 0) {
return false;
}
for (int i = 0; i < source.length; i++) {
String aSource = source[i];
if (aSource.equals(substring)) {
return true;
}
}
return false;
}
/**
* �
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaWeb(Servlet+Mybatis+Thymeleaf)的在线学习考试系统 (783个子文件)
DBWrapper.class 12KB
DBWrapper.class 12KB
BookServiceImpl.class 11KB
BookServiceImpl.class 11KB
oConvertUtils.class 10KB
oConvertUtils.class 10KB
ProjectInitializer.class 6KB
ProjectInitializer.class 6KB
UserServiceImpl.class 5KB
UserServiceImpl.class 5KB
KaoshiServlet.class 5KB
KaoshiServlet.class 5KB
doExamView.class 5KB
doExamView.class 5KB
Paper.class 4KB
Paper.class 4KB
Log.class 4KB
Log.class 4KB
Question.class 4KB
Question.class 4KB
Tiezi.class 4KB
Tiezi.class 4KB
Zuoye.class 4KB
Zuoye.class 4KB
AddZuoyeServlet.class 4KB
AddZuoyeServlet.class 4KB
TiebaServiceImpl.class 4KB
TiebaServiceImpl.class 4KB
LoginServlet.class 4KB
LoginServlet.class 4KB
MessageServiceImpl.class 4KB
MessageServiceImpl.class 4KB
AddFileListServlet.class 4KB
AddFileListServlet.class 4KB
FileList.class 4KB
FileList.class 4KB
FileListServlet.class 3KB
FileListServlet.class 3KB
AddUserServlet.class 3KB
AddUserServlet.class 3KB
AbstractBaseView.class 3KB
AbstractBaseView.class 3KB
BookMapper.class 3KB
BookMapper.class 3KB
AddTiebaServlet.class 3KB
AddTiebaServlet.class 3KB
AddMessageServlet.class 3KB
AddMessageServlet.class 3KB
User.class 3KB
User.class 3KB
Borrow.class 3KB
Borrow.class 3KB
Message.class 3KB
Message.class 3KB
TieziServlet.class 3KB
TieziServlet.class 3KB
MessageServlet.class 3KB
MessageServlet.class 3KB
AddliuyanServlet.class 3KB
AddliuyanServlet.class 3KB
IndexServlet.class 3KB
IndexServlet.class 3KB
DatabaseConfig.class 3KB
DatabaseConfig.class 3KB
DyKechengServiceImpl.class 3KB
DyKechengServiceImpl.class 3KB
ZuoyeServlet.class 3KB
ZuoyeServlet.class 3KB
LogsServlet.class 3KB
LogsServlet.class 3KB
AddDYKCServlet.class 3KB
AddDYKCServlet.class 3KB
boradListServlet.class 3KB
boradListServlet.class 3KB
TiebaServlet.class 3KB
TiebaServlet.class 3KB
registerListServlet.class 2KB
registerListServlet.class 2KB
Book.class 2KB
Book.class 2KB
Role.class 2KB
Role.class 2KB
DyKecheng.class 2KB
DyKecheng.class 2KB
Student.class 2KB
Student.class 2KB
LogsListServlet.class 2KB
LogsListServlet.class 2KB
BookServlet.class 2KB
BookServlet.class 2KB
AddBorrowServlet.class 2KB
AddBorrowServlet.class 2KB
DyKechengServlet.class 2KB
DyKechengServlet.class 2KB
Type.class 2KB
Type.class 2KB
UserServlet.class 2KB
UserServlet.class 2KB
UserMapper.class 2KB
UserMapper.class 2KB
共 783 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
锦鲤锦鲤jl
- 粉丝: 65
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功