转自:http://www.cnblogs.com/younggun/p/3247261.html
Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。下面是我这几年做开发过程中自己用过的工具类做简单介绍。
组件 | 功能介绍 |
BeanUtils | 提供了对于JavaBean进行各种操作,克隆对象,属性等等. |
Betwixt | XML与Java对象之间相互转换. |
Codec | 处理常用的编码方法的工具类包例如DES、SHA1、MD5、Base64等. |
Collections | java集合框架操作. |
Compress | java提供文件打包 压缩类库. |
Configuration | 一个java应用程序的配置管理类库. |
DBCP | 提供数据库连接池服务. |
DbUtils | 提供对jdbc的操作封装来简化数据查询和记录读取操作. |
java发送邮件 对javamail的封装. | |
FileUpload | 提供文件上传功能. |
HttpClien | 提供HTTP客户端与服务器的各种通讯操作. 现在已改成HttpComponents |
IO | io工具的封装. |
Lang | Java基本对象方法的工具类包 如:StringUtils,ArrayUtils等等. |
Logging | 提供的是一个Java 的日志接口. |
Validator | 提供了客户端和服务器端的数据验证框架. |
1、BeanUtils
提供了对于JavaBean进行各种操作, 比如对象,属性复制等等。
- //1、克隆对象
- //新创建一个普通JavaBean,用来作为被克隆的对象
- publicclassPerson{
- privateStringname="";
- privateStringemail="";
- privateintage;
- //省略set,get方法
- }
- //再创建一个Test类,其中在main方法中代码如下:
- importjava.lang.reflect.InvocationTargetException;
- importjava.util.HashMap;
- importjava.util.Map;
- importorg.apache.commons.beanutils.BeanUtils;
- importorg.apache.commons.beanutils.ConvertUtils;
- publicclassTest{
- /**
- *@paramargs
- */
- publicstaticvoidmain(String[]args){
- Personperson=newPerson();
- person.setName("tom");
- person.setAge(21);
- try{
- //克隆
- Personperson2=(Person)BeanUtils.cloneBean(person);
- System.out.println(person2.getName()+">>"+person2.getAge());
- }catch(IllegalAccessExceptione){
- e.printStackTrace();
- }catch(InstantiationExceptione){
- e.printStackTrace();
- }catch(InvocationTargetExceptione){
- e.printStackTrace();
- }catch(NoSuchMethodExceptione){
- e.printStackTrace();
- }
- }
- }
- //原理也是通过Java的反射机制来做的。
- //2、将一个Map对象转化为一个Bean
- //这个Map对象的key必须与Bean的属性相对应。
- Mapmap=newHashMap();
- map.put("name","tom");
- map.put("email","tom@");
- map.put("age","21");
- //将map转化为一个Person对象
- Personperson=newPerson();
- BeanUtils.populate(person,map);
- //通过上面的一行代码,此时person的属性就已经具有了上面所赋的值了。
- //将一个Bean转化为一个Map对象了,如下:
- Mapmap=BeanUtils.describe(person)
2、Betwixt
XML与Java对象之间相互转换。
- //1、将JavaBean转为XML内容
- //新创建一个Person类
- publicclassPerson{
- privateStringname;
- privateintage;
- /**Needtoallowbeantobecreatedviareflection*/
- publicPersonBean(){
- }
- publicPersonBean(Stringname,intage){
- this.name=name;
- this.age=age;
- }
- //省略set,get方法
- publicStringtoString(){
- return"PersonBean[name='"+name+"',age='"+age+"']";
- }
- }
- //再创建一个WriteApp类:
- importjava.io.StringWriter;
- importorg.apache.commons.betwixt.io.BeanWriter;
- publicclassWriteApp{
- /**
- *创建一个例子Bean,并将它转化为XML.
- */
- publicstaticfinalvoidmain(String[]args)throwsException{
- //先创建一个StringWriter,我们将把它写入为一个字符串
- StringWriteroutputWriter=newStringWriter();
- //Betwixt在这里仅仅是将Bean写入为一个片断
- //所以如果要想完整的XML内容,我们应该写入头格式
- outputWriter.write(“<?xmlversion=’1.0′encoding=’UTF-8′?> ”);
- //创建一个BeanWriter,其将写入到我们预备的stream中
- BeanWriterbeanWriter=newBeanWriter(outputWriter);
- //配置betwixt
- //更多详情请参考javadocs或最新的文档
- beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
- beanWriter.getBindingConfiguration().setMapIDs(false);
- beanWriter.enablePrettyPrint();
- //如果这个地方不传入XML的根节点名,Betwixt将自己猜测是什么
- //但是让我们将例子Bean名作为根节点吧
- beanWriter.write(“person”,newPersonBean(“JohnSmith”,21));
- //输出结果
- System.out.println(outputWriter.toString());
- //Betwixt写的是片断而不是一个文档,所以不要自动的关闭掉writers或者streams,
- //但这里仅仅是一个例子,不会做更多事情,所以可以关掉
- outputWriter.close();
- }
- }
- //2、将XML转化为JavaBean
- importjava.io.StringReader;
- importorg.apache.commons.betwixt.io.BeanReader;
- publicclassReadApp{
- publicstaticfinalvoidmain(Stringargs[])throwsException{
- //先创建一个XML,由于这里仅是作为例子,所以我们硬编码了一段XML内容
- StringReaderxmlReader=newStringReader(
- "<?xmlversion=’1.0′encoding=’UTF-8′?><person><age>25</age><name>JamesSmith</name></person>");
- //创建BeanReader
- BeanReaderbeanReader=newBeanReader();
- //配置reader
- beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
- beanReader.getBindingConfiguration().setMapIDs(false);
- //注册beans,以便betwixt知道XML将要被转化为一个什么Bean
- beanReader.registerBeanClass("person",PersonBean.class);
- //现在我们对XML进行解析
- PersonBeanperson=(PersonBean)beanReader.parse(xmlReader);
- //输出结果
- System.out.println(person);
- }
- }
3、Codec
提供了一些公共的编解码实现,比如Base64, Hex, MD5,Phonetic and URLs等等。
- //Base64编解码
- privatestaticStringencodeTest(Stringstr){
- Base64base64=newBase64();
- try{
- str=base64.encodeToString(str.getBytes("UTF-8"));
- }catch(UnsupportedEncodingExceptione){
- e.printStackTrace();
- }
- System.out.println("Base64编码后:"+str);
- returnstr;
- }
- privatestaticvoiddecodeTest(Stringstr){
- Base64base64=newBase64();
- //str=Arrays.toString(Base64.decodeBase64(str));
- str=newString(Base64.decodeBase64(str));
- System.out.println("Base64解码后:"+str);
- }
4、Collections
对java.util的扩展封装,处理数据还是挺灵活的。
org.apache.commons.collections – Commons Collections自定义的一组公用的接口和工具类
org.apache.commons.collections.bag –实现Bag接口的一组类
org.apache.commons.collections.bidimap –实现BidiMap系列接口的一组类
org.apache.commons.collections.buffer –实现Buffer接口的一组类
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系列接口的一组类
- /**
- *得到集合里按顺序存放的key之后的某一Key
- */
- OrderedMapmap=newLinkedMap();
- 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对调
- */
- BidiMapbidi=newTreeBidiMap();
- bidi.put("SIX","6");
- bidi.get("SIX");//returns"6"
- bidi.getKey("6");//returns"SIX"
- //bidi.removeValue("6");//removesthemapping
- BidiMapinverse=bidi.inverseBidiMap();//returnsamapwithkeysandvaluesswapped
- System.out.println(inverse);
- /**
- *得到两个集合中相同的元素
- */
- List<String>list1=newArrayList<String>();
- list1.add("1");
- list1.add("2");
- list1.add("3");
- List<String>list2=newArrayList<String>();
- list2.add("2");
- list2.add("3");
- list2.add("5");
- Collectionc=CollectionUtils.retainAll(list1,list2);
- System.out.println(c);
5、Compress
commons compress中的打包、压缩类库。
- //创建压缩对象
- ZipArchiveEntryentry=newZipArchiveEntry("CompressTest");
- //要压缩的文件
- Filef=newFile("e:\test.pdf");
- FileInputStreamfis=newFileInputStream(f);
- //输出的对象压缩的文件
- ZipArchiveOutputStreamzipOutput=newZipArchiveOutputStream(newFile("e:\test.zip"));
- zipOutput.putArchiveEntry(entry);
- inti=0,j;
- while((j=fis.read())!=-1)
- {
- zipOutput.write(j);
- i++;
- System.out.println(i);
- }
- zipOutput.closeArchiveEntry();
- zipOutput.close();
- fis.close();
6、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
- //举一个Properties的简单例子
- #usergui.properties
- colors.background=#FFFFFF
- colors.foreground=#000080
- window.width=500
- window.height=300
- PropertiesConfigurationconfig=newPropertiesConfiguration("usergui.properties");
- config.setProperty("colors.background","#000000);
- config.save();
- config.save("usergui.backup.properties);//saveacopy
- Integerinteger=config.getInteger("window.width");
7、DBCP
(Database Connection Pool)是一个依赖Jakarta commons-pool对象池机制的数据库连接池,Tomcat的数据源使用的就是DBCP。
- importjavax.sql.DataSource;
- importjava.sql.Connection;
- importjava.sql.Statement;
- importjava.sql.ResultSet;
- importjava.sql.SQLException;
- importorg.apache.commons.pool.ObjectPool;
- importorg.apache.commons.pool.impl.GenericObjectPool;
- importorg.apache.commons.dbcp.ConnectionFactory;
- importorg.apache.commons.dbcp.PoolingDataSource;
- importorg.apache.commons.dbcp.PoolableConnectionFactory;
- importorg.apache.commons.dbcp.DriverManagerConnectionFactory;
- //官方示例
- publicclassPoolingDataSources{
- publicstaticvoidmain(String[]args){
- System.out.println("加载jdbc驱动");
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }
- System.out.println("Done.");
- //
- System.out.println("设置数据源");
- DataSourcedataSource=setupDataSource("jdbc:oracle:thin:@localhost:1521:test");
- System.out.println("Done.");
- //
- Connectionconn=null;
- Statementstmt=null;
- ResultSetrset=null;
- try{
- System.out.println("Creatingconnection.");
- conn=dataSource.getConnection();
- System.out.println("Creatingstatement.");
- stmt=conn.createStatement();
- System.out.println("Executingstatement.");
- rset=stmt.executeQuery("select*fromperson");
- System.out.println("Results:");
- intnumcols=rset.getMetaData().getColumnCount();
- while(rset.next()){
- for(inti=0;i<=numcols;i++){
- System.out.print(" "+rset.getString(i));
- }
- System.out.println("");
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }finally{
- try{if(rset!=null)rset.close();}catch(Exceptione){}
- try{if(stmt!=null)stmt.close();}catch(Exceptione){}
- try{if(conn!=null)conn.close();}catch(Exceptione){}
- }
- }
- publicstaticDataSourcesetupDataSource(StringconnectURI){
- //设置连接地址
- ConnectionFactoryconnectionFactory=newDriverManagerConnectionFactory(
- connectURI,null);
- //创建连接工厂
- PoolableConnectionFactorypoolableConnectionFactory=newPoolableConnectionFactory(
- connectionFactory);
- //获取GenericObjectPool连接的实例
- ObjectPoolconnectionPool=newGenericObjectPool(
- poolableConnectionFactory);
- //创建PoolingDriver
- PoolingDataSourcedataSource=newPoolingDataSource(connectionPool);
- returndataSource;
- }
- }
8、DbUtils
Apache组织提供的一个资源JDBC工具类库,它是对JDBC的简单封装,对传统操作数据库的类进行二次封装,可以把结果集转化成List。,同时也不影响程序的性能。
DbUtils类:启动类
ResultSetHandler接口:转换类型接口
MapListHandler类:实现类,把记录转化成List
BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象
Qrery Runner类:执行SQL语句的类
- importorg.apache.commons.dbutils.DbUtils;
- importorg.apache.commons.dbutils.QueryRunner;
- importorg.apache.commons.dbutils.handlers.BeanListHandler;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.SQLException;
- importjava.util.List;
- //转换成list
- publicclassBeanLists{
- publicstaticvoidmain(String[]args){
- Connectionconn=null;
- Stringurl="jdbc:mysql://localhost:3306/ptest";
- StringjdbcDriver="com.mysql.jdbc.Driver";
- Stringuser="root";
- Stringpassword="ptest";
- DbUtils.loadDriver(jdbcDriver);
- try{
- conn=DriverManager.getConnection(url,user,password);
- QueryRunnerqr=newQueryRunner();
- Listresults=(List)qr.query(conn,"selectid,namefromperson",newBeanListHandler(Person.class));
- for(inti=0;i<results.size();i++){
- Personp=(Person)results.get(i);
- System.out.println("id:"+p.getId()+",name:"+p.getName());
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }finally{
- DbUtils.closeQuietly(conn);
- }
- }
- }
- publicclassPerson{
- privateIntegerid;
- privateStringname;
- //省略set,get方法
- }
- importorg.apache.commons.dbutils.DbUtils;
- importorg.apache.commons.dbutils.QueryRunner;
- importorg.apache.commons.dbutils.handlers.MapListHandler;
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.SQLException;
- importjava.util.List;
- importjava.util.Map;
- //转换成map
- publicclassMapLists{
- publicstaticvoidmain(String[]args){
- Connectionconn=null;
- Stringurl="jdbc:mysql://localhost:3306/ptest";
- StringjdbcDriver="com.mysql.jdbc.Driver";
- Stringuser="root";
- Stringpassword="ptest";
- DbUtils.loadDriver(jdbcDriver);
- try{
- conn=DriverManager.getConnection(url,user,password);
- QueryRunnerqr=newQueryRunner();
- Listresults=(List)qr.query(conn,"selectid,namefromperson",newMapListHandler());
- for(inti=0;i<results.size();i++){
- Mapmap=(Map)results.get(i);
- System.out.println("id:"+map.get("id")+",name:"+map.get("name"));
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }finally{
- DbUtils.closeQuietly(conn);
- }
- }
- }
9、Email
提供的一个开源的API,是对javamail的封装。
- //用commonsemail发送邮件
- publicstaticvoidmain(Stringargs[]){
- Emailemail=newSimpleEmail();
- email.setHostName("smtp.googlemail.com");
- email.setSmtpPort(465);
- email.setAuthenticator(newDefaultAuthenticator("username","password"));
- email.setSSLOnConnect(true);
- email.setFrom("user@gmail.com");
- email.setSubject("TestMail");
- email.setMsg("Thisisatestmail...:-)");
- email.addTo("foo@bar.com");
- email.send();
- }
10、FileUpload
java web文件上传功能。
- //官方示例:
- //*检查请求是否含有上传文件
- //Checkthatwehaveafileuploadrequest
- booleanisMultipart=ServletFileUpload.isMultipartContent(request);
- //现在我们得到了items的列表
- //如果你的应用近于最简单的情况,上面的处理就够了。但我们有时候还是需要更多的控制。
- //下面提供了几种控制选择:
- //Createafactoryfordisk-basedfileitems
- DiskFileItemFactoryfactory=newDiskFileItemFactory();
- //Setfactoryconstraints
- factory.setSizeThreshold(yourMaxMemorySize);
- factory.setRepository(yourTempDirectory);
- //Createanewfileuploadhandler
- ServletFileUploadupload=newServletFileUpload(factory);
- //设置最大上传大小
- upload.setSizeMax(yourMaxRequestSize);
- //解析所有请求
- List/*FileItem*/items=upload.parseRequest(request);
- //Createafactoryfordisk-basedfileitems
- DiskFileItemFactoryfactory=newDiskFileItemFactory(
- yourMaxMemorySize,yourTempDirectory);
- //一旦解析完成,你需要进一步处理item的列表。
- //Processtheuploadeditems
- Iteratoriter=items.iterator();
- while(iter.hasNext()){
- FileItemitem=(FileItem)iter.next();
- if(item.isFormField()){
- processFormField(item);
- }else{
- processUploadedFile(item);
- }
- }
- //区分数据是否为简单的表单数据,如果是简单的数据:
- //processFormField
- if(item.isFormField()){
- Stringname=item.getFieldName();
- Stringvalue=item.getString();
- //...省略步骤
- }
- //如果是提交的文件:
- //processUploadedFile
- if(!item.isFormField()){
- StringfieldName=item.getFieldName();
- StringfileName=item.getName();
- StringcontentType=item.getContentType();
- booleanisInMemory=item.isInMemory();
- longsizeInBytes=item.getSize();
- //...省略步骤
- }
- //对于这些item,我们通常要把它们写入文件,或转为一个流
- //Processafileupload
- if(writeToFile){
- FileuploadedFile=newFile(...);
- item.write(uploadedFile);
- }else{
- InputStreamuploadedStream=item.getInputStream();
- //...省略步骤
- uploadedStream.close();
- }
- //或转为字节数组保存在内存中:
- //Processafileuploadinmemory
- byte[]data=item.get();
- //...省略步骤
- //如果这个文件真的很大,你可能会希望向用户报告到底传了多少到服务端,让用户了解上传的过程
- //Createaprogresslistener
- ProgressListenerprogressListener=newProgressListener(){
- publicvoidupdate(longpBytesRead,longpContentLength,intpItems){
- System.out.println("Wearecurrentlyreadingitem"+pItems);
- if(pContentLength==-1){
- System.out.println("Sofar,"+pBytesRead+"byteshavebeenread.");
- }else{
- System.out.println("Sofar,"+pBytesRead+"of"+pContentLength
- +"byteshavebeenread.");
- }
- }
- };
- upload.setProgressListener(progressListener);
11、HttpClient
基于HttpCore实 现的一个HTTP/1.1兼容的HTTP客户端,它提供了一系列可重用的客户端身份验证、HTTP状态保持、HTTP连接管理module。
- //GET方法
- importjava.io.IOException;
- importorg.apache.commons.httpclient.*;
- importorg.apache.commons.httpclient.methods.GetMethod;
- importorg.apache.commons.httpclient.params.HttpMethodParams;
- publicclassGetSample{
- publicstaticvoidmain(String[]args){
- //构造HttpClient的实例
- HttpClienthttpClient=newHttpClient();
- //创建GET方法的实例
- GetMethodgetMethod=newGetMethod("http://www.ibm.com");
- //使用系统提供的默认的恢复策略
- getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
- newDefaultHttpMethodRetryHandler());
- try{
- //执行getMethod
- intstatusCode=httpClient.executeMethod(getMethod);
- if(statusCode!=HttpStatus.SC_OK){
- System.err.println("Methodfailed:"
- +getMethod.getStatusLine());
- }
- //读取内容
- byte[]responseBody=getMethod.getResponseBody();
- //处理内容
- System.out.println(newString(responseBody));
- }catch(HttpExceptione){
- //发生致命的异常,可能是协议不对或者返回的内容有问题
- System.out.println("Pleasecheckyourprovidedhttpaddress!");
- e.printStackTrace();
- }catch(IOExceptione){
- //发生网络异常
- e.printStackTrace();
- }finally{
- //释放连接
- getMethod.releaseConnection();
- }
- }
- }
- //POST方法
- importjava.io.IOException;
- importorg.apache.commons.httpclient.*;
- importorg.apache.commons.httpclient.methods.PostMethod;
- importorg.apache.commons.httpclient.params.HttpMethodParams;
- publicclassPostSample{
- publicstaticvoidmain(String[]args){
- //构造HttpClient的实例
- HttpClienthttpClient=newHttpClient();
- //创建POST方法的实例
- Stringurl="http://www.oracle.com/";
- PostMethodpostMethod=newPostMethod(url);
- //填入各个表单域的值
- NameValuePair[]data={newNameValuePair("id","youUserName"),
- newNameValuePair("passwd","yourPwd")};
- //将表单的值放入postMethod中
- postMethod.setRequestBody(data);
- //执行postMethod
- intstatusCode=httpClient.executeMethod(postMethod);
- //HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
- //301或者302
- if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||
- statusCode==HttpStatus.SC_MOVED_TEMPORARILY){
- //从头中取出转向的地址
- HeaderlocationHeader=postMethod.getResponseHeader("location");
- Stringlocation=null;
- if(locationHeader!=null){
- location=locationHeader.getValue();
- System.out.println("Thepagewasredirectedto:"+location);
- }else{
- System.err.println("Locationfieldvalueisnull.");
- }
- return;
- }
- }
- }
12、IO
对java.io的扩展 操作文件非常方便。
- //1.读取Stream
- //标准代码:
- InputStreamin=newURL("http://jakarta.apache.org").openStream();
- try{
- InputStreamReaderinR=newInputStreamReader(in);
- BufferedReaderbuf=newBufferedReader(inR);
- Stringline;
- while((line=buf.readLine())!=null){
- System.out.println(line);
- }
- }finally{
- in.close();
- }
- //使用IOUtils
- InputStreamin=newURL("http://jakarta.apache.org").openStream();
- try{
- System.out.println(IOUtils.toString(in));
- }finally{
- IOUtils.closeQuietly(in);
- }
- //2.读取文件
- Filefile=newFile("/commons/io/project.properties");
- Listlines=FileUtils.readLines(file,"UTF-8");
- //3.察看剩余空间
- longfreeSpace=FileSystemUtils.freeSpace("C:/");
13、Lang
主要是一些公共的工具集合,比如对字符、数组的操作等等。
- //1合并两个数组:org.apache.commons.lang.ArrayUtils
- //有时我们需要将两个数组合并为一个数组,用ArrayUtils就非常方便,示例如下:
- privatestaticvoidtestArr(){
- String[]s1=newString[]{"1","2","3"};
- String[]s2=newString[]{"a","b","c"};
- String[]s=(String[])ArrayUtils.addAll(s1,s2);
- for(inti=0;i<s.length;i++){
- System.out.println(s[i]);
- }
- Stringstr=ArrayUtils.toString(s);
- str=str.substring(1,str.length()-1);
- System.out.println(str+">>"+str.length());
- }
- //2截取从from开始字符串
- StringUtils.substringAfter("SELECT*FROMPERSON","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"));
- //DeletesallwhitespacesfromaString删除所有空格
- System.out.println(StringUtils.deleteWhitespace("abc"));
- //判断是否包含这个字符
- System.out.println(StringUtils.contains("abc","ba"));
- //表示左边两个字符
- System.out.println(StringUtils.left("abc",2));
- System.out.println(NumberUtils.stringToInt("33"));
14、Logging
提供的是一个Java 的日志接口,同时兼顾轻量级和不依赖于具体的日志实现工具。
- importorg.apache.commons.logging.Log;
- importorg.apache.commons.logging.LogFactory;
- publicclassCommonLogTest{
- privatestaticLoglog=LogFactory.getLog(CommonLogTest.class);
- //日志打印
- publicstaticvoidmain(String[]args){
- log.error("ERROR");
- log.debug("DEBUG");
- log.warn("WARN");
- log.info("INFO");
- log.trace("TRACE");
- System.out.println(log.getClass());
- }
- }
15、Validator
通用验证系统,该组件提供了客户端和服务器端的数据验证框架。
验证日期
- //获取日期验证
- DateValidatorvalidator=DateValidator.getInstance();
- //验证/转换日期
- DatefooDate=validator.validate(fooString,"dd/MM/yyyy");
- if(fooDate==null){
- //错误不是日期
- return;
- }
表达式验证
- //设置参数
- booleancaseSensitive=false;
- Stringregex1="^([A-Z]*)(?:\-)([A-Z]*)*$"
- Stringregex2="^([A-Z]*)$";
- String[]regexs=newString[]{regex1,regex1};
- //创建验证
- RegexValidatorvalidator=newRegexValidator(regexs,caseSensitive);
- //验证返回boolean
- booleanvalid=validator.isValid("abc-def");
- //验证返回字符串
- Stringresult=validator.validate("abc-def");
- //验证返回数组
- String[]groups=validator.match("abc-def");
配置文件中使用验证
- <form-validation>
- <global>
- <validatorname="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>
- <validatorname="required"
- classname="org.apache.commons.validator.TestValidator"
- method="validateRequired"
- methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
- </global>
- <formset>
- <formname="nameForm">
- <fieldproperty="firstName"depends="required">
- <arg0key="nameForm.firstname.displayname"/>
- </field>
- <fieldproperty="lastName"depends="required">
- <arg0key="nameForm.lastname.displayname"/>
- </field>
- </form>
- </formset>
- </form-validation>
验证类
- Excerptsfromorg.apache.commons.validator.RequiredNameTest
- //加载验证配置文件
- InputStreamin=this.getClass().getResourceAsStream("validator-name-required.xml");
- ValidatorResourcesresources=newValidatorResources(in);
- //这个是自己创建的bean我这里省略了
- Namename=newName();
- Validatorvalidator=newValidator(resources,"nameForm");
- //设置参数
- validator.setParameter(Validator.BEAN_PARAM,name);
- Mapresults=null;
- //验证
- results=validator.validate();
- if(results.get("firstName")==null){
- //验证成功
- }else{
- //有错误interrors=((Integer)results.get("firstName")).intValue();
- }