设为首页 收藏本站
查看: 743|回复: 0

[经验分享] 提高开发效率的组件 -- Apache Commons

[复制链接]

尚未签到

发表于 2017-1-7 07:54:05 | 显示全部楼层 |阅读模式
                                            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();
}
}
 

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324853-1-1.html 上篇帖子: Apache Tez DAG计算应用框架 下篇帖子: apache的作用和tomcat的区别(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表