Apache Commons
1. 简介:
Apache Commons:是一套建立和维护可重用的Java组件。它包含了很多开源的工具,用于解决平时编程经常会遇到的问题,可以减少重复劳动,大大提高开发的效率和质量。其实就是我们平时经常会用到的工具类。
2. Apache Commons中的几个工具类:
(1) BeanUtils:针对Bean操作的一个工具集
这里只介绍copyProperties
假如有2个类的成员变量都一样,且个数都很多,此时我们要把其中一个类中的很多成员变量拷贝到另外一个类中时,可能会写很多代码,此时可以copyProperties它可以把一个对象的成员变量赋给另外一个类,这样是不很方便呢
例:
现有A,B类他们的有name和age 2个变量,要把B类中的age和name拷贝给A
public class Beanutils {
public static void main(String[] args) {
Beanutils bu = new Beanutils();
bu.copyProperties();
}
public void copyProperties(){
A a = new A();
B b = new B();
b.setAge("20");
b.setName("jack");
try {
BeanUtils.copyProperties(a, b);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(b);
System.out.println(a);
}
}
(2) 加密与加密
base64 hex md5加密与解密
public class Codec {
public static void main(String[] args) {
Codec c = new Codec();
String str = "辅龙";
c.base64(str);
c.hex(str);
c.md5(str);
}
public void base64(String str) {
try {
str = Base64.encodeBase64String(str.getBytes("UTF-8"));
System.out.println("Base64 编码后:" + str);
str = new String(Base64.decodeBase64(str),"UTF-8");
System.out.println("Base64 解码后:" + str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void hex(String str) {
try {
str = Hex.encodeHexString(str.getBytes("UTF-8"));
System.out.println("Hex 编码后:"+str);
Hex hex = new Hex();
str = new String((byte[])hex.decode(str),"UTF-8") ;
System.out.println("Hex 解码后:"+str);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (DecoderException e) {
e.printStackTrace();
}
}
public void md5(String str){
str = DigestUtils.md5Hex(str.getBytes());
System.out.println("MD5编码后:" + str);
try {
str = new String(DigestUtils.md5Hex(str.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("MD5编码后:" + str);
}
}
(3) HashedMap
以前迭代map的时候是不是觉得很麻烦呢?HashedMap可以用增强的for循环迭代了
public void testHashedMap() {
IterableMap map = new HashedMap();
map.put("a", "1");
map.put("b", new Integer(2));
MapIterator it = map.mapIterator();
while (it.hasNext()) {
Object key = it.next();
Object value = it.getValue();
System.out.println("key : " + key + ";value:" + value);
}
}
/**
*
* 得到集合里按顺序存放的key之后的某一Key
*/
public void testLinkedMap() {
OrderedMap map = new LinkedMap();
map.put("FIVE", "5");
map.put("SIX", "6");
map.put("SEVEN", "7");
System.out.println(map.firstKey()); // returns "FIVE"
System.out.println(map.nextKey("FIVE")); // returns "SIX"
System.out.println(map.nextKey("SEVEN")); // returns null
System.out.println(map.previousKey("SEVEN")); // returns "SIX"
System.out.println(map.lastKey());
}
(4) 读取property配置文件
public class Configuration {
public static void main(String[] args) {
Configuration c = new Configuration();
c.properties();
}
public void properties() {
try {
PropertiesConfiguration pc = new PropertiesConfiguration(
"E:\\projects\\commonstest\\commonstest\\src\\cn\\fulong\\configuration\\config.properties");
pc.getString("colors.background");
Integer integer = pc.getInt("window.width");
System.out.println(integer);
pc.setProperty("colors.backgroundaaa", "#000012");
pc.save();
pc.save("usergui.backup.properties");// save a copy
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(5) 发邮件(可以添加附件)
public class EmailTest {
//用commons email发送邮件
public static void main(String args[]) throws EmailException{
//Email email = new SimpleEmail();
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");//设置SMTP服务器
email.setAuthenticator(new DefaultAuthenticator("javaemailtest", "fulong"));
email.setFrom("javaemailtest@163.com");
email.setSubject("TestMail");
//email.setMsg("This is a test mail ... :-)");
email.setHtmlMsg("<b>msg中文</b>");
email.addTo("490791132@qq.com");
File file = new File("E:\\projects\\commonstest\\commonstest\\src\\cn\\fulong\\configuration\\config.properties");
email.attach(file);
email.send();
}
}