Apache Commons包含了不少開源的工具,用於解決平時編程常常會遇到的問題,減小重複勞動。下面是我這幾年作開發過程當中本身用過的工具類作簡單介紹。html
組件 |
功能介紹 |
BeanUtils |
提供了對於JavaBean進行各類操做,克隆對象,屬性等等. |
Betwixt |
XML與Java對象之間相互轉換. |
Codec |
處理經常使用的編碼方法的工具類包 例如DES、SHA一、MD五、Base64等. |
Collections |
java集合框架操做. |
Compress |
java提供文件打包 壓縮類庫. |
Configuration |
一個java應用程序的配置管理類庫. |
DBCP |
提供數據庫鏈接池服務. |
DbUtils |
提供對jdbc 的操做封裝來簡化數據查詢和記錄讀取操做. |
Email |
java發送郵件 對javamail的封裝. |
FileUpload |
提供文件上傳功能. |
HttpClien |
提供HTTP客戶端與服務器的各類通信操做. 如今已改爲HttpComponents |
IO |
io工具的封裝. |
Lang |
Java基本對象方法的工具類包 如:StringUtils,ArrayUtils等等. |
Logging |
提供的是一個Java 的日誌接口. |
Validator |
提供了客戶端和服務器端的數據驗證框架. |
一、BeanUtils
提供了對於JavaBean進行各類操做, 好比對象,屬性複製等等。java
Java代碼
- //一、 克隆對象
- // 新建立一個普通Java Bean,用來做爲被克隆的對象
-
- public class Person {
- private String name = "";
- private String email = "";
-
- private int age;
- //省略 set,get方法
- }
-
- // 再建立一個Test類,其中在main方法中代碼以下:
- import java.lang.reflect.InvocationTargetException;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.beanutils.BeanUtils;
- import org.apache.commons.beanutils.ConvertUtils;
- public class Test {
-
- /**
-
- * @param args
-
- */
- public static void main(String[] args) {
- Person person = new Person();
- person.setName("tom");
- person.setAge(21);
- try {
- //克隆
- Person person2 = (Person)BeanUtils.cloneBean(person);
- System.out.println(person2.getName()+">>"+person2.getAge());
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
-
- }
-
- }
-
- }
-
- // 原理也是經過Java的反射機制來作的。
- // 二、 將一個Map對象轉化爲一個Bean
- // 這個Map對象的key必須與Bean的屬性相對應。
- Map map = new HashMap();
- map.put("name","tom");
- map.put("email","tom@");
- map.put("age","21");
- //將map轉化爲一個Person對象
- Person person = new Person();
- BeanUtils.populate(person,map);
- // 經過上面的一行代碼,此時person的屬性就已經具備了上面所賦的值了。
- // 將一個Bean轉化爲一個Map對象了,以下:
- Map map = BeanUtils.describe(person)
二、Betwixt
XML與Java對象之間相互轉換。
mysql
Java代碼
- //一、 將JavaBean轉爲XML內容
- // 新建立一個Person類
- public class Person{
- private String name;
- private int age;
- /** Need to allow bean to be created via reflection */
- public PersonBean() {
- }
- public PersonBean(String name, int age) {
- this.name = name;
- this.age = age;
- }
- //省略set, get方法
- public String toString() {
- return "PersonBean[name='" + name + "',age='" + age + "']";
- }
- }
-
- //再建立一個WriteApp類:
- import java.io.StringWriter;
- import org.apache.commons.betwixt.io.BeanWriter;
- public class WriteApp {
- /**
- * 建立一個例子Bean,並將它轉化爲XML.
- */
- public static final void main(String [] args) throws Exception {
- // 先建立一個StringWriter,咱們將把它寫入爲一個字符串
- StringWriter outputWriter = new StringWriter();
- // Betwixt在這裏僅僅是將Bean寫入爲一個片段
- // 因此若是要想完整的XML內容,咱們應該寫入頭格式
- outputWriter.write(「<?xml version=’1.0′ encoding=’UTF-8′ ?>\n」);
- // 建立一個BeanWriter,其將寫入到咱們預備的stream中
- BeanWriter beanWriter = new BeanWriter(outputWriter);
- // 配置betwixt
- // 更多詳情請參考java docs 或最新的文檔
- beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
- beanWriter.getBindingConfiguration().setMapIDs(false);
- beanWriter.enablePrettyPrint();
- // 若是這個地方不傳入XML的根節點名,Betwixt將本身猜想是什麼
- // 可是讓咱們將例子Bean名做爲根節點吧
- beanWriter.write(「person」, new PersonBean(「John Smith」, 21));
- //輸出結果
- System.out.println(outputWriter.toString());
- // Betwixt寫的是片段而不是一個文檔,因此不要自動的關閉掉writers或者streams,
- //但這裏僅僅是一個例子,不會作更多事情,因此能夠關掉
- outputWriter.close();
- }
- }
- //二、 將XML轉化爲JavaBean
- import java.io.StringReader;
- import org.apache.commons.betwixt.io.BeanReader;
- public class ReadApp {
- public static final void main(String args[]) throws Exception{
- // 先建立一個XML,因爲這裏僅是做爲例子,因此咱們硬編碼了一段XML內容
- StringReader xmlReader = new StringReader(
- "<?xml version=’1.0′ encoding=’UTF-8′ ?> <person><age>25</age><name>James Smith</name></person>");
- //建立BeanReader
- BeanReader beanReader = new BeanReader();
- //配置reader
- beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
- beanReader.getBindingConfiguration().setMapIDs(false);
- //註冊beans,以便betwixt知道XML將要被轉化爲一個什麼Bean
- beanReader.registerBeanClass("person", PersonBean.class);
- //如今咱們對XML進行解析
- PersonBean person = (PersonBean) beanReader.parse(xmlReader);
- //輸出結果
- System.out.println(person);
- }
- }
三、Codec
提供了一些公共的編解碼實現,好比Base64, Hex, MD5,Phonetic and URLs等等。web
Java代碼
- //Base64編解碼
- private static String encodeTest(String str){
- Base64 base64 = new Base64();
- try {
- str = base64.encodeToString(str.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- System.out.println("Base64 編碼後:"+str);
- return str;
- }
-
- private static void decodeTest(String str){
- Base64 base64 = new Base64();
- //str = Arrays.toString(Base64.decodeBase64(str));
- str = new String(Base64.decodeBase64(str));
- System.out.println("Base64 解碼後:"+str);
- }
四、Collections
對java.util的擴展封裝,處理數據仍是挺靈活的。
sql
org.apache.commons.collections – Commons Collections自定義的一組公用的接口和工具類數據庫
org.apache.commons.collections.bag – 實現Bag接口的一組類apache
org.apache.commons.collections.bidimap – 實現BidiMap系列接口的一組類編程
org.apache.commons.collections.buffer – 實現Buffer接口的一組類api
org.apache.commons.collections.collection – 實現java.util.Collection接口的一組類數組
org.apache.commons.collections.comparators – 實現java.util.Comparator接口的一組類
org.apache.commons.collections.functors – Commons Collections自定義的一組功能類
org.apache.commons.collections.iterators – 實現java.util.Iterator接口的一組類
org.apache.commons.collections.keyvalue – 實現集合和鍵/值映射相關的一組類
org.apache.commons.collections.list – 實現java.util.List接口的一組類
org.apache.commons.collections.map – 實現Map系列接口的一組類
org.apache.commons.collections.set – 實現Set系列接口的一組類
Java代碼
- /**
- * 獲得集合裏按順序存放的key以後的某一Key
- */
- 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"
-
- /**
- * 經過key獲得value
- * 經過value獲得key
- * 將map裏的key和value對調
- */
-
- BidiMap bidi = new TreeBidiMap();
- bidi.put("SIX", "6");
- bidi.get("SIX"); // returns "6"
- bidi.getKey("6"); // returns "SIX"
- // bidi.removeValue("6"); // removes the mapping
- BidiMap inverse = bidi.inverseBidiMap(); // returns a map with keys and values swapped
- System.out.println(inverse);
-
- /**
- * 獲得兩個集合中相同的元素
- */
- List<String> list1 = new ArrayList<String>();
- list1.add("1");
- list1.add("2");
- list1.add("3");
- List<String> list2 = new ArrayList<String>();
- list2.add("2");
- list2.add("3");
- list2.add("5");
- Collection c = CollectionUtils.retainAll(list1, list2);
- System.out.println(c);
五、Compress
commons compress中的打包、壓縮類庫。
Java代碼
- //建立壓縮對象
- ZipArchiveEntry entry = new ZipArchiveEntry("CompressTest");
- //要壓縮的文件
- File f=new File("e:\\test.pdf");
- FileInputStream fis=new FileInputStream(f);
- //輸出的對象 壓縮的文件
- ZipArchiveOutputStream zipOutput=new ZipArchiveOutputStream(new File("e:\\test.zip"));
- zipOutput.putArchiveEntry(entry);
- int i=0,j;
- while((j=fis.read()) != -1)
- {
- zipOutput.write(j);
- i++;
- System.out.println(i);
- }
- zipOutput.closeArchiveEntry();
- zipOutput.close();
- fis.close();
六、Configuration
用來幫助處理配置文件的,支持不少種存儲方式。
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
Java代碼
- //舉一個Properties的簡單例子
- # usergui.properties
- colors.background = #FFFFFF
- colors.foreground = #000080
- window.width = 500
- window.height = 300
-
- PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
- config.setProperty("colors.background", "#000000);
- config.save();
-
- config.save("usergui.backup.properties);//save a copy
- Integer integer = config.getInteger("window.width");
七、DBCP
(Database Connection Pool)是一個依賴Jakarta commons-pool對象池機制的數據庫鏈接池,Tomcat的數據源使用的就是DBCP。
Java代碼
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import org.apache.commons.pool.ObjectPool;
- import org.apache.commons.pool.impl.GenericObjectPool;
- import org.apache.commons.dbcp.ConnectionFactory;
- import org.apache.commons.dbcp.PoolingDataSource;
- import org.apache.commons.dbcp.PoolableConnectionFactory;
- import org.apache.commons.dbcp.DriverManagerConnectionFactory;
- //官方示例
- public class PoolingDataSources {
-
- public static void main(String[] args) {
- System.out.println("加載jdbc驅動");
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- System.out.println("Done.");
- //
- System.out.println("設置數據源");
- DataSource dataSource = setupDataSource("jdbc:oracle:thin:@localhost:1521:test");
- System.out.println("Done.");
-
- //
- Connection conn = null;
- Statement stmt = null;
- ResultSet rset = null;
-
- try {
- System.out.println("Creating connection.");
- conn = dataSource.getConnection();
- System.out.println("Creating statement.");
- stmt = conn.createStatement();
- System.out.println("Executing statement.");
- rset = stmt.executeQuery("select * from person");
- System.out.println("Results:");
- int numcols = rset.getMetaData().getColumnCount();
- while(rset.next()) {
- for(int i=0;i<=numcols;i++) {
- System.out.print("\t" + rset.getString(i));
- }
- System.out.println("");
- }
- } catch(SQLException e) {
- e.printStackTrace();
- } finally {
- try { if (rset != null) rset.close(); } catch(Exception e) { }
- try { if (stmt != null) stmt.close(); } catch(Exception e) { }
- try { if (conn != null) conn.close(); } catch(Exception e) { }
- }
- }
-
- public static DataSource setupDataSource(String connectURI) {
- //設置鏈接地址
- ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
- connectURI, null);
-
- // 建立鏈接工廠
- PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
- connectionFactory);
-
- //獲取GenericObjectPool 鏈接的實例
- ObjectPool connectionPool = new GenericObjectPool(
- poolableConnectionFactory);
-
- // 建立 PoolingDriver
- PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
-
- return dataSource;
- }
- }
八、DbUtils
Apache組織提供的一個資源JDBC工具類庫,它是對JDBC的簡單封裝,對傳統操做數據庫的類進行二次封裝,能夠把結果集轉化成List。,同時也不影響程序的性能。
DbUtils類:啓動類
ResultSetHandler接口:轉換類型接口
MapListHandler類:實現類,把記錄轉化成List
BeanListHandler類:實現類,把記錄轉化成List,使記錄爲JavaBean類型的對象
Qrery Runner類:執行SQL語句的類
Java代碼
- import org.apache.commons.dbutils.DbUtils;
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.dbutils.handlers.BeanListHandler;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.List;
- //轉換成list
- public class BeanLists {
- public static void main(String[] args) {
- Connection conn = null;
- String url = "jdbc:mysql://localhost:3306/ptest";
- String jdbcDriver = "com.mysql.jdbc.Driver";
- String user = "root";
- String password = "ptest";
-
- DbUtils.loadDriver(jdbcDriver);
- try {
- conn = DriverManager.getConnection(url, user, password);
- QueryRunner qr = new QueryRunner();
- List results = (List) qr.query(conn, "select id,name from person", new BeanListHandler(Person.class));
- for (int i = 0; i < results.size(); i++) {
- Person p = (Person) results.get(i);
- System.out.println("id:" + p.getId() + ",name:" + p.getName());
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DbUtils.closeQuietly(conn);
- }
- }
- }
-
- public class Person{
- private Integer id;
- private String name;
-
- //省略set, get方法
- }
-
- import org.apache.commons.dbutils.DbUtils;
- import org.apache.commons.dbutils.QueryRunner;
- import org.apache.commons.dbutils.handlers.MapListHandler;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
-
- import java.util.List;
- import java.util.Map;
- //轉換成map
- public class MapLists {
- public static void main(String[] args) {
- Connection conn = null;
- String url = "jdbc:mysql://localhost:3306/ptest";
- String jdbcDriver = "com.mysql.jdbc.Driver";
- String user = "root";
- String password = "ptest";
-
- DbUtils.loadDriver(jdbcDriver);
- try {
- conn = DriverManager.getConnection(url, user, password);
- QueryRunner qr = new QueryRunner();
- List results = (List) qr.query(conn, "select id,name from person", 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("name"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- DbUtils.closeQuietly(conn);
- }
- }
- }
九、Email
提供的一個開源的API,是對javamail的封裝。
Java代碼
- //用commons email發送郵件
- public static void main(String args[]){
- Email email = new SimpleEmail();
- email.setHostName("smtp.googlemail.com");
- email.setSmtpPort(465);
- email.setAuthenticator(new DefaultAuthenticator("username", "password"));
- email.setSSLOnConnect(true);
- email.setFrom("user@gmail.com");
- email.setSubject("TestMail");
- email.setMsg("This is a test mail ... :-)");
- email.addTo("foo@bar.com");
- email.send();
- }
十、FileUpload
java web文件上傳功能。
Java代碼
- //官方示例:
- //* 檢查請求是否含有上傳文件
- // Check that we have a file upload request
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
-
- //如今咱們獲得了items的列表
-
- //若是你的應用近於最簡單的狀況,上面的處理就夠了。但咱們有時候仍是須要更多的控制。
- //下面提供了幾種控制選擇:
- // Create a factory for disk-based file items
- DiskFileItemFactory factory = new DiskFileItemFactory();
-
- // Set factory constraints
- factory.setSizeThreshold(yourMaxMemorySize);
- factory.setRepository(yourTempDirectory);
-
- // Create a new file upload handler
- ServletFileUpload upload = new ServletFileUpload(factory);
-
- // 設置最大上傳大小
- upload.setSizeMax(yourMaxRequestSize);
-
- // 解析全部請求
- List /* FileItem */ items = upload.parseRequest(request);
-
- // Create a factory for disk-based file items
- DiskFileItemFactory factory = new DiskFileItemFactory(
- yourMaxMemorySize, yourTempDirectory);
-
- //一旦解析完成,你須要進一步處理item的列表。
- // Process the uploaded items
- Iterator iter = items.iterator();
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
-
- if (item.isFormField()) {
- processFormField(item);
- } else {
- processUploadedFile(item);
- }
- }
-
- //區分數據是否爲簡單的表單數據,若是是簡單的數據:
- // processFormField
- if (item.isFormField()) {
- String name = item.getFieldName();
- String value = item.getString();
- //...省略步驟
- }
-
- //若是是提交的文件:
- // processUploadedFile
- if (!item.isFormField()) {
- String fieldName = item.getFieldName();
- String fileName = item.getName();
- String contentType = item.getContentType();
- boolean isInMemory = item.isInMemory();
- long sizeInBytes = item.getSize();
- //...省略步驟
- }
-
- //對於這些item,咱們一般要把它們寫入文件,或轉爲一個流
- // Process a file upload
- if (writeToFile) {
- File uploadedFile = new File(...);
- item.write(uploadedFile);
- } else {
- InputStream uploadedStream = item.getInputStream();
- //...省略步驟
- uploadedStream.close();
- }
-
- //或轉爲字節數組保存在內存中:
- // Process a file upload in memory
- byte[] data = item.get();
- //...省略步驟
- //若是這個文件真的很大,你可能會但願向用戶報告到底傳了多少到服務端,讓用戶瞭解上傳的過程
- //Create a progress listener
- ProgressListener progressListener = new ProgressListener(){
- public void update(long pBytesRead, long pContentLength, int pItems) {
- System.out.println("We are currently reading item " + pItems);
- if (pContentLength == -1) {
- System.out.println("So far, " + pBytesRead + " bytes have been read.");
- } else {
- System.out.println("So far, " + pBytesRead + " of " + pContentLength
- + " bytes have been read.");
- }
- }
- };
- upload.setProgressListener(progressListener);
十一、HttpClient
基於HttpCore實 現的一個HTTP/1.1兼容的HTTP客戶端,它提供了一系列可重用的客戶端身份驗證、HTTP狀態保持、HTTP鏈接管理module。
Java代碼
- //GET方法
- import java.io.IOException;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
-
- public class GetSample{
- public static void main(String[] args) {
- // 構造HttpClient的實例
- HttpClient httpClient = new HttpClient();
- // 建立GET方法的實例
- GetMethod getMethod = new GetMethod("http://www.ibm.com");
- // 使用系統提供的默認的恢復策略
- getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
- new DefaultHttpMethodRetryHandler());
- try {
- // 執行getMethod
- int statusCode = httpClient.executeMethod(getMethod);
- if (statusCode != HttpStatus.SC_OK) {
- System.err.println("Method failed: "
- + getMethod.getStatusLine());
- }
- // 讀取內容
- byte[] responseBody = getMethod.getResponseBody();
- // 處理內容
- System.out.println(new String(responseBody));
- } catch (HttpException e) {
- // 發生致命的異常,多是協議不對或者返回的內容有問題
- System.out.println("Please check your provided http address!");
- e.printStackTrace();
- } catch (IOException e) {
- // 發生網絡異常
- e.printStackTrace();
- } finally {
- // 釋放鏈接
- getMethod.releaseConnection();
- }
- }
- }
-
- //POST方法
- import java.io.IOException;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
-
- public class PostSample{
- public static void main(String[] args) {
- // 構造HttpClient的實例
- HttpClient httpClient = new HttpClient();
- // 建立POST方法的實例
- String url = "http://www.oracle.com/";
- PostMethod postMethod = new PostMethod(url);
- // 填入各個表單域的值
- NameValuePair[] data = { new NameValuePair("id", "youUserName"),
- new NameValuePair("passwd", "yourPwd") };
- // 將表單的值放入postMethod中
- postMethod.setRequestBody(data);
- // 執行postMethod
- int statusCode = httpClient.executeMethod(postMethod);
- // HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發
- // 301或者302
- if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
- statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
- // 從頭中取出轉向的地址
- Header locationHeader = postMethod.getResponseHeader("location");
- String location = null;
- if (locationHeader != null) {
- location = locationHeader.getValue();
- System.out.println("The page was redirected to:" + location);
- } else {
- System.err.println("Location field value is null.");
- }
- return;
- }
- }
- }
十二、IO
對java.io的擴展 操做文件很是方便。
Java代碼
- //1.讀取Stream
-
- //標準代碼:
- InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
- try {
- InputStreamReader inR = new InputStreamReader( in );
- BufferedReader buf = new BufferedReader( inR );
- String line;
- while ( ( line = buf.readLine() ) != null ) {
- System.out.println( line );
- }
- } finally {
- in.close();
- }
-
- //使用IOUtils
-
- InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
- try {
- System.out.println( IOUtils.toString( in ) );
- } finally {
- IOUtils.closeQuietly(in);
- }
-
- //2.讀取文件
- File file = new File("/commons/io/project.properties");
- List lines = FileUtils.readLines(file, "UTF-8");
- //3.察看剩餘空間
- long freeSpace = FileSystemUtils.freeSpace("C:/");
1三、Lang
主要是一些公共的工具集合,好比對字符、數組的操做等等。
![](http://static.javashuo.com/static/loading.gif)
Java代碼
- // 1 合併兩個數組: org.apache.commons.lang. ArrayUtils
- // 有時咱們須要將兩個數組合併爲一個數組,用ArrayUtils就很是方便,示例以下:
- private static void testArr() {
- String[] s1 = new String[] { "1", "2", "3" };
- String[] s2 = new String[] { "a", "b", "c" };
- String[] s = (String[]) ArrayUtils.addAll(s1, s2);
- for (int i = 0; i < s.length; i++) {
- System.out.println(s[i]);
- }
- String str = ArrayUtils.toString(s);
- str = str.substring(1, str.length() - 1);
- System.out.println(str + ">>" + str.length());
-
- }
- //2 截取從from開始字符串
- StringUtils.substringAfter("SELECT * FROM PERSON ", "from");
- //3 判斷該字符串是否是爲數字(0~9)組成,若是是,返回true 但該方法不識別有小數點和 請注意。
- StringUtils.isNumeric("454534"); //返回true
- //4.取得類名
- System.out.println(ClassUtils.getShortClassName(Test.class));
- //取得其包名
- System.out.println(ClassUtils.getPackageName(Test.class));
-
- //5.NumberUtils
- System.out.println(NumberUtils.stringToInt("6"));
- //6.五位的隨機字母和數字
- System.out.println(RandomStringUtils.randomAlphanumeric(5));
- //7.StringEscapeUtils
- System.out.println(StringEscapeUtils.escapeHtml("<html>"));
- //輸出結果爲<html>
- System.out.println(StringEscapeUtils.escapeJava("String"));
-
- //8.StringUtils,判斷是不是空格字符
- System.out.println(StringUtils.isBlank(" "));
- //將數組中的內容以,分隔
- System.out.println(StringUtils.join(test,","));
- //在右邊加下字符,使之總長度爲6
- System.out.println(StringUtils.rightPad("abc", 6, 'T'));
- //首字母大寫
- System.out.println(StringUtils.capitalize("abc"));
- //Deletes all whitespaces from a String 刪除全部空格
- System.out.println( StringUtils.deleteWhitespace(" ab c "));
- //判斷是否包含這個字符
- System.out.println( StringUtils.contains("abc", "ba"));
- //表示左邊兩個字符
- System.out.println( StringUtils.left("abc", 2));
- System.out.println(NumberUtils.stringToInt("33"));
1四、Logging
提供的是一個Java 的日誌接口,同時兼顧輕量級和不依賴於具體的日誌實現工具。
Java代碼
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- public class CommonLogTest {
- private static Log log = LogFactory.getLog(CommonLogTest.class);
- //日誌打印
- public static void main(String[] args) {
- log.error("ERROR");
- log.debug("DEBUG");
- log.warn("WARN");
- log.info("INFO");
- log.trace("TRACE");
- System.out.println(log.getClass());
- }
-
- }
1五、Validator
通用驗證系統,該組件提供了客戶端和服務器端的數據驗證框架。
驗證日期
Java代碼
- // 獲取日期驗證
- DateValidator validator = DateValidator.getInstance();
-
- // 驗證/轉換日期
- Date fooDate = validator.validate(fooString, "dd/MM/yyyy");
- if (fooDate == null) {
- // 錯誤 不是日期
- return;
- }
表達式驗證
Java代碼
- // 設置參數
- boolean caseSensitive = false;
- String regex1 = "^([A-Z]*)(?:\\-)([A-Z]*)*$"
- String regex2 = "^([A-Z]*)$";
- String[] regexs = new String[] {regex1, regex1};
-
- // 建立驗證
- RegexValidator validator = new RegexValidator(regexs, caseSensitive);
-
- // 驗證返回boolean
- boolean valid = validator.isValid("abc-def");
-
- // 驗證返回字符串
- String result = validator.validate("abc-def");
-
- // 驗證返回數組
- String[] groups = validator.match("abc-def");
配置文件中使用驗證
Xml代碼
- <form-validation>
- <global>
- <validator name="required"
- classname="org.apache.commons.validator.TestValidator"
- method="validateRequired"
- methodParams="java.lang.Object, org.apache.commons.validator.Field"/>
- </global>
- <formset>
- </formset>
- </form-validation>
-
- 添加姓名驗證.
-
- <form-validation>
- <global>
- <validator name="required"
- classname="org.apache.commons.validator.TestValidator"
- method="validateRequired"
- methodParams="java.lang.Object, org.apache.commons.validator.Field"/>
- </global>
- <formset>
- <form name="nameForm">
- <field property="firstName" depends="required">
- <arg0 key="nameForm.firstname.displayname"/>
- </field>
- <field property="lastName" depends="required">
- <arg0 key="nameForm.lastname.displayname"/>
- </field>
- </form>
- </formset>
- </form-validation>
驗證類
Java代碼
- Excerpts from org.apache.commons.validator.RequiredNameTest
- //加載驗證配置文件
- InputStream in = this.getClass().getResourceAsStream("validator-name-required.xml");
-
- ValidatorResources resources = new ValidatorResources(in);
- //這個是本身建立的bean 我這裏省略了
- Name name = new Name();
-
- Validator validator = new Validator(resources, "nameForm");
- //設置參數
- validator.setParameter(Validator.BEAN_PARAM, name);
-
-
- Map results = null;
- //驗證
- results = validator.validate();
-
- if (results.get("firstName") == null) {
- //驗證成功
- } else {
- //有錯誤 int errors = ((Integer)results.get("firstName")).intValue();
- }