package com.ruoyi.common.utils.netty;
|
|
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.*;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.util.*;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
|
public class NettyTool {
|
|
/**
|
* 每找到一个“ABCD”,就将字符串截取,直到找到第三个,将每次下标累加即可。
|
*
|
* @param str1
|
* @param str2
|
* @return
|
*/
|
public static Integer getIndex(String str1, String str2) {
|
int num = 0;
|
int location = 0;
|
|
int index = str1.indexOf(str2);
|
while (-1 != str1.indexOf(str2)) {
|
location += index;
|
|
num++;
|
if (3 == num) {
|
break;
|
} else {
|
str1 = str1.substring(index);
|
index = str1.indexOf(str2, 1);
|
}
|
}
|
|
return location;
|
}
|
|
public static List<Integer> getIndexs(String str1, String str2) {
|
|
int length = str2.length();
|
|
int index;
|
int location = 0;
|
List<Integer> indexs = new ArrayList<>();
|
while (-1 != str1.indexOf(str2)) {
|
index = str1.indexOf(str2);
|
|
indexs.add(location + index);
|
|
index = index + length;
|
|
location = location + index;
|
|
str1 = str1.substring(index);
|
|
}
|
return indexs;
|
}
|
|
|
|
|
|
public static String readCityFile(MultipartFile multipartFile) {
|
StringBuilder stringBuilder = null;
|
try {
|
if (multipartFile!=null) {
|
InputStream bb = multipartFile.getInputStream();
|
InputStreamReader streamReader = new InputStreamReader(bb);
|
BufferedReader reader = new BufferedReader(streamReader);
|
String line;
|
stringBuilder = new StringBuilder();
|
while ((line = reader.readLine()) != null) {
|
stringBuilder.append(line);
|
}
|
reader.close();
|
bb.close();
|
} else {
|
stringBuilder.append("空的");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return String.valueOf(stringBuilder);
|
}
|
|
//对找map下表
|
public static int Selectsubscript(Object[] keys, String xiabiao) {
|
//遍历key 找到
|
Integer mapkey1 = Integer.parseInt(xiabiao,16);
|
String mapkey="@"+Integer.toHexString(mapkey1).toUpperCase();
|
int a = 0;
|
for (int i = 0; i < ChannelHandlerContextCache.keys.length; i++) {
|
String data1=String.valueOf(ChannelHandlerContextCache.keys[i]);
|
if (data1.equals( mapkey)) {
|
a = i;
|
}
|
}
|
return a;
|
}
|
|
public static int Calculatepagenumber(Map<String, List<String>> cachepage) {
|
int a = 0;
|
for (List<String> value : cachepage.values()) {
|
int a1 = value.size();
|
a = a + a1;
|
}
|
return a;
|
}
|
|
//计算块数量
|
public static Integer Numberofblocks(Map<String, List<String>> cachepage) {
|
int a = 0;
|
for (String key : cachepage.keySet()) {
|
a = a + 1;
|
}
|
|
return a;
|
}
|
|
//向右补位
|
public static String addstrfFForNum(String str, int strLength) {
|
int strLen = str.length();
|
if (strLen < strLength) {
|
while (strLen < strLength) {
|
StringBuffer sb = new StringBuffer();
|
//sb.append("f").append(str);// 左补0
|
sb.append(str).append("f");//右补0
|
str = sb.toString();
|
strLen = str.length();
|
}
|
}
|
return str;
|
}
|
|
|
|
//向左补位
|
public static String addleftForNum(String str, int strLength) {
|
int strLen = str.length();
|
if (strLen < strLength) {
|
while (strLen < strLength) {
|
StringBuffer sb = new StringBuffer();
|
sb.append("0").append(str);// 左补0
|
// sb.append(str).append("f");//右补0
|
str = sb.toString();
|
strLen = str.length();
|
}
|
}
|
return str;
|
}
|
|
|
/**
|
* 把原始字符串分割成指定长度的字符串列表
|
*
|
* @param inputString 原始字符串
|
* @param length 指定长度
|
* @return
|
*/
|
public static List<String> getStrList(String inputString, int length) {
|
int size = inputString.length() / length;
|
if (inputString.length() % length != 0) {
|
size += 1;
|
}
|
return getStrList(inputString, length, size);
|
}
|
|
/**
|
* 把原始字符串分割成指定长度的字符串列表
|
*
|
* @param inputString 原始字符串
|
* @param length 指定长度
|
* @param size 指定列表大小
|
* @return
|
*/
|
public static List<String> getStrList(String inputString, int length,
|
int size) {
|
List<String> list = new ArrayList<String>();
|
for (int index = 0; index < size; index++) {
|
String childStr = substring(inputString, index * length,
|
(index + 1) * length);
|
list.add(childStr);
|
}
|
return list;
|
}
|
|
|
/**
|
* 分割字符串,如果开始位置大于字符串长度,返回空
|
*
|
* @param str 原始字符串
|
* @param f 开始位置
|
* @param t 结束位置
|
* @return
|
*/
|
public static String substring(String str, int f, int t) {
|
if (f > str.length()){
|
return null;
|
}
|
|
if (t > str.length()) {
|
return str.substring(f, str.length());
|
} else {
|
return str.substring(f, t);
|
}
|
}
|
|
|
|
/*开始编写 将map开始拆分*/
|
public static Map<String, List<String>> cachepage(Map<String, String> filemap) {
|
//开始遍历 map
|
Map<String, List<String>> cachepage = new LinkedHashMap<>();
|
List<String> lists = null;
|
for (String key : filemap.keySet()) {
|
System.out.println("Key = " + key);
|
//根据kay 获取到map
|
String a = filemap.get(key);
|
if (a.length() > 1024) {
|
//如果大于则开始分段
|
lists = getStrList(a, 1024);
|
cachepage.put(key, lists);
|
} else {
|
cachepage.put(key, Arrays.asList(a.split(",")));
|
}
|
}
|
return cachepage;
|
}
|
|
/**
|
* 读取File文件的信息内容
|
*
|
* @param file
|
* @return
|
*/
|
public static String txt2String(File file) {
|
String result = "";
|
try {
|
//构造一个BufferedReader类来读取文件
|
BufferedReader br = new BufferedReader(new FileReader(file));
|
String s = null;
|
//使用readLine方法,一次读一行
|
while ((s = br.readLine()) != null) {
|
result = result + "\n" + s;
|
}
|
br.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
public static Map<String, String> xiabiao(String str) {
|
//第一种方法
|
String st1 = str;
|
int frontLength = 0;//定义该变量用于记录匹配"love"的元素前面的长度
|
ArrayList<Integer> lists = new ArrayList();
|
while (str.contains("@")) {//只要该str字符串中有匹配"love"的元素,才进行以下操作
|
int index = str.indexOf("@");//定义该变量用于记录匹配"love"的元素在当前字符串的位置
|
//匹配"love"的元素位置等于frontLength加上index;加1为了从1开始计数,更加直观:
|
System.out.println(String.format("str[%d] = @", index + frontLength + 1));
|
lists.add(index + frontLength);
|
frontLength += (index + 5);
|
str = str.substring(index + 5);//将字符串中匹配"love"元素的前面部分及其本身截取,留下后面的部分
|
}
|
String value = "";
|
String key = "";
|
Map<String, String> fileMpa = new LinkedHashMap<>();
|
//开始对 list 遍历并且添加到 map 中去
|
int ss = st1.length();
|
for (int i = 0; i < lists.size(); i++) {
|
if (i == lists.size() - 1) {
|
value = st1.substring(lists.get(i) + 5, st1.length() - 1);
|
key = st1.substring(lists.get(i), lists.get(i) + 5);
|
|
|
} else {
|
key = st1.substring(lists.get(i), lists.get(i) + 5);
|
value = st1.substring(lists.get(i) + 5, lists.get(i + 1));
|
}
|
fileMpa.put(key, value);
|
}
|
return fileMpa;
|
}
|
|
|
public static Map<String, String> xiabiao_new (String str, List<String> kuai1List) {
|
Map<String, String> fileMpa = new LinkedHashMap<>();
|
for (int i = 0; i < kuai1List.size(); i++) {
|
String key;
|
String value;
|
|
if (i == kuai1List.size() - 1) {
|
key = kuai1List.get(i);
|
value = str.substring(str.indexOf(kuai1List.get(i))+kuai1List.get(i).length(), str.length()-1);
|
} else {
|
key = kuai1List.get(i);
|
value = str.substring(str.indexOf(kuai1List.get(i))+kuai1List.get(i).length(), str.indexOf(kuai1List.get(i+1)));
|
}
|
fileMpa.put(key, value);
|
}
|
return fileMpa;
|
|
}
|
|
|
//获取主机的当前的时间
|
public static String getDateTime() {
|
Calendar cal = Calendar.getInstance();
|
int y = cal.get(Calendar.YEAR);
|
String year = Integer.toString(y - 2000);
|
int m = cal.get(Calendar.MONTH) + 1;
|
String month = String.format("%02d", m);
|
int d = cal.get(Calendar.DATE);
|
String date = String.format("%02d", d);
|
int h = cal.get(Calendar.HOUR_OF_DAY);
|
String HOUR = String.format("%02d", h);
|
int mi = cal.get(Calendar.MINUTE);
|
String MINUTE = String.format("%02d", mi);
|
int s = cal.get(Calendar.SECOND);
|
String SECOND = String.format("%02d", s);
|
String data = year + month + date + HOUR + MINUTE + SECOND;
|
return data;
|
}
|
|
|
//计算CRC16校验码 //低位在前 高位在后
|
public static String getCRC(String data) {
|
data = data.replace(" ", "");
|
int len = data.length();
|
if (!(len % 2 == 0)) {
|
return "0000";
|
}
|
int num = len / 2;
|
byte[] para = new byte[num];
|
for (int i = 0; i < num; i++) {
|
int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
|
para[i] = (byte) value;
|
}
|
return getCRC(para);
|
}
|
|
/**
|
* 计算CRC16校验码
|
*
|
* @param bytes 字节数组
|
* @return {@link String} 校验码
|
* @since 1.0
|
*/
|
public static String getCRC(byte[] bytes) {
|
// CRC寄存器全为1
|
int CRC = 0x0000ffff;
|
// 多项式校验值
|
int POLYNOMIAL = 0x0000a001;
|
int i, j;
|
for (i = 0; i < bytes.length; i++) {
|
CRC ^= ((int) bytes[i] & 0x000000ff);
|
for (j = 0; j < 8; j++) {
|
if ((CRC & 0x00000001) != 0) {
|
CRC >>= 1;
|
CRC ^= POLYNOMIAL;
|
} else {
|
CRC >>= 1;
|
}
|
}
|
}
|
|
// 结果转换为16进制
|
String result = Integer.toHexString(CRC).toUpperCase();
|
if (result.length() != 4) {
|
StringBuffer sb = new StringBuffer("0000");
|
result = sb.replace(4 - result.length(), 4, result).toString();
|
}
|
//低位在前 高位在后
|
return result.substring(2, 4) + result.substring(0, 2);
|
// 高位为在前 低位在后
|
// return result.substring(0, 2)+ result.substring(2, 4)
|
// return (result.substring(0, 2)+ result.substring(2, 4)).toLowerCase();
|
}
|
|
|
// 高位为在前 低位在后
|
|
public static String getCRChigh(String data) {
|
data = data.replace(" ", "");
|
int len = data.length();
|
if (!(len % 2 == 0)) {
|
return "0000";
|
}
|
int num = len / 2;
|
byte[] para = new byte[num];
|
for (int i = 0; i < num; i++) {
|
int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
|
para[i] = (byte) value;
|
}
|
return getCRChigh(para);
|
}
|
|
/**
|
* 计算CRC16校验码
|
*
|
* @param bytes 字节数组
|
* @return {@link String} 校验码
|
* @since 1.0
|
*/
|
public static String getCRChigh(byte[] bytes) {
|
// CRC寄存器全为1
|
int CRC = 0x0000ffff;
|
// 多项式校验值
|
int POLYNOMIAL = 0x0000a001;
|
int i, j;
|
for (i = 0; i < bytes.length; i++) {
|
CRC ^= ((int) bytes[i] & 0x000000ff);
|
for (j = 0; j < 8; j++) {
|
if ((CRC & 0x00000001) != 0) {
|
CRC >>= 1;
|
CRC ^= POLYNOMIAL;
|
} else {
|
CRC >>= 1;
|
}
|
}
|
}
|
|
// 结果转换为16进制
|
String result = Integer.toHexString(CRC).toUpperCase();
|
if (result.length() != 4) {
|
StringBuffer sb = new StringBuffer("0000");
|
result = sb.replace(4 - result.length(), 4, result).toString();
|
}
|
//低位在前 高位在后
|
// return result.substring(2, 4) + result.substring(0, 2);
|
// 高位为在前 低位在后
|
return result.substring(0, 2) + result.substring(2, 4);
|
// return (result.substring(0, 2)+ result.substring(2, 4)).toLowerCase();
|
}
|
|
|
//获取字节的长度 并将其修改成16位数
|
public static String Hexlength(String Hexstr) {
|
|
int a = Hexstr.length() / 2;
|
String lenght = ConvertCode.intToHexString(a, 1);
|
|
return lenght;
|
}
|
|
|
//对数据 进行截取
|
public static String InterceptController(String requestData) {
|
return requestData.substring(20, 22);
|
}
|
|
|
//控制器
|
public static String getController(String data) {
|
|
return data.substring(6, 16);
|
}
|
|
|
//密码
|
public static String getPassword(String requestData) {
|
|
String data = requestData.substring(16, 20);
|
return data;
|
}
|
|
//固定
|
public static String getfixed() {
|
String data = "2F";
|
return data;
|
}
|
|
/**
|
* 时间日期转换
|
*
|
* @param strDate 字符串yyyyMMddHHmmss
|
* @return 字符串yyyy-MM-dd HH:mm:ss 2019 06 19 12 00
|
* 注意 提前向字符前补位“20”
|
*/
|
public static String strToDateLong(String strDate) {
|
Date date = new Date();
|
try {
|
date = new SimpleDateFormat("yyyyMMddHHmmss").parse("20" + strDate);//先按照原格式转换为时间
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//再将时间转换为对应格式字符串
|
return str;
|
}
|
|
/**
|
* 将字符串的长度为0,直接将其数值修改为0
|
*
|
* @param number
|
* @return String
|
*/
|
|
public static String returnToZero(String number) {
|
if (number.length() == 0) {
|
return "0";
|
} else {
|
return number;
|
}
|
}
|
|
|
public static String replace(String str) {
|
String destination = "";
|
if (str != null) {
|
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
Matcher m = p.matcher(str);
|
destination = m.replaceAll("");
|
}
|
return destination;
|
}
|
|
|
//根据value 找到key
|
public static <K, V> K getKeyByLoop(Map<K, V> map, V value) {
|
for (Map.Entry<K, V> entry : map.entrySet()) {
|
if (Objects.equals(entry.getValue(), value)) {
|
return entry.getKey();
|
}
|
}
|
return null;
|
}
|
|
//向左补位0
|
public static String addZeroForNum(String str, int strLength) {
|
int strLen = str.length();
|
if (strLen < strLength) {
|
while (strLen < strLength) {
|
StringBuffer sb = new StringBuffer();
|
sb.append("0").append(str);// 左补0
|
// sb.append(str).append("0");//右补0
|
str = sb.toString();
|
strLen = str.length();
|
}
|
}
|
return str;
|
}
|
|
|
/**
|
* //解析stcd设备编号
|
* 功能描述:
|
*
|
* @param: [stcd]
|
* @return: java.lang.String
|
* @auther: Administrator
|
* @date: 2020/7/23 0023 15:52
|
*/
|
public static String stcdHex2bcd(String stcd) {
|
// 2222 --> ae 08 00
|
//截取stcd 的最后4个字母 转成bcd
|
stcd=Integer.toHexString(Integer.parseInt(stcd));
|
stcd=addZeroForNum(stcd,6);
|
|
return stcd.substring(4,6)+stcd.substring(2,4)+stcd.substring(0,2);
|
}
|
|
/**
|
* 功能描述: 解析stc 将 bcd 转成hex
|
*
|
* @param: [stcd]
|
* @return: java.lang.String
|
* @auther: Administrator
|
* @date: 2020/8/4 0004 11:48
|
*/
|
public static String stcdbcd2hex(String stcd) {
|
|
//010203 000005
|
//截取 hou6位数据 转成 hex
|
String bcdnumber = stcd.substring(6, 12);
|
Integer hex = Integer.parseInt(stcd.substring(6, 12));
|
String hexnumber = addZeroForNum(encodeHEX(hex), 4);
|
String data = stcd.substring(0, 6) + hexnumber;
|
return data;
|
}
|
|
|
/**
|
* 功能描述:
|
*
|
* @param: [receiveStr]
|
* @return: java.lang.Boolean
|
* @auther: Administrator
|
* @date: 2020/7/29 0029 10:36
|
*/
|
//CRC 校验 只需要输入完整的字符串 就可以验证是否成功 低位在前 高位在后
|
public static Boolean determineCRC16(String receiveStr) {
|
String data = receiveStr.substring(receiveStr.length() - 4);
|
String RSC16 = getCRC(receiveStr.substring(0, receiveStr.length() - 4)).toLowerCase();
|
if (data.equals(RSC16)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
|
//高位在前低位在后
|
public static Boolean determineCRC16High(String receiveStr) {
|
//数据全部变成小写
|
receiveStr = receiveStr.toLowerCase();
|
String data = receiveStr.substring(receiveStr.length() - 4);
|
String RSC16 = getCRChigh(receiveStr.substring(0, receiveStr.length() - 4)).toLowerCase();
|
if (data.equals(RSC16)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
|
//将10进制 转16
|
public static String encodeHEX(Integer numb) {
|
|
String hex = Integer.toHexString(numb);
|
return hex;
|
|
}
|
|
|
/**
|
* 功能描述: 16进制转2进制
|
*
|
* @param: [hexString]
|
* @return: java.lang.String
|
* @auther: Administrator
|
* @date: 2020/8/6 0006 9:34
|
*/
|
// public static String hexString2binaryString(String hexString) {
|
//
|
// if (hexString == null || hexString.length() % 2 != 0)
|
// return null;
|
// String bString = "", tmp;
|
// for (int i = 0; i < hexString.length(); i++) {
|
// tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
|
// bString += tmp.substring(tmp.length() - 4);
|
// }
|
// return bString;
|
// }
|
//------------------------------------------------------
|
|
/**
|
* 功能描述: 2进制转16进制
|
*
|
* @param: [bString]
|
* @return: java.lang.String
|
* @auther: Administrator
|
* @date: 2020/8/6 0006 9:34
|
*/
|
|
public static String binaryString2hexString(String bString) {
|
|
|
StringBuffer tmp = new StringBuffer();
|
int iTmp = 0;
|
for (int i = 0; i < bString.length(); i += 4) {
|
iTmp = 0;
|
for (int j = 0; j < 4; j++) {
|
iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
|
}
|
tmp.append(Integer.toHexString(iTmp));
|
}
|
return tmp.toString();
|
}
|
|
|
/**
|
* 功能描述: 将16进制转ascII 码
|
*
|
* @param: [hex]
|
* @return: java.lang.String
|
* @auther: Administrator
|
* @date: 2020/8/24 0024 15:37
|
*/
|
public static String convertHexToString(String hex) {
|
|
StringBuilder sb = new StringBuilder();
|
StringBuilder temp = new StringBuilder();
|
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
|
for (int i = 0; i < hex.length() - 1; i += 2) {
|
//grab the hex in pairs
|
String output = hex.substring(i, (i + 2));
|
//convert hex to decimal
|
int decimal = Integer.parseInt(output, 16);
|
//convert the decimal to character
|
sb.append((char) decimal);
|
|
temp.append(decimal);
|
}
|
|
return sb.toString();
|
}
|
|
|
/**
|
* 功能描述: 解析字符串 YYMMDDHHmmSS 直接解析时间
|
*
|
* @param: [dateStr]
|
* @return: java.time.LocalDateTime
|
* @auther: Administrator
|
* @date: 2020/8/25 0025 16:24
|
*/
|
public static LocalDateTime Generationtime(String dateStr) {
|
String year = "20" + dateStr.substring(0, 2).replaceAll("^(0+)", "");
|
String month = dateStr.substring(2, 4).replaceAll("^(0+)", "");
|
String daymonth = dateStr.substring(4, 6).replaceAll("^(0+)", "");
|
LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(daymonth));
|
String hour = dateStr.substring(6, 8);
|
String minute = dateStr.substring(8, 10);
|
String second = dateStr.substring(10, 12);
|
LocalDateTime datetime = LocalDateTime.of(localDate, LocalTime.parse(hour + ":" + minute + ":" + second));
|
return datetime;
|
}
|
|
|
public static LocalDateTime Generationtime_ss(String dateStr) {
|
String year = "20" + dateStr.substring(0, 2).replaceAll("^(0+)", "");
|
String month = dateStr.substring(2, 4).replaceAll("^(0+)", "");
|
String daymonth = dateStr.substring(4, 6).replaceAll("^(0+)", "");
|
LocalDate localDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(daymonth));
|
String hour = dateStr.substring(6, 8);
|
String minute = dateStr.substring(8, 10);
|
// String second = dateStr.substring(10, 12);
|
LocalDateTime datetime = LocalDateTime.of(localDate, LocalTime.parse(hour + ":" + minute + ":" + "00"));
|
return datetime;
|
|
|
}
|
|
|
/**
|
* @param hexStr
|
* @return
|
* @description 将16进制转换为二进制
|
*/
|
// public static byte[] parseHexStr2Byte(String hexStr) {
|
// if (hexStr.length() < 1)
|
// return null;
|
// byte[] result = new byte[hexStr.length() / 2];
|
// for (int i = 0; i < hexStr.length() / 2; i++) {
|
// int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
|
// int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
|
// result[i] = (byte) (high * 16 + low);
|
// }
|
//
|
// return result;
|
// }
|
|
|
private static String[] binaryArray =
|
{"0000", "0001", "0010", "0011",
|
"0100", "0101", "0110", "0111",
|
"1000", "1001", "1010", "1011",
|
"1100", "1101", "1110", "1111"};
|
|
private static String hexStr = "0123456789abcdef";
|
|
public static String bytes2BinaryStr(byte[] bArray) {
|
|
String outStr = "";
|
int pos = 0;
|
for (byte b : bArray) {
|
//高四位
|
pos = (b & 0xF0) >> 4;
|
outStr += binaryArray[pos];
|
//低四位
|
pos = b & 0x0F;
|
outStr += binaryArray[pos];
|
}
|
return outStr;
|
|
}
|
|
|
public static byte[] HexStringToBinary(String hexString) {
|
//hexString的长度对2取整,作为bytes的长度
|
int len = hexString.length() / 2;
|
byte[] bytes = new byte[len];
|
byte high = 0;//字节高四位
|
byte low = 0;//字节低四位
|
|
for (int i = 0; i < len; i++) {
|
//右移四位得到高位
|
high = (byte) ((hexStr.indexOf(hexString.charAt(2 * i))) << 4);
|
low = (byte) hexStr.indexOf(hexString.charAt(2 * i + 1));
|
bytes[i] = (byte) (high | low);//高地位做或运算
|
}
|
return bytes;
|
}
|
|
|
// public static String binaryString2hexString1(String bString) {
|
// if (bString == null || bString.equals("") || bString.length() % 8 != 0)
|
// return null;
|
// StringBuffer tmp = new StringBuffer();
|
// int iTmp = 0;
|
// for (int i = 0; i < bString.length(); i += 4) {
|
// iTmp = 0;
|
// for (int j = 0; j < 4; j++) {
|
// iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
|
// }
|
// tmp.append(Integer.toHexString(iTmp));
|
// }
|
// return tmp.toString();
|
// }
|
|
|
public static String fatsDay() {
|
Calendar currCal = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
calendar.clear();
|
calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
|
Date time = calendar.getTime();
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
String firstday = format.format(time);
|
return firstday;
|
}
|
|
|
// public static Pushstatus SendSms(String stcd, String phonenumber) {
|
// Pushstatus pushstatus=new Pushstatus();
|
//
|
// try {
|
// CloseableHttpClient client = null;
|
// CloseableHttpResponse response = null;
|
// try {
|
// List<BasicNameValuePair> formparams = new ArrayList<>();
|
// formparams.add(new BasicNameValuePair("Account","17582900772"));
|
// formparams.add(new BasicNameValuePair("Pwd","36514ef9d3fe42e7791eb2ca4"));//登录后台 首页显示
|
// formparams.add(new BasicNameValuePair("Content","请回复大写字母‘Y’即可开启水泵,回复大写‘N’即可关闭水泵 设备编号:"+stcd+"请核对后回复,【有效时间10分钟】"));
|
// formparams.add(new BasicNameValuePair("TemplateId","191576"));
|
// formparams.add(new BasicNameValuePair("Mobile",phonenumber));
|
// formparams.add(new BasicNameValuePair("SignId","389157"));//登录后台 添加签名获取id
|
// HttpPost httpPost = new HttpPost("http://api.feige.ee/SmsService/Send");
|
// httpPost.setEntity(new UrlEncodedFormEntity(formparams,"UTF-8"));
|
// client = HttpClients.createDefault();
|
// response = client.execute(httpPost);
|
// HttpEntity entity = response.getEntity();
|
// String result = EntityUtils.toString(entity);
|
// System.out.println(result);
|
// JSONObject json = JSONObject.parseObject(result);
|
// pushstatus.setCode(json.get("Code").toString());
|
// pushstatus.setMessage(json.get("Message").toString());
|
// } finally {
|
// if (response != null) {
|
// response.close();
|
// }
|
// if (client != null) {
|
// client.close();
|
// }
|
// }
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// return pushstatus;
|
// }
|
//
|
//
|
// //直接获取 上行数据
|
// public static Pushstatus Pusphone(){
|
// Pushstatus pushstatus=new Pushstatus();
|
// try {
|
// CloseableHttpClient client = null;
|
// CloseableHttpResponse response = null;
|
// try {
|
// List<BasicNameValuePair> formparams = new ArrayList<>();
|
// formparams.add(new BasicNameValuePair("Account","17582900772"));
|
// formparams.add(new BasicNameValuePair("Pwd","36514ef9d3fe42e7791eb2ca4"));//登录后台 首页显示
|
// HttpPost httpPost = new HttpPost("http://api.feige.ee/ReportMo/Mo");
|
// httpPost.setEntity(new UrlEncodedFormEntity(formparams,"UTF-8"));
|
// client = HttpClients.createDefault();
|
// response = client.execute(httpPost);
|
// HttpEntity entity = response.getEntity();
|
// String result = EntityUtils.toString(entity);
|
// System.out.println(result);
|
// /* String a1="{\"MoList\":null,\"Code\":10012,\"Message\":\"暂无上行\"}";*/
|
// /*String a="{\"MoList\":[{\"Mobile\":\"17582900772\",\"Content\":\"Y\",\"Times\":1615551076,\"ExtNo\":\"97411\"}],\"Code\":0,\"Message\":\"OK\"}";*/
|
// JSONObject json = JSONObject.parseObject(result);
|
// pushstatus.setCode(json.get("Code").toString());
|
// pushstatus.setMessage(json.get("Message").toString());
|
//
|
// if (json.get("Code").toString().equals("0")){
|
// /*List<FeigedataEntity> feigedata= ( List<FeigedataEntity> ) json.get("MoList");*/
|
// List<FeigedataEntity> feigedata= ( List<FeigedataEntity> ) JSONArray.parseArray(json.get("MoList").toString(),FeigedataEntity.class);
|
// pushstatus.setFeigedataEntityList(feigedata);
|
// //向数据库中插入数据
|
// }
|
//
|
//
|
//
|
//
|
// } finally {
|
// if (response != null) {
|
// response.close();
|
// }
|
// if (client != null) {
|
// client.close();
|
// }
|
// }
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// return pushstatus;
|
// }
|
|
|
public static String dateToStamp(String s) {
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String res = "";
|
if (!"".equals(s)) {
|
try {
|
res = String.valueOf(sdf.parse(s).getTime() / 1000);
|
} catch (Exception e) {
|
System.out.println("传入了null值");
|
}
|
} else {
|
long time = System.currentTimeMillis();
|
res = String.valueOf(time / 1000);
|
}
|
|
return res;
|
}
|
|
|
/*
|
* 将时间戳转换为时间
|
*/
|
public static String stampToDate(int time) {
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String times = format.format(new Date(time * 1000L));
|
System.out.println("日期格式---->" + times);
|
return times;
|
}
|
|
|
public static long GetDateName(String endtime) {
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
|
|
String result = "";
|
long nm = 60;
|
formatter.format(new Date());
|
// 计算差多少分钟
|
Long aa = new Date().getTime();
|
String aa1 = aa.toString().substring(0, 10);
|
|
long diff = Long.parseLong(aa1) - Long.parseLong(endtime);
|
long min = diff / nm;
|
// System.out.print(new Date().getTime());
|
|
if (min <= 30) { // 半小时内发布
|
result = "半小时前";
|
} else if (min <= 60) { // 1小时内发布
|
result = "1小时前";
|
} else if (min <= 120) { // 2小时内发布
|
result = "2小时前";
|
} else if (min <= 180) { // 3小时内发布
|
result = "3小时前";
|
} else if (min <= 240) { // 4小时内发布
|
result = "4小时前";
|
} else if (min <= 300) { // 5小时内发布
|
result = "5小时前";
|
} else if (min <= 360) { // 6小时内发布
|
result = "6小时前";
|
} else if (min <= 420) { // 7小时内发布
|
result = "7小时前";
|
} else if (min <= 480) { // 8小时内发布
|
result = "8小时前";
|
} else if (min <= 540) { // 9小时内发布
|
result = "9小时前";
|
} else if (min <= 600) { // 10小时内发布
|
result = "10小时前";
|
} else if (min <= 660) { // 11小时内发布
|
result = "11小时前";
|
} else if (min <= 720) { // 12小时内发布
|
result = "12小时前";
|
}/*else{
|
result = formatter.format(discuzcreatetime);
|
}*/
|
return min;
|
}
|
|
|
public static String getDate(Date endDate, Date nowDate) {
|
|
long nd = 1000 * 24 * 60 * 60;
|
long nh = 1000 * 60 * 60;
|
long nm = 1000 * 60;
|
// long ns = 1000;
|
|
long diff = endDate.getTime() - nowDate.getTime();
|
// 计算差多少天
|
long day = diff / nd;
|
// 计算差多少小时
|
long hour = diff % nd / nh;
|
// 计算差多少分钟
|
long min = diff % nd % nh / nm;
|
// 计算差多少秒//输出结果
|
// long sec = diff % nd % nh % nm / ns;
|
return day + "天" + hour + "小时" + min + "分钟";
|
}
|
|
|
//将BCD的ip解析成HEXip
|
public static String ipToLong(String ipString) {
|
if (StringUtils.isEmpty(ipString)) {
|
return null;
|
}
|
String host = ipString.substring(ipString.indexOf(":") + 1, ipString.length());
|
//截取端口
|
ipString = ipString.substring(0, ipString.indexOf(":"));
|
String[] ip = ipString.split("\\.");
|
StringBuffer sb = new StringBuffer();
|
for (String str : ip) {
|
String hexstr = Integer.toHexString(Integer.parseInt(str));
|
sb.append(addZeroForNum(hexstr, 2));
|
}
|
String hoststr = Integer.toHexString(Integer.parseInt(host));
|
return sb.toString() + addZeroForNum(hoststr, 4);
|
}
|
|
|
public static String iphex2bcd(String ipString) {
|
String ip1 = Integer.toString(Integer.parseInt(ipString.substring(0, 2), 16));
|
String ip2 = Integer.toString(Integer.parseInt(ipString.substring(2, 4), 16));
|
String ip3 = Integer.toString(Integer.parseInt(ipString.substring(4, 6), 16));
|
String ip4 = Integer.toString(Integer.parseInt(ipString.substring(6, 8), 16));
|
String host = Integer.toString(Integer.parseInt(ipString.substring(8, 12), 16));
|
return ip1 + "." + ip2 + "." + ip3 + "." + ip4 + ":" + host;
|
}
|
|
public static String replace (String fileStr,List<String> pieceList){
|
for (String piece: pieceList){
|
int number;
|
if ((piece.length()-1)%2==0){
|
number=piece.length()-1;
|
}else{
|
number=piece.length();
|
}
|
String replace="40"+addZeroForNum(piece.substring(1),6);
|
fileStr=fileStr.replaceAll(piece,replace);
|
}
|
return fileStr;
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
boolean data = determineCRC16High("11223344554BD1");
|
|
}
|
|
|
}
|