在进行此项目之前需要将dom4j的jar包及其依赖包导入,具体操作间本博客文章:XML之------dom4j对XML文档增删改查点击打开链接
公共类:DocumentFactory.java
package cn.hncu.contact.common;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class DocumentFactory {
private static Document dom=null;
static{
try {
SAXReader sax=new SAXReader();
dom=sax.read("xml/contact.xml");
} catch (Exception e) {
throw new RuntimeException("xml文件解析出错", e);
}
}
private DocumentFactory(){
}
public static Document getDocument(){
return dom;
}
public static void save(){
try {
XMLWriter writer=new XMLWriter(new FileWriter("xml/contact.xml"));
writer.write(dom);
writer.close();
} catch (IOException e) {
throw new RuntimeException("xml文件保存出错", e);
}
}
}
工具类:
IDGenerate.java
package cn.hncu.contact.utils;
import java.util.UUID;
public class IDGenerate {
private IDGenerate(){
}
public static String getId(){
String uuid=UUID.randomUUID().toString();
uuid=uuid.replace("-", "");
return uuid;
}
}
PwdMd5Utils.java
package cn.hncu.contact.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
public class PwdMd5Utils {
public static String mkPwd(String pwd){
String pwd2="";
try {
MessageDigest msg=MessageDigest.getInstance("MD5");
byte buf[]=msg.digest(pwd.getBytes());//buf的长度是16
for (byte b:buf){
String s=Integer.toHexString(b & 0xff);
if (s.length()==1){
s+="f";//可以把'f'换成其它的16进制数如'a'
}
pwd2+=s;
}
return pwd2;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("加密失败", e);
}
}
public static void main(String[] args) {
//introduce1();
//introduce2();
md5_2();
}
//该加密方式目前可以用,但不建议,因为使用了oracle公司没有公开的类
private static void introduce1() {
String pwd="1234";
try {
MessageDigest msg=MessageDigest.getInstance("MD5");
b