一、xml转json对象
依赖jar
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
XmlToJsonUtil工具类:
package com.example.sms.util;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.StringReader;
public class XmlToJsonUtil {
public static JSONObject xmlToJson(String xml) throws DocumentException {
JSONObject json = new JSONObject();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(xml));
Element root = document.getRootElement();
json.put(root.getName(), elementToJson(root));
return json;
}
public static JSONObject elementToJson(Element element) {
JSONObject json = new JSONObject();
for (Object child : element.elements()) {
Element e = (Element) child;
if (e.elements().isEmpty()) {
json.put(e.getName(), e.getText());
} else {
json.put(e.getName(), elementToJson(e));
}
}
return json;
}
}
二、MD5加密工具类:
package com.example.sms.util;
import java.security.MessageDigest;
public class MD5Util {
/***
* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
/**
* 判断输入的密码和数据库中保存的MD5密码是否一致
* @param inputPassword 输入的密码
* @param md5DB 数据库保存的密码
* @return
*/
public static boolean passwordIsTrue(String inputPassword,String md5DB) {
String md5 = string2MD5(inputPassword);
return md5DB.equals(md5);
}
}
三、Base64加密工具类:
package com.example.sms.util;
import java.util.Base64;
public class Base64Util {
public static String base64(String str) {
byte[] bytes = str.getBytes();
//Base64 加密
String encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println("Base 64 加密后:" + encoded);
/* //Base64 解密
byte[] decoded = Base64.getDecoder().decode(encoded);
String decodeStr = new String(decoded);
System.out.println("Base 64 解密后:" + decodeStr);
System.out.println();*/
return encoded;
}
}
四、StringUtils.join()
将数组或集合以某拼接符拼接到一起形成新的字符串。