apache commons 使用說明_第1頁
apache commons 使用說明_第2頁
apache commons 使用說明_第3頁
apache commons 使用說明_第4頁
apache commons 使用說明_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、(轉(zhuǎn))Apache Commons工具集簡(jiǎn)介Apache Commons包含了很多開源的工具,用于解決平時(shí)編程經(jīng)常會(huì)遇到的問題,減少重復(fù)勞動(dòng)。我選了一些比較常用的項(xiàng)目做簡(jiǎn)單介紹。文中用了很多網(wǎng)上現(xiàn)成的東西,我只是做了一個(gè)匯總整理。一、Commons BeanUtils/commons/beanutils/index.html說明:針對(duì)Bean的一個(gè)工具集。由于Bean往往是有一堆get和set組成,所以BeanUtils也是在此基礎(chǔ)上進(jìn)行一些包裝。使用示例:功能有很多,網(wǎng)站上有詳細(xì)介紹。一個(gè)比較常用的功能是Bean Copy,也就是copy be

2、an的屬性。如果做分層架構(gòu)開發(fā)的話就會(huì)用到,比如從PO(Persistent Object)拷貝數(shù)據(jù)到VO(Value Object)。傳統(tǒng)方法如下:/得到TeacherFormTeacherForm teacherForm=(TeacherForm)form;/構(gòu)造Teacher對(duì)象Teacher teacher=new Teacher();/賦值teacher.setName(teacherForm.getName();teacher.setAge(teacherForm.getAge();teacher.setGender(teacherForm.getGender();teacher.

3、setMajor(teacherForm.getMajor();teacher.setDepartment(teacherForm.getDepartment();/持久化Teacher對(duì)象到數(shù)據(jù)庫(kù)HibernateDAO= ;HibernateDAO.save(teacher);使用BeanUtils后,代碼就大大改觀了,如下所示:/得到TeacherFormTeacherForm teacherForm=(TeacherForm)form;/構(gòu)造Teacher對(duì)象Teacher teacher=new Teacher();/賦值BeanUtils.copyProperties(teache

4、r,teacherForm);/持久化Teacher對(duì)象到數(shù)據(jù)庫(kù)HibernateDAO= ;HibernateDAO.save(teacher);二、Commons CLI/commons/cli/index.html說明:這是一個(gè)處理命令的工具。比如main方法輸入的string需要解析。你可以預(yù)先定義好參數(shù)的規(guī)則,然后就可以調(diào)用CLI來解析。使用示例:/ create Options objectOptions options = new Options();/ add t option, option is the command par

5、ameter, 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();CommandLine cmd = parser.parse( options, args);if(cmd.hasOption("t&quo

6、t;)    / print the date and timeelse    / print the date/ get c option valueString countryCode = cmd.getOptionValue("c");if(countryCode = null)     / print default dateelse     / print date for country specified by countryCode三、Co

7、mmons Codec/commons/codec/index.html說明:這個(gè)工具是用來編碼和解碼的,包括Base64,URL,Soundx等等。用這個(gè)工具的人應(yīng)該很清楚這些,我就不多介紹了。四、Commons Collections/commons/collections/說明:你可以把這個(gè)工具看成是java.util的擴(kuò)展。使用示例:舉一個(gè)簡(jiǎn)單的例子OrderedMap map = new LinkedMap();map.put("FIVE", "5"

8、);map.put("SIX", "6");map.put("SEVEN", "7");map.firstKey(); / returns "FIVE"map.nextKey("FIVE"); / returns "SIX"map.nextKey("SIX"); / returns "SEVEN"五、Commons Configuration/commons/confi

9、guration/說明:這個(gè)工具是用來幫助處理配置文件的,支持很多種存儲(chǔ)方式1. Properties files2. XML documents3. Property list files (.plist)4. JNDI5. JDBC Datasource6. System properties7. Applet parameters8. Servlet parameters使用示例:舉一個(gè)Properties的簡(jiǎn)單例子# perties, definining the GUI,colors.background = #FFFFFFcolors.foreground =

10、 #000080window.width = 500window.height = 300 PropertiesConfiguration config = new PropertiesConfiguration("perties");config.setProperty("colors.background", "#000000);config.save();config.save("perties);/save a copyInteger integer = co

11、nfig.getInteger("window.width");Commons DBCP/commons/dbcp/說明:Database Connection pool, Tomcat就是用的這個(gè),不用我多說了吧,要用的自己去網(wǎng)站上看說明。六、Commons DbUtils/commons/dbutils/說明:我以前在寫數(shù)據(jù)庫(kù)程序的時(shí)候,往往把數(shù)據(jù)庫(kù)操作單獨(dú)做一個(gè)包。DbUtils就是這樣一個(gè)工具,以后開發(fā)不用再重復(fù)這樣的工作了。值得一體的是,這個(gè)工具并不是現(xiàn)在流行的OR-M

12、apping工具(比如Hibernate),只是簡(jiǎn)化數(shù)據(jù)庫(kù)操作,比如QueryRunner run = new QueryRunner(dataSource);/ Execute the query and get the results back from the handlerObject result = (Object) run.query("SELECT * FROM Person WHERE name=?", "John Doe");七、Commons FileUpload/commons/fi

13、leupload/說明:jsp的上傳文件功能怎么做呢?使用示例:/ Create a factory for disk-based file itemsFileItemFactory factory = new DiskFileItemFactory();/ Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory); / Parse the requestList /* FileItem */ items = upload.parseRequest(request

14、);/ Process the uploaded itemsIterator iter = items.iterator();while (iter.hasNext()      FileItem item = (FileItem) iter.next();     if (item.isFormField()         processFormField(item);    

15、60; else         processUploadedFile(item);     八、Commons HttpClient/commons/httpclient/說明:這個(gè)工具可以方便通過編程的方式去訪問網(wǎng)站。使用示例:最簡(jiǎn)單的Get操作GetMethod get = new GetMethod("");/ execute method

16、and handle any error responses.InputStream in = get.getResponseBodyAsStream();/ Process the data from the input stream.get.releaseConnection();九、Commons IO/commons/io/說明:可以看成是java.io的擴(kuò)展,我覺得用起來非常方便。使用示例:1讀取Stream標(biāo)準(zhǔn)代碼:InputStream in = new URL( "&quo

17、t; ).openStream();try        InputStreamReader inR = new InputStreamReader( in );       BufferedReader buf = new BufferedReader( inR );       String line;       whil

18、e ( ( line = buf.readLine() ) != null )           System.out.println( line );          finally     in.close();  使用IOUtilsInputStream in = new URL( "http:/jakarta.apache.o

19、rg" ).openStream();try     System.out.println( IOUtils.toString( in ) ); finally     IOUtils.closeQuietly(in);2讀取文件File file = new File("/commons/io/perties");List lines = FileUtils.readLines(file, "UTF-8");3察看剩余空間long freeSpace

20、= FileSystemUtils.freeSpace("C:/");十、Commons JXPath/commons/jxpath/說明:Xpath你知道吧,那么JXpath就是基于Java對(duì)象的Xpath,也就是用Xpath對(duì)Java對(duì)象進(jìn)行查詢。這個(gè)東西還是很有想像力的。使用示例:Address address = (Address)JXPathContext.newContext(vendor).getValue("locationsaddress/zipCode='90210'/addres

21、s");上述代碼等同于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

22、(zipCode.equals("90210")       address = location.getAddress();        break;    十一、Commons Lang/commons/lang/說明:這個(gè)工具包可以看成是對(duì)java.lang的擴(kuò)展。提供了諸如StringUtils, StringEscapeUtil

23、s, RandomStringUtils, Tokenizer, WordUtils等工具類。十二、Commons Logging/commons/logging/說明:你知道Log4j嗎?十三、Commons Math/commons/math/說明:看名字你就應(yīng)該知道這個(gè)包是用來干嘛的了吧。這個(gè)包提供的功能有些和Commons Lang重復(fù)了,但是這個(gè)包更專注于做數(shù)學(xué)工具,功能更強(qiáng)大。十四、Commons Net/commons/net/說明:這個(gè)

24、包還是很實(shí)用的,封裝了很多網(wǎng)絡(luò)協(xié)議。1. FTP2. NNTP3. SMTP4. POP35. Telnet6. TFTP7. Finger8. Whois9. rexec/rcmd/rlogin10. Time (rdate) and Daytime11. Echo12. Discard13. NTP/SNTP使用示例:TelnetClient telnet = new TelnetClient();telnet.connect( "9", 23 );InputStream in = telnet.getInputStream();PrintStre

25、am out = new PrintStream( telnet.getOutputStream() );.telnet.close();十五、Commons Validator/commons/validator/說明:用來幫助進(jìn)行驗(yàn)證的工具。比如驗(yàn)證Email字符串,日期字符串等是否合法。使用示例:/ Get the Date validatorDateValidator validator = DateValidator.getInstance();/ Validate/Convert the dateDate fooDate = vali

26、dator.validate(fooString, "dd/MM/yyyy");if (fooDate = null)     / error.not a valid date    return;十六、Commons Virtual File System/commons/vfs/說明:提供對(duì)各種資源的訪問接口。支持的資源類型包括1. CIFS2. FTP3. Local Files4. HTTP and HTTPS5. SFTP6. Temporary Files7. WebDAV8. Z

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論