编写更少许的代码:使用apache commons工具类库

 

 

在看项目代码的过程当中你会发现某些代码彻底能够直接用开源框架来减小代码量的,如一些带有util的工具类、一些经常使用的io操做等;程序员

研究发现通常的应用程序每 1,000 行代码就包含 20 到 250 个 bug!这个度量被称做缺陷密度。所以可得出一个重要的结论:更少的代码意味着更少的缺陷。面试

我的认为在项目开发过程当中最好能有这样的习惯:能用开源框架(开源框架基本都是众多程序员智慧的结晶,经得住考验)就尽可能用,最大限度地减小编码量;即当编码处理一些业务逻辑时首先想一想或找找有没相关的开源框架,有适合的就用。算法

重视Apache Commonsspring

公司的各个应用下基本都有Apache Commons下的一些jar包,但在开发过程当中习惯性用到这些工具包的童鞋可能比较少(常用到这些jar包的童鞋能够忽视),其实用好Apache Commons下工具集和帮助集能够减小好些编码量。 Apache Commons包含了不少开源的工具,用于解决平时编程常常会遇到的问题,减小重复编码。sql

 

 

Commons-configuration

  1. 若是要使用configuration这个包,首先要保证使用JDK1.2以上,还要引入以下jar包

 

commons-beanutils数据库

commons-langapache

commons-logging

commons-collections

commons-digester

commons-codec

commons-jxpath

 

  1. commons-configuration最新的版本是1.5,这个工具是用来帮助处理配置文件的,支持不少种存储方式
1.    Properties files
2.    XML documents
3.    Property list files (.plist)
4.    JNDI
5.    JDBC Datasource
6.    System properties
7.    Applet parameters
8.    Servlet parameters

最主要的做用是读取资源文件,每一种文件格式都有一个对应的类,以下

 

properties文件--PropertiesConfiguration类;

xml文件—XMLConfiguration;

.ini文件—INIConfiguration;

.plist文件—PropertyListConfiguration;

 

还能够从JNDI中读取properties—JNDIConfiguration;固然还可使用system的properties--SystemConfiguration

  1. 用Properties读取配置文件

usergui.properties(放在类根路径下面): 

colors.background = #FFFFFF
colors.foreground = #000080
window.width = 500
window.height = 300
keys=cn,com,org,uk,edu,jp,hk

 

(1) 通常写法:

public static void readProperties() {
        InputStream in = null;
        try {
            in = new BufferedInputStream((new ClassPathResource(
                    "usergui.properties")).getInputStream());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Properties p = new Properties();
        try {
            p.load(in);
            System.out.println(p.getProperty("colors.background"));
        } catch (IOException e) {
        }
    }

 

(2) 另外一种ResourceBundle方式: 

public static void readProperties() {
        Locale locale = Locale.getDefault();
        ResourceBundlelocalResource = ResourceBundle.getBundle("usergui", locale);
        String value = localResource.getString("colors.background");
        System.out.println("ResourceBundle: " + value);
    }

 

(3) 使用PropertiesConfiguration

public static void loadProperty() {

        try {
            PropertiesConfigurationconfig = new PropertiesConfiguration(
                    "usergui.properties");
            config.setProperty("colors.background", "#00000F");// 更改值
            config.save();
            config.save("usergui.backup.properties");// save a copy
            System.out.println(config.getProperty("colors.background"));
            System.out.println(config.getInt("window.width"));
            String[] keys = config.getStringArray("keys");
            List key2 = config.getList("keys");
            for (int i = 0; i < keys.length; i++) {
                System.out.println(keys[i]);
            }

            for (Object str : key2) {
                System.out.println(str.toString());
            }
        } catch (Exception e) {
        }
    }

 

 

Commons-FileUpload:

 详细案例请参考 文件上传---普通文件和url文件

 

 

Commons DbUtils

这个工具并非如今流行的OR-Mapping工具(好比Hibernate),只是简化数据库操做,好比

QueryRunner run = new QueryRunner(dataSource);
// Execute the query and get the results back from the handler
Object[] result = (Object[]) run.query("SELECT * FROM Person WHERE name=?", "John Doe");

commons-dbutils是 Apache 组织提供的一个开源 JDBC 工具类库,对传统操做数据库的类进行二次封装,能够把结果集转化成List。

项目主页: http://commons.apache.org/dbutils/ 

文档地址: http://commons.apache.org/dbutils/examples.html 

下载地址:http://commons.apache.org/downloads/download_dbutils.cgi 

(1) org.apache.commons.dbutils

DbUtils  : 提供如关闭链接、装载 JDBC 驱动程序等常规工做的工具类

QueryRunner : 该类简单化了 SQL 查询,它与ResultSetHandler组合在一块儿使用能够完成大部分的数据库操做,可以大大减小编码量。

QueryLoader : 属性文件加载器,主要用于加载属性文件中的 SQL 到内存中。

(2) org.apache.commons.dbutils.handlers

ArrayHandler:将ResultSet中第一行的数据转化成对象数组

ArrayListHandler将ResultSet中全部的数据转化成List,List中存放的是Object[] 

BeanHandler:将ResultSet中第一行的数据转化成类对象

BeanListHandler:将ResultSet中全部的数据转化成List,List中存放的是类对象

ColumnListHandler:将ResultSet中某一列的数据存成List,List中存放的是Object对象

KeyedHandler:将ResultSet中存成映射,key为某一列对应为Map。Map中存放的是数据

MapHandler:将ResultSet中第一行的数据存成Map映射

MapListHandler:将ResultSet中全部的数据存成List。List中存放的是Map 

ScalarHandler:将ResultSet中一条记录的其中某一列的数据存成Object 

(3) org.apache.commons.dbutils.wrappers

SqlNullCheckedResultSet:该类是用来对sql语句执行完成以后的的数值进行null的替换。

StringTrimmedResultSet:去除ResultSet中中字段的左右空格。Trim()

  1. 例子
public class TestDbUtils {

    /**
     * 
     * BeanListHandler:将ResultSet中全部的数据转化成List,List中存放的是类对象
     */

    public static void getBeanListData() {
        Connection conn = getConnection();
        QueryRunner qr = new QueryRunner();
        try {
            ResultSetHandler rsh = new BeanHandler(TUser.class);
            TUser usr = (TUser) qr.query(conn,
                    "SELECTid,username,gender FROM t_user WHERE id=10000", rsh);
            System.out.println(StringUtils.center("findById", 50, '*'));
            // System.out.println("id=" + usr.getId() + " name=" +
            // usr.getUsername() + " gender=" + usr.getGender());
            List results = (List) qr.query(conn,
                    "SELECT id,username,gender FROM t_user",
                    new BeanListHandler(TUser.class));
            System.out.println(StringUtils.center("findAll", 50, '*'));
            for (int i = 0; i < results.size(); i++) {
                TUser user = (TUser) results.get(i);
                // System.out.println("id=" + user.getId() + "  name=" +
                // user.getUsername() + "  gender=" + user.getGender());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
    }

    /**
     * 
     * MapListHandler:将ResultSet中全部的数据存成List。List中存放的是Map
     */

    public static void getMapListData() {
        Connection conn = getConnection();
        QueryRunner qr = new QueryRunner();
        try {
            List results = (List) qr.query(conn,
                    "SELECTid,username,gender FROM t_user",
                    new MapListHandler());
            for (int i = 0; i < results.size(); i++) {
                Map map = (Map) results.get(i);
                System.out.println("id=" + map.get("id") + " name="
                        + map.get("username") + " gender=" + map.get("gender"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }

    }

    /**
     * 
     * 新增和更新例子
     */

    public static void insertAndUpdateData() {
        Connection conn = getConnection();
        QueryRunner qr = new QueryRunner();
        try {
            // 建立一个数组来存要insert的数据
            Object[] insertParams = { "John Doe", "000000", "" };
            int inserts = qr
                    .update(conn,
                            "INSERT INTO t_user(username,password,gender) VALUES (?,?,?)",
                            insertParams);
            System.out.println("inserted " + inserts + " data");
            Object[] updateParams = { "111111", "John Doe" };
            int updates = qr.update(conn,
                    "UPDATE t_user SET password=? WHERE username=?",
                    updateParams);
            System.out.println("updated " + updates + " data");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }

    }

    /**
     * 
     * Unlike some other classes in DbUtils, this class(SqlNullCheckedResultSet)
     * is NOT thread-safe.
     */

    public static void findUseSqlNullCheckedResultSet() {
        Connection conn = getConnection();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt
                    .executeQuery("SELECT id, username, gender FROM t_user");
            SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
            wrapper.setNullString("N/A"); // Set null string
            rs = ProxyFactory.instance().createResultSet(wrapper);
            while (rs.next()) {
                System.out.println("id=" + rs.getInt("id") + " username="
                        + rs.getString("username") + " gender="
                        + rs.getString("gender"));
            }
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
    }

    /**** 数据库链接*** */

    public static Connection getConnection() {
        Connection conn = null;
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1/springapp?useUnicode=true&characterEncoding=gb2312";
        DbUtils.loadDriver(driver);
        try {
            conn = DriverManager.getConnection(url, "root", "root");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return conn;
    }
View Code

 

 

Commons BeanUtils

Commons BeanUtils是针对Bean的一个工具集。因为Bean每每是有一堆get和set组成,因此BeanUtils也是在此基础上进行一些包装。

使用示例:一个比较经常使用的功能是Bean Copy,也就是copy bean的属性。若是作分层架构开发的话就会用到,好比从PO(Persistent Object)拷贝数据到VO(Value Object)。

  1. 传统方法以下:
TeacherFormteacherForm=(TeacherForm)form; //获得TeacherForm
Teacher teacher=new Teacher();//构造Teacher对象
//赋值
teacher.setName(teacherForm.getName());
teacher.setAge(teacherForm.getAge());
teacher.setGender(teacherForm.getGender());
teacher.setMajor(teacherForm.getMajor());
teacher.setDepartment(teacherForm.getDepartment());
//持久化Teacher对象到数据库
HibernateDAO= ;
HibernateDAO.save(teacher);

 

使用BeanUtils后,代码就大大改观了,以下所示:

TeacherForm teacherForm=(TeacherForm)form; //获得TeacherForm
Teacher teacher=new Teacher();//构造Teacher对象
BeanUtils.copyProperties(teacher,teacherForm); //赋值
HibernateDAO.save(teacher); //持久化Teacher对象到数据库

 

 

Commons CLI

说明:这是一个处理命令的工具。好比main方法输入的string[]须要解析。你能够预先定义好参数的规则,而后就能够调用CLI来解析。
使用示例:

Options options = new Options();// create Options object
// add t option, option is the command parameter, false indicates that 
// this parameter is not required.
options.addOption(“t”, false, “display current time”);
options.addOption("c", true, "country code");
CommandLineParser parser = new PosixParser();
CommandLinecmd = parser.parse( options, args);
if(cmd.hasOption("t")) {
    // print the date and time
}
else {
    // print the date
}
// get c option value
String countryCode = cmd.getOptionValue("c");
if(countryCode == null) {
    // print default date
}
else {
    // print date for country specified by countryCode
}
View Code

 

 

Commons Codec

说明:这个工具是用来编码和解码的,包括Base64,URL,DES、SHA一、MD五、Soundx等等。

MD5

String str = "abc";

DigestUtils.md5Hex(str);

  

SHA1

String str = "abc";

DigestUtils.shaHex(str);

 

BASE64

//加密

String str= "abc"; // abc为要加密的字符串

byte[] b = Base64.encodeBase64(str.getBytes(), true);

System.out.println(new String(b));

 

//解密

String str = "YWJj"; // YWJj为要解密的字符串

byte[] b = Base64.decodeBase64(str.getBytes());

System.out.println(new String(b));

 

Commons Collections

说明:你能够把这个工具当作是java.util的扩展。

使用示例:举一个简单的例子

OrderedMap map = new LinkedMap();
map.put("FIVE", "5");
map.put("SIX", "6");
map.put("SEVEN", "7");
map.firstKey();  // returns "FIVE"
map.nextKey("FIVE");  // returns "SIX"
map.nextKey("SIX");  // returns "SEVEN"

 

Commons DBCP

说明:数据库链接池,相似的还有common-pool和c3p0等,具体之后再总结

 

Commons HttpClient

详情请参考:HttpClient学习整理

 

Commons IO

说明:能够当作是java.io的扩展

使用示例:

  1. 读取Stream

a) 标准代码:

InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
   InputStreamReaderinR = new InputStreamReader( in );
   BufferedReaderbuf = new BufferedReader( inR );
   String line;
   while ( ( line = buf.readLine() ) != null ) {
     System.out.println( line );
   }
} finally {
   in.close();
}

b) 使用IOUtils
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
   System.out.println( IOUtils.toString( in ) );
} finally {
   IOUtils.closeQuietly(in);
}

  1. 读取文件

File file = new File("/commons/io/project.properties");
List lines = FileUtils.readLines(file, "UTF-8");

  1. 察看剩余空间

longfreeSpace = FileSystemUtils.freeSpace("C:/");

 

Commons JXPath

说明:那么JXpath就是基于Java对象的Xpath,也就是用Xpath对Java对象进行查询。
使用示例:

Addressaddress=(Address)JXPathContext.newContext(vendor).getValue("locations[address/zipCode='90210']/address");
上述代码等同于

Address address = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
while (it.hasNext()){
    Location location = (Location)it.next();
    String zipCode = location.getAddress().getZipCode();
    if (zipCode.equals("90210")){
      address = location.getAddress();
      break;
    }
}

 

Commons Lang

说明:这个工具包能够当作是对java.lang的扩展。提供了诸如StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils等工具类。

Lang.*下有不少Utils类,提供了若干static方法供调用,涵盖了字符串操做、字符操做、JVM交互操做、归类、异常和位域校验等等。

首先看看字符串处理类StringUtils

所有的静态方法,StringUtils继承自Object。属于null safe操做。何谓null safe,就是遇到是null的string对象,也会quietly的把它处理掉,而不会有NullPointerException异常,若是有这个异常,那么就是该类的bug了~~

看看StringUtils的各类方法对比吧

比较一下isEmpty和isBlank这两个方法。isEmpty在判断” “这样的字符串时返回的是false,而isBlank返回的是true。这就明显了吧,empty只对null和””有效,而blank对” ”也有效~~

Clean方法和trim方法都封装了String的trim方法,惟一的不一样是clean将null处理为””,而trim将null处理为null。

以Strip开头的一系列方法基本能够看作trim方法的扩展,可是更强大的是能够处理前导和后续的各类stripChars,不局限于空白符了。

Equal系列的方法加入了null的判断,若是都是null那么也返回true。

IndexOf与String的indexOf相似,加入了null的判断而已,且多了一个ordinalIndexOf方法,能够找到第n个字符串出现的位置。还有一个indexOfIgnoreCase能够不考虑大小写的判断位置匹配。实际代码是调用了string的regionMatches方法,只不过在ignorecase参数上选择了true。

Contains的实现是同样的,只是加入了null的判断。剩下的indexOfAny和containsAny方法都很不错,完成了集合的匹配~~都包含一个字符数组的参数,能够检测字符串中是否包含字符数组中的任意个元素,算法没有特殊的地方,复杂度O(MN)吧~~相比较而言,contains系列的containsOnly和containsNone我却是以为更有用一些。

Substring系列的方法,除了substring封装了string的substring完成了null safe的检查外,还加入了left、right和mid方法。顾名思义,left能够获得最左边的若干个字符做为字串,其实就是调用了str.substring(0, len);而right一样是调用了str.substring(str.length() - len);另外还有一些好比substringBefore和substringAfter之类的,用法相似。substringBetween能够找到两个字串中间的子串。

Split是值得说道的一个改动,本来大量封装string的方法,split是个例外,你们知道,string的split方法用到的参数是一个正则式,虽然强大,可是有时候容易出错。并且string并无提供简化版本。StringUtils提供的split改变了这一情况,开始使用完整的字符串做为参数,而不是regex。同时,对相似功能的jdk版本的StringTokenizer,在内部方法splitWorker中有段注释:Direct code is quicker than StringTokenizer.也就是说,这个是更快的一个工具了~~

对于split的反向操做join,用到了一个lang.text包下的StrBuilder类。主要实现即将若干个object数组一个个的append到buffer中。而后最后toString就好。

Delete和remove能够用来删除string中的内容,好比deleteSpaces能够除去字符串中的全部空白字符(" "t"r"n"b");remove更强大,能够removeStart(只匹配开头)和removeEnd(只匹配结尾),固然remove能够删掉字符串中的任意字串。

Replace,同理这里的replace不像string中的同样是基于正则的,用起来更简单。并且有replaceOnce和replaceEach等各类用法。还有一个辅助的overlay方法,能够直接替换~~

Chomp和chop我以为是比较鸡肋的一个功能了,去除字符串的最后一个字符或者尾部串,这功能很必要么?

Repeat将已有字符串复制n次。

Pad是一个颇有意思的方法,能够将一个已有字符串扩展n个大小,而且填充特定的字符。好比StringUtils.rightPad("bat", 5, 'z') = "batzz"。Pad调用了string的concat方法。

Case conversion的操做就不用多讲了,upper和lower都是同样的。补充说的是,capitalize系列的方法真的很贴心。

补充一个容易让人误会的方法——countMatches,记录一个字符串str中某串sub出现的次数。为何容易误会,“aaaa”中有多少“aa”呢?用该方法获得的答案是2~~~你们懂的

Is打头的一系列方法都很是强大,能够判断字符串是否符合某种格式,好比isAlpha判断是否都是字母,isNumeric判断是否都是数字等等等等。

Reverse这个功能出来后,最早想到的是当初笔试面试时候的一堆回文字符串翻转之类的考试都要囧了。

Abbreviate方法我以为是至关实用的一个方法封装,咱们在各类应用中都很常见的“一堆文字……”就是这个方法的最好应用。

Difference方法返回两个字符串的不一样处,能够说是返回第二个字符串的第一个不一样的位置开始的子串。indexOfDifference返回不一样处的位置。

getCommonPrefix这个方法也很好,能够找到一组字符串的公共前缀。固然只是调用了indexOfDifference这个方法。

接着就是ArrayUtils了

ArrayUtils是一个对数组进行特殊处理的类。固然jdk中的Arrays是有一些功能的,Array也提供了一些动态访问java数组的方法,这里的ArrayUtils扩展提供了更多的功能。

第一个能够说的方法是toMap方法,该方法就是将一个二维数组转换为一个HashMap。固然对输入的参数要求比较严格,会抛出各类异常。NullToEmpty方法是一个防护性编程方法的表明,将null的数组直接变为一个空(长度为0)的数组。Subarray方法提供了基于拷贝的子数组生成方法。Reverse方法提供了一个数组翻转的算法实现。indexOf方法是一个查找指定对象的线性数组查找方法。还提供了一系列装箱拆箱方法(toPrimitive和toObject),就是将Integer之类的对象类型变成int,反之亦然。addAll方法提供了基于数组拷贝的数组合并,就是将数组1和数组2合并为一个新的数组返回。固然,add方法虽然只添加了一个元素,可是也是要数组拷贝的(数组的效率啊!!!)。一样的原理,remove(删除)方法也是基于数组拷贝的,以指定删除元素为界,分别两次拷贝它先后的子数组。

再来就是一些补充了

把一些看到的有意思的可能有用的接口方法提取出来。

RandomStringUtils类里有一个random方法,能够产生一个固定长度的随机字符串。用到了java.util.Random。其中的注释中提到了对Unicode中无法处理的surrogate的处理方法。若是不幸随机到那个位置(D800-DBFF, DC00-DFFF),那么算法中将进行count补充,即提供一次从新随机的机会。

另一个比较有趣的类是StopWatch,这是一个秒表类,经过start方法开始计时,经过split方法截断每一次的分段计时,suspend方法能够暂停秒表,resume恢复计时。最后stop后能够经过getTime得到总共计时。固然在split后的分段计时能够用getSplitTime获取。技术实现上就是定义了几个状态,而后经过每次状态的转变和系统时间的差来表达计时效果。

 

Commons Math

说明:这个包提供的功能有些和Commons Lang重复了,可是这个包更专一于作数学工具,功能更强大。

Math 是一个轻量的,自包含的数学和统计组件,解决了许多很是通用但没有及时出如今Java标准语言中的实践问题.

  咱们能够参考其主页上的用户指导(User Guide ).或者直接研究其API,发如今commons-math中有如下几个包:

 1.org.apache.commons.math Common classes used throughout the commons-math library.

 2.org.apache.commons.math.analysis Implementations of common numerical analysis procedures, including root finding and function interpolation. 主要解决一些数学一般的分析方法,包括求方程的根,其中有对分算法,牛顿算法等等.以及函数的改写.

 3.org.apache.commons.math.complex Complex number type and implementations of complex transcendental functions. 主要用来解决复数的计算.

 4.org.apache.commons.math.distribution Implementations of common discrete and continuous distributions.   主要用来解决连续分布和不连续分布的计算.

 5.org.apache.commons.math.fraction   Fraction number type and fraction number formatting.主要讨论分数的格式和类型.

 6.org.apache.commons.math.linear    Linear algebra support. 线性代数中矩阵和行列式的算法.

 7.org.apache.commons.math.random   Random number and random data generators.随机数算法

 8.org.apache.commons.math.special   Implementations of special functions such as Beta and Gamma.一些特别的函数算法.

 9.org.apache.commons.math.stat     Data storage, manipulation and summary routines.

 10.org.apache.commons.math.util    Convenience routines and common data structures used throughout the commons-math library.

 

Commons Net

 

说明:这个包仍是很实用的,封装了不少网络协议。

 

1.    FTP
2.    NNTP
3.    SMTP
4.    POP3
5.    Telnet
6.    TFTP
7.    Finger
8.    Whois
9.    rexec/rcmd/rlogin
10.    Time (rdate) and Daytime
11.    Echo
12.    Discard
13.    NTP/SNTP

 

使用示例:

TelnetClient telnet = new TelnetClient();
telnet.connect( "192.168.1.99", 23 );
InputStream in = telnet.getInputStream();
PrintStream out = new PrintStream( telnet.getOutputStream() );
...
telnet.close();

 

Commons Validator

说明:用来帮助进行验证的工具。好比验证Email字符串,日期字符串等是否合法。
使用示例:

DateValidator validator = DateValidator.getInstance();// Get the Date validator
Date fooDate = validator.validate(fooString, "dd/MM/yyyy");// Validate/Convert the date
if (fooDate == null) {
     // error...not a valid date
     return;
}

Commons Virtual File System

说明:提供对各类资源的访问接口。支持的资源类型包括

 

1.    CIFS 
2.    FTP 
3.    Local Files 
4.    HTTP and HTTPS 
5.    SFTP 
6.    Temporary Files 
7.    WebDAV 
8.    Zip, Jar and Tar (uncompressed, tgz or tbz2) 
9.    gzip and bzip2 
10.    res
11.    ram

 

这个包的功能很强大,极大的简化了程序对资源的访问。
使用示例:

  1. 从jar中读取文件
    FileSystemManagerfsManager = VFS.getManager();// Locate the Jar file
    FileObjectjarFile = fsManager.resolveFile( "jar:lib/aJarFile.jar" );

    FileObject[] children = jarFile.getChildren();// List the children of the Jar file
    System.out.println( "Children of " + jarFile.getName().getURI() );
    for ( int i = 0; i <children.length; i++ )
    {
        System.out.println( children[ i ].getName().getBaseName() );
    }
  2. 从smb读取文件
    StaticUserAuthenticatorauth = new StaticUserAuthenticator("username", "password", null);
    FileSystemOptions opts = new FileSystemOptions();
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth); 
    FileObjectfo = VFS.getManager().resolveFile("smb://host/anyshare/dir", opts);

Commons Email

一、一个简单的文本邮件

咱们的第一个例子是利用你本地的邮件服务器向"John Doe"发送一个基本邮件信息。

import org.apache.commons.mail.SimpleEmail;

...

  SimpleEmail email = new SimpleEmail();

  email.setHostName("mail.myserver.com");

  email.addTo("jdoe@somewhere.org", "John Doe");

  email.setFrom("me@apache.org", "Me");

  email.setSubject("Test message");

  email.setMsg("This is a simple test of commons-email");

  email.send();

调用setHostName("mail.myserver.com")来设置发送信息的SMTP服务器地址。若是你不设置,将会使用系统属性"mail.host"。

二、发送带附件的邮件

为了向一个邮件添加附件,你须要使用MultiPartEmail这个类。这个类的工做方式和SimpleEmail相似,可是其重载了attach()方法使其能够向邮件添加附件。你能够经过附加或者内联来添加无限数量的附件,这些附件将采用MIME编码。

最简单的方式是使用EmailAttachment类引用你得附件来添加附件。

下面的例子咱们将建立一个图片的附件。把图片附加在邮件中并发送它。

import org.apache.commons.mail.*;

...

  // Create the attachment

  EmailAttachment attachment = new EmailAttachment();

  attachment.setPath("mypictures/john.jpg");

  attachment.setDisposition(EmailAttachment.ATTACHMENT);

  attachment.setDescription("Picture of John");

  attachment.setName("John");

  // Create the email message

  MultiPartEmail email = new MultiPartEmail();

  email.setHostName("mail.myserver.com");

  email.addTo("jdoe@somewhere.org", "John Doe");

  email.setFrom("me@apache.org", "Me");

  email.setSubject("The picture");

  email.setMsg("Here is the picture you wanted");

  // add the attachment

  email.attach(attachment);

  // send the email

  email.send();

你也可使用EmailAttachment引用一个非本地的URL文件。当发送邮件时,URL文件会被自动下载下来附件在邮件中。

下面的例子演示如何把apache的徽标附件在邮件中发送给John 。

import org.apache.commons.mail.*;

...

  // Create the attachment

  EmailAttachment attachment = new EmailAttachment();

  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));

  attachment.setDisposition(EmailAttachment.ATTACHMENT);

  attachment.setDescription("Apache logo");

  attachment.setName("Apache logo");

  // Create the email message

  MultiPartEmail email = new MultiPartEmail();

  email.setHostName("mail.myserver.com");

  email.addTo("jdoe@somewhere.org", "John Doe");

  email.setFrom("me@apache.org", "Me");

  email.setSubject("The logo");

  email.setMsg("Here is Apache's logo");

  

  // add the attachment

  email.attach(attachment);

  // send the email

  email.send();

 

三、发送HTML格式的邮件

发送HTML格式邮件要使用HtmlEmail类。这个类的工做方式很像使用附加方法设置html内容的MultiPartEmail类相似,若是接收者不支持HTML邮件将替换成普通的文本和内联图片。

在下面例子中,咱们使用HTML格式内容和以俄国内联图像。

import org.apache.commons.mail.HtmlEmail;

...

  // Create the email message

  HtmlEmail email = new HtmlEmail();

  email.setHostName("mail.myserver.com");

  email.addTo("jdoe@somewhere.org", "John Doe");

  email.setFrom("me@apache.org", "Me");

  email.setSubject("Test email with inline image");

  // embed the image and get the content id

  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");

  String cid = email.embed(url, "Apache logo");

  // set the html message

  email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

  // set the alternative message

  email.setTextMsg("Your email client does not support HTML messages");

  // send the email

  email.send();

首先,注意,调用embed函数返回一个字符串。这个字符串是一个随机的标识符用来引用一个图像标签中的图像。下一步,在这个例子中咱们没有调用setMsg()函数。这个方法在HtmlEmail中仍然可用,可是若是你使用内联图像时就不该该使用,应该使用setHtmlMsg()函数和setTextMsg()函数。

四、调试

JavaMail API支持一个调试选项,若是你遇到了问题这个可能很是有用。你能够经过调用setDebug(true)来激活调试模式,调试信息将被输出到标准输出中。

五、验证

若是SMTP服务器须要验证,你能够在发送邮件前调用setAuthentication(userName,password)设置账号和密码。这个将会建立一个DefaultAuthenticator实例,JavaMail API在发送邮件时会使用它。你的服务器必须支持RFC2554标准。

 

参考:

http://zhoualine.iteye.com/blog/1770014