
com.mycomm.YesHttp.comm.SystemUtil Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycomm.YesHttp.comm;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.regex.Pattern;
import java.io.File;
import java.io.FileInputStream;
import java.io.Reader;
/**
*
* @author jw362j
*/
public class SystemUtil {
private static final String default_charSet = "UTF-8";
public static boolean isPasswordValid(String password) {
boolean result = true;
if ("".equals(password) || password == null) {
return false;
}
if (password.length() < 6) {
return false;
}
if (password.startsWith("asd")) {
result = false;
}
return result;
}
/**
* 将图片文件转化为字符串,并对其进行Base64编码处理
*
* @param ip
* @return macAddress
*/
public static String getMACAddress(String ip) {
String str;
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i < 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") > 1) {
macAddress = str.substring(
str.indexOf("MAC Address") + 14, str.length());
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
public static String getMD5(byte[] source) {
String sour = new String(source);
// log.info("the input parameter for md5 is:"+sour);
String s = null;
char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update(source);
byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数,
// 用字节表示就是 16 个字节
char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
// 所以表示成 16 进制需要 32 个字符
int k = 0; // 表示转换结果中对应的字符位置
for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
// 转换成 16 进制字符的转换
byte byte0 = tmp[i]; // 取第 i 个字节
str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
// >>> 为逻辑右移,将符号位一起右移
str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
}
s = new String(str); // 换后的结果转换为字符串
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
public static String ReadFile(String fileFullPath, String charSet, String[] allowed_file_postfix_list) {
String extentsion = getExtensionName(fileFullPath);
extentsion = extentsion.toLowerCase();
if (!isContainItem(extentsion, allowed_file_postfix_list)) {
return "bad file extention type!";
}
return ReadFromFile(fileFullPath, charSet);
}
public static String ReadFromFile(String fileFullPath, String charSet) {
charSet = (charSet == null || "".equals(charSet)) ? default_charSet : charSet;
StringBuilder sb = new StringBuilder();
if ("".equals(fileFullPath) || fileFullPath == null) {
sb.append("文件不存在:").append(fileFullPath);
return sb.toString();
}
File file = new File(fileFullPath);
if (!file.exists()) {
sb.append("文件不存在:").append(fileFullPath);
return sb.toString();
}
Reader reader = null;
try {
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(fileFullPath), charSet);
int tempchar;
while ((tempchar = reader.read()) != -1) {
sb.append((char) tempchar);
}
reader.close();
} catch (Exception e) {
sb.append("file error:").append(e.getMessage());
}
if ("".equals(sb.toString())) {
return "file content is empty!";
}
return sb.toString();
}
public static boolean isContainItem(String filename, String[] items) {
boolean result = false;
if (items == null || items.length < 1) {
return result;
}
for (String name : items) {
if (name.equals(filename)) {
result = true;
break;
}
}
return result;
}
/*
* Java文件操作 获取文件扩展名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/*
* Java文件操作 获取不带扩展名的文件名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String getFileNameNoPostFix(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
public static String getQueryStringItem(String qstring, String keyItem) {
String result = null;
if ("".equals(qstring) || qstring == null || "".equals(keyItem) || keyItem == null) {
return result;
}
String[] qs = qstring.split("&");
if (qs == null || qs.length < 1) {
return result;
}
for (String s : qs) {
String[] s_ = s.split("=");
if (s_ != null && s_.length == 2) {
if (keyItem.equals(s_[0])) {
result = s_[1];
break;
}
}
}
return result;
}
public static boolean isNumberString(String target) {
boolean result = false;
if ("".equals(target) || target == null) {
return result;
}
return Pattern.compile("[0-9]*").matcher(target).matches();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
public static String byteArraytoHex(byte[] buf) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
/*
* 中文转unicode编码
*/
public static String cn2Unicode(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int i = 0; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
/*
* unicode编码转中文
unicode编码必须为6位长度 如果不足则在前面补0 ,如4E03 补为004E03
*/
public static String Unicode2cn(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
private static boolean isChineseCharacter(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
return true;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy