1.springboot框架自带了jackson解析工具,这里为了保险起见,还是选择手动导入jackson依赖
<dependency>
<groupId>com.fasterxml.jackson.modulegroupId>
<artifactId>jackson-module-jaxb-annotationsartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.6.3version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.7.4version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformatgroupId>
<artifactId>jackson-dataformat-yamlartifactId>
<version>2.8.6version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatypegroupId>
<artifactId>jackson-datatype-jodaartifactId>
<version>2.8.6version>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.5version>
dependency>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* JSON 对象 互转 工具类
*
* @author abao
* @create 2018-01-03 19:16
**/
public class JsonUtil {
public static final ObjectMapper MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
public static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory());
/**
* 将对象转换成JSON格式字符串
*
* @param data POJO
* @return string
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将JSON格式字符串转对象
*
* @param jsonData JSON格式字符串
* @param beanType POJO对象
* @return POJO
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
* @param jsonData JSON格式数据
* @param beanType POJO
*/
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
* @param jsonData JSON格式数据
*/
public static <T> Map<Integer, String> jsonToMap(String jsonData) {
try {
Map<Integer, String> map = MAPPER.readValue(jsonData, Map.class);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
* @param jsonData JSON格式数据
*/
public static <T> Map<String, String> jsonToMap2(String jsonData) {
try {
Map<String, String> map = MAPPER.readValue(jsonData, Map.class);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
* @param jsonData JSON格式数据
*/
public static <T> Map<String, Integer> jsonToMap3(String jsonData) {
try {
Map<String, Integer> map = MAPPER.readValue(jsonData, Map.class);
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将POJO对象转换成yaml格式字符串
*
* @param data
* @return
*/
public static String pojoToYaml(Object data) {
try {
String yaml = YAML_MAPPER.writeValueAsString(data);
return yaml;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json格式字符串转换为yaml格式字符串
*
* @param jsonData
* @
public static String jsonToYaml(String jsonData) {
try {
JsonNode jsonNodeTree = MAPPER.readTree(jsonData);
String yaml = YAML_MAPPER.writeValueAsString(jsonNodeTree);
return yaml;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将yaml格式数据写入文件
*
* @param filePath 文件生成路径
* @param yamlData yaml格式数据
*/
public static void createYamlFile(String filePath, String yamlData) {
Yaml yaml = new Yaml();
FileWriter writer;
try {
Map<String, Object> map = (Map<String, Object>) yaml.load(yamlData);
writer = new FileWriter(filePath);
yaml.dump(map, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将yaml格式文件转成json格式字符串
*
* @param filePath
* @return
*/
public static String yamlFileToJson(String filePath) {
Map<String, Object> map = null;
try {
FileInputStream fis = new FileInputStream(filePath);
Yaml yaml = new Yaml();
map = (Map<String, Object>) yaml.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
return objectToJson(map);
}
/**
* 将yaml格式字符串转成json格式字符串
*
* @param filePath
* @return
*/
public static String yamlToJson(String filePath) {
Map<String, Object> map = null;
try {
// FileInputStream fis = new FileInputStream(filePath);
Yaml yaml = new Yaml();
map = (Map<String, Object>) yaml.load(filePath);
} catch (Exception e) {
e.printStackTrace();
}
return objectToJson(map);
}
/**
* 将yaml格式字符串转成map
*
* @param filePath
* @return
*/
public static Map<String, String> yamlToMap(String filePath) {
Map<String, String> map = null;
try {
FileInputStream fis = new FileInputStream(filePath);
Yaml yaml = new Yaml();
map = (Map<String, String>) yaml.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static void main(String[] args) {
String json = "{\n" +
" \"x\": 0,\n" +
" \"y\": 0,\n" +
" \"scale\": 1,\n" +
" \"pens\": [\n" +
" {\n" +
" \"name\": \"image\",\n" +
" \"componentId\": 88,\n" +
" \"componentType\": 0,\n" +
" \"penExampleId\": null,\n" +
" \"text\": \"测试三号\",\n" +
" \"level\": 1,\n" +
" \"rootOid\": \"s1\",\n" +
" \"width\": 100,\n" +
" \"height\": 100,\n" +
" \"hiddenText\": true,\n" +
" \"image\": \"/cdn-img/dcim/picture/icon/2f53590aa80b4a40-arrow_right.png\",\n" +
" \"id\": \"9c02d64\",\n" +
" \"children\": [\n" +
"\n" +
" ],\n" +
" \"x\": 268,\n" +
" \"y\": 308,\n" +
" \"lineWidth\": 1,\n" +
" \"fontSize\": 12,\n" +
" \"lineHeight\": 1.5,\n" +
" \"anchors\": [\n" +
" {\n" +
" \"id\": \"0\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0.5,\n" +
" \"y\": 0\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 1,\n" +
" \"y\": 0.5\n" +
" },\n" +
" {\n" +
" \"id\": \"2\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0.5,\n" +
" \"y\": 1\n" +
" },\n" +
" {\n" +
" \"id\": \"3\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0,\n" +
" \"y\": 0.5\n" +
" }\n" +
" ],\n" +
" \"rotate\": 0,\n" +
" \"extend_rootmain\": \"1245\"\n" +
" },\n" +
" {\n" +
" \"name\": \"image\",\n" +
" \"componentId\": 88,\n" +
" \"componentType\": 0,\n" +
" \"penExampleId\": null,\n" +
" \"text\": \"测试三号\",\n" +
" \"level\": 1,\n" +
" \"rootOid\": \"s1\",\n" +
" \"width\": 100,\n" +
" \"height\": 100,\n" +
" \"hiddenText\": true,\n" +
" \"image\": \"/cdn-img/dcim/picture/icon/2f53590aa80b4a40-arrow_right.png\",\n" +
" \"id\": \"9c02d64\",\n" +
" \"children\": [\n" +
"\n" +
" ],\n" +
" \"x\": 268,\n" +
" \"y\": 308,\n" +
" \"lineWidth\": 1,\n" +
" \"fontSize\": 12,\n" +
" \"lineHeight\": 1.5,\n" +
" \"anchors\": [\n" +
" {\n" +
" \"id\": \"0\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0.5,\n" +
" \"y\": 0\n" +
" },\n" +
" {\n" +
" \"id\": \"1\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 1,\n" +
" \"y\": 0.5\n" +
" },\n" +
" {\n" +
" \"id\": \"2\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0.5,\n" +
" \"y\": 1\n" +
" },\n" +
" {\n" +
" \"id\": \"3\",\n" +
" \"penId\": \"9c02d64\",\n" +
" \"x\": 0,\n" +
" \"y\": 0.5\n" +
" }\n" +
" ],\n" +
" \"rotate\": 0,\n" +
" \"extend_rootmain\": \"1245\"\n" +
" }\n" +
" ],\n" +
" \"origin\": {\n" +
" \"x\": 0,\n" +
" \"y\": 0\n" +
" },\n" +
" \"center\": {\n" +
" \"x\": 0,\n" +
" \"y\": 0\n" +
" },\n" +
" \"paths\": {\n" +
" },\n" +
" \"version\": \"1.1.27\"\n" +
"}";
JsonNode jsonNode = JsonUtil.jsonToPojo(json, JsonNode.class);
((ArrayNode) jsonNode.get("pens")).remove(0);
System.out.println(JsonUtil.objectToJson(jsonNode));
}
}
2.远程发送http请求调用接口
okhttp远程服务调用常用工具类
http请求由哪几部分组成
1.导入依赖
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>okhttpartifactId>
<version>3.8.1version>
dependency>
2.工具类如下:
1.创建单例远程请求对象
public class OkHttpUtil {
private volatile static OkHttpClient singleton;
private OkHttpUtil() {
}
public static OkHttpClient getInstance() {
if (singleton == null) {
synchronized (OkHttpUtil.class) {
if (singleton == null) {
singleton = new OkHttpClient();
}
}
}
return singleton;
}
}
2.封装请求工具类
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static OkHttpClient client = OkHttpUtil.getInstance();
public static Response post(String url, String json) throws Exception {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (Exception e) {
throw new Exception("HttpUtils post错误信息:" + e);
} finally {
if (response != null) {
response.close();
}
}
return response;
}
public static Response get(String url) throws Exception {
Request request = new Request.Builder()
.url(url)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (Exception e) {
throw new Exception("HttpUtils get错误信息:" + e);
} finally {
if (response != null) {
response.close();
}
}
return response;
}
}
常见正则校验
public class StringUtil extends org.apache.commons.lang3.StringUtils {
private static Pattern emailPattern = Pattern.compile("^([a-zA-Z]|[0-9])(\\w|\\-)+@[a-zA-Z0-9]+\\.([a-zA-Z]{2,4})$");
private static Pattern mobilePattern = Pattern.compile("^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$");
private static Pattern chinaeseNamePattern = Pattern.compile("^[\\S]{1}[\\s\\S]{0,48}[\\S]{1}$");
private static Pattern employeeIdPattern = Pattern.compile("^[0-9A-Za-z]{0,50}$");
private static Pattern bucketNamePattern = Pattern.compile("^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$");
private static Pattern usernamePattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]{4,15}");
private static Pattern namePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{2,50}");
private static Pattern passwordPattern = Pattern.compile("^[0-9A-Za-z]{6,20}$");
private static Pattern addressPattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{1,200}");
private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");
private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
private static final Pattern BANDWIDTH_PATTERN = Pattern.compile("^[1-9][0-9]{0,3}[G|M]$");
private static final Pattern OID_PATTERN = Pattern.compile("^(\\d+\\.)+\\d+$");
private static final Pattern METRIC_NAME_PATTERN = Pattern.compile("[a-zA-Z_:][a-zA-Z0-9_:]{2,200}");
private static Pattern fieldNamePattern = Pattern.compile("[A-Za-z0-9]+([A-Za-z0-9_]+){4,15}");
//规则名称正则,长度2-200,大小写字母数字开头,后面可以是大小写字母,数字,下划线,中横线
private static Pattern ruleNamePattern = Pattern.compile("^[A-Za-z0-9]([A-Za-z0-9_-]){1,199}$");
//告警名称正则,长度1-50,字母,数字,汉字,下划线,中横线
private static Pattern alarmNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{1,50}");
// private static Pattern alarmNamePattern = Pattern.compile("^[a-zA-Z0-9_-\\u4e00-\\u9fa5]{1,50}");
//数字正则
private static Pattern numberPattern = Pattern.compile("[^0-9]");
//1-100之间的整数
private static final Pattern MODEL_HEIGHT_PATTERN = Pattern.compile("^([1-9][0-9]{0,1}|100)$");
//0-99之间的整数
private static final Pattern SLOT_PATTERN = Pattern.compile("^([0-9][0-9]{0,1}|99)$");
//资产属性展示名正则 输入1-64个字符,可输入0-9,字母a-zA-Z,空格,点(.),下划线,以及汉字
private static final Pattern DISPLAY_NAME_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5\\ \\w\\s\\(\\)\\(\\)\\.]{1,64}$");
//请输入1-32个字符,不能包含?<=>'%#"!&+/\;
private static final Pattern CUSTOMER_NAME_PATTERN = Pattern.compile("^((?!\\\\|\\/|:|\\?|<|=|>|'|%|#|\"|!|;|;|!|?|&|\\+|\\/|\\\\).){1,32}$");
//手机号码
private static final Pattern likeManPhonePattern = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
//请输入0-999999.99之间的浮点数(俩位小数)
private static final Pattern contractPowerPattern = Pattern.compile("^([0-9]|[1-9][0-9]{0,5})(\\.[1-9]|\\.[0-9][1-9])?$");
//请输入1-64个字符,可输入中文,数字0-9,字母a-zA-Z,空格,以及字符+._-()()[]:,/
private static final Pattern classificationNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_\\+\\.\\(\\)\\(\\)\\:\\,\\/\\[\\]]{1,64}");
//请输入1-32位字母
private static final Pattern attributesNamePattern = Pattern.compile("^[A-Za-z]{1,32}$");
//制造商名称 长度1-64,字母,数字,汉字,下划线,中横线
private static final Pattern manufacturerNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{1,64}");
//只能输入中文,英文,数字,空格和特殊字符#@_.*-/[]()~′^{}|:;、,‘’
private static final Pattern deviceTypeNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_\\#\\@\\.\\*\\-\\/\\[\\]\\(\\)\\^\\~\\′\\{\\}\\|\\:\\;\\、\\,\\‘\\’\\ ]{1,64}");
//请输入0-9999.99之间的浮点数(俩位小数)
private static final Pattern weightPattern = Pattern.compile("^([0-9]|[1-9][0-9]{0,3})(\\.[1-9]|\\.[0-9][1-9])?$");
//请输入1-128位字符,只能输入中文、英文、数字、空格和特殊字符#@!_.*-[]+()~`^{}|'?$:; !。……【】()‘?’¥:;
private static final Pattern assetLocationNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\#\\@\\_\\.\\*\\-\\[\\]\\+\\(\\)\\~\\`\\^\\{\\}\\|\\'\\?\\$\\:\\;\\!\\。\\……\\【\\】\\(\\)\\‘\\?\\¥\\;\\:\\\\]{1,128}");
private static final Pattern isNumber = Pattern.compile("[0-9]*");
//请输入长度1到100的字符,包含数字、字母、中文、下划线
private static final Pattern enumPattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\_]{1,100}");
//请输入数字0-9字母a-zA-Z-{}=,最大长度为64
private static final Pattern assetNumberPattern = Pattern.compile("^[A-Za-z0-9\\-\\{\\}\\=]{1,64}");
//请输入1-64位字母,不能包含!"#%&'/;<=>?\
private static final Pattern assetTagPattern = Pattern.compile("^([a-zA-Z](?!\\!|\\\"|#|\\%|\\&|'|\\/|;|<|=|>|\\?|\\\\).){1,64}$");
//请输入1-50位字母,不能包含!"@&'/;=?\
private static final Pattern assetNamePattern = Pattern.compile("^((?!\\!|\\\"|\\@|\\&|'|\\/|;|=|\\?|\\\\).){1,50}$");
//请输入1-32位字符,不能包含!"#%&'/;<=>?\
private static final Pattern assetCustomerNamePattern = Pattern.compile("^((?!\\!|\\\"|#|\\%|\\&|'|\\/|;|<|=|>|\\?|\\\\).){1,32}$");
//请输入1-64个字符,可输入数字0-9 字母a-zA-Z,空格,点(.),下划线,以及汉字
private static final Pattern assetPaymentPeoplePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\_\\.\\ ]{1,64}");
//请输入1-50个字符,可输入数字0-9 字母a-zA-Z,空格,点(.),下划线,中划线,以及汉字(动环主机名称)
private static final Pattern dynamicRingHostPattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\_\\.\\-\\ ]{1,50}");
/**
* 回路规则模块
*/
//规则名称,线路名称 (请输入1-32个字符,可输入数字0-9,字母a-zA-Z,汉字,下划线,中划线)
private static final Pattern returnRuleNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{1,32}");
/**
* 闪断震荡规则模块
*/
//闪断震荡规则 名称 (请输入1-32个字符,可输入数字0-9,字母a-zA-Z,汉字,下划线,中划线)
private static final Pattern flashRuleNamePattern = Pattern.compile("^[\\u4e00-\\u9fa5\\ A-Za-z0-9\\-\\_]{1,32}");
public static boolean checkFlashRuleName(String str){
try {
return flashRuleNamePattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkReturnRuleName(String str){
try {
return returnRuleNamePattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAssetCustomerName(String str){
try {
return assetCustomerNamePattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAssetPaymentPeople(String str){
try {
return assetPaymentPeoplePattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAssetName(String str){
try {
boolean matches = assetNamePattern.matcher(str).matches();
return matches;
}catch (Exception e){
return false;
}
}
public static boolean checkAssetTag(String str){
try {
return assetTagPattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAssetNumber(String str){
try {
return assetNumberPattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkEnumValue(String str){
try {
return enumPattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
// 判断字符串是否能转换为数字
public static boolean checkStrIsNumber(String str){
try {
return assetLocationNamePattern.matcher(str).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAssetLocationName(String name){
try {
return assetLocationNamePattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkWeight(String name){
try {
return weightPattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkDeviceTypeName(String name){
try {
return deviceTypeNamePattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkManufacturerName(String name){
try {
return manufacturerNamePattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkAttributesName(String name){
try {
return attributesNamePattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkClassificationName(String name){
try {
return classificationNamePattern.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkContractPower(String contractPower){
try {
return contractPowerPattern.matcher(contractPower).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkLikeManPhone(String phone){
try {
return likeManPhonePattern.matcher(phone).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkCustomerName(String name){
try {
return CUSTOMER_NAME_PATTERN.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkDisplayName(String name){
try {
return DISPLAY_NAME_PATTERN.matcher(name).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkSlotHeight(String slot){
try {
return SLOT_PATTERN.matcher(slot).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkModelHeight(String height){
try {
return MODEL_HEIGHT_PATTERN.matcher(height).matches();
}catch (Exception e){
return false;
}
}
public static boolean checkUsername(String username) {
try {
return usernamePattern.matcher(username).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkName(String name) {
try {
return namePattern.matcher(name).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkPassword(String password) {
try {
return passwordPattern.matcher(password).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkAddress(String address) {
try {
return addressPattern.matcher(address).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkEmailPattern(String email) {
try {
return emailPattern.matcher(email).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkMobilePattern(String mobile) {
try {
return mobilePattern.matcher(mobile).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkFieldNamePattern(String name) {
try {
return fieldNamePattern.matcher(name).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkChinaeseNamePattern(String chinaeseName) {
try {
return chinaeseNamePattern.matcher(chinaeseName).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkEmployeeIdPattern(String employeeId) {
try {
return employeeIdPattern.matcher(employeeId).matches();
} catch (Exception e) {
return false;
}
}
public static boolean match(String str) {
if (StringUtils.isEmpty(str)) {
return false;
}
// 匹配数字、字母、中/下划线
Pattern pattern = Pattern.compile("^[0-9a-zA-Z_-]+$");
return pattern.matcher(str).matches();
}
/**
* 手机号伪处理
*
* @param phone 手机号
* @return 处理后的结果
*/
public static String phoneDispose(String phone) {
if (StringUtils.isEmpty(phone)) return null;
return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
public static boolean checkMetricName(String metricName) {
try {
return METRIC_NAME_PATTERN.matcher(metricName).matches();
} catch (Exception e) {
return false;
}
}
/**
* 身份证号伪处理
*
* @param idCard 身份证号
* @return 处理好的结果
*/
public static String idCardDispose(String idCard) {
return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2");
}
/**
* 生成一个没有-分隔符的uuid字符串
*/
public static String randomUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 左边补齐0
*
* @param num 数值
* @return 补齐0后的字符串
*/
public static String makeUp(int num) {
return "B" + String.format("%09d", num);
}
/**
* 16进制转换成为string类型字符串
*
* @param s
* @return
*/
public static String hexStringToString(String s) {
if (s == null || s.equals("")) {
return null;
}
s = s.replace(" ", "");
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "UTF-8");
new String();
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
public static void main(String[] args) throws Exception {
// File file = new File("d:/2/sdn.mp4");
// // File file = new File("d:/2/Python.pdf");
// Parser parser = new AutoDetectParser();
// BodyContentHandler handler = new BodyContentHandler();
// Metadata metadata = new Metadata();
// FileInputStream inputstream = new FileInputStream(file);
// ParseContext context = new ParseContext();
// parser.parse(inputstream, handler, metadata, context);
// System.out.println(handler.toString());
//
// //getting the list of all meta data elements
// String[] metadataNames = metadata.names();
//
// for (String name : metadataNames) {
// System.out.println(name + ": " + metadata.get(name));
// }
// System.out.println(metadata.get("xmpDM:duration"));
// String s = IpUtils.numberToIPv6(new BigInteger("47925090005790777572784935962679443456"));
//
// System.out.println(s);
// System.out.println(IPV6_HEX_COMPRESSED_PATTERN.matcher(s).matches());
// System.out.println(IPV6_STD_PATTERN.matcher(s).matches());
// String s1 = IpUtils.intToIpv6(new BigInteger("47925090005790777572784935962679443456"));
// System.out.println(s1);
// System.out.println(IPV6_HEX_COMPRESSED_PATTERN.matcher(s1).matches());
// System.out.println(IPV6_STD_PATTERN.matcher(s1).matches());
//
// IPv6Address address = IPv6Address.fromString("fd13:6d20:29dc:cf27::2");
// System.out.println(address.toBigInteger());
// System.out.println(checkOid("121.3.6.1.4.1.123456.999.1"));
System.out.println(getStringNumber("电池组是实时"));
}
/**
* @param bucket:
* @Description:检查存储桶名称是否合法
* @Author: hch
* @Date: 2020/7/28 12:56
* @return: boolean
**/
public static boolean checkBucketPattern(String bucket) {
try {
return bucketNamePattern.matcher(bucket).matches();
} catch (Exception e) {
return false;
}
}
/**
* @param name:
* @Description:检查套餐名称是否合法
* @Author: hch
* @Date: 2020/8/28 12:56
* @return: boolean
**/
public static boolean checkPackageNamePattern(String name) {
try {
return namePattern.matcher(name).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkIpv4(String ip) {
try {
return IPV4_PATTERN.matcher(ip).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkIpv6(String ipv6) {
try {
return (IPV6_HEX_COMPRESSED_PATTERN.matcher(ipv6).matches() || IPV6_STD_PATTERN.matcher(ipv6).matches());
} catch (Exception e) {
return false;
}
}
public static boolean checkBandWidth(String bandWidth) {
try {
return BANDWIDTH_PATTERN.matcher(bandWidth).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkRuleName(String ruleName) {
try {
return ruleNamePattern.matcher(ruleName).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkAlarmName(String alarmName) {
try {
return alarmNamePattern.matcher(alarmName).matches();
} catch (Exception e) {
return false;
}
}
public static boolean checkOid(String oid) {
try {
return OID_PATTERN.matcher(oid).matches();
} catch (Exception e) {
return false;
}
}
public static Integer getStringNumber(String string) {
Matcher m = numberPattern.matcher(string);
String num = m.replaceAll("").trim();
return isNotBlank(num) ? Integer.valueOf(num) : 0;
}
public static boolean checkEnumConstant(String enumConstant) {
String[] enumConstants = enumConstant.split(",");
if (enumConstants.length < 1 ) {
return false;
}
Map<Integer, String> hashMap = new HashMap<>();
for (String data : enumConstants) {
if (!data.contains(":")) {
return false;
}
String[] map = data.split(":");
if (map.length != 2) {
return false;
}
try {
Integer key = Integer.valueOf(map[0]);
if (hashMap.containsKey(key)) {
return false;
}
hashMap.put(key, map[1]);
} catch (Exception e) {
return false;
}
if (StringUtil.isBlank(map[1])) {
return false;
}
}
return true;
}
}