您的位置:首页 > 运维架构 > Apache

Apache Commons工具集简介(二)

2009-11-25 11:08 337 查看
5、Commons FileUpload
http://jakarta.apache.org/commons/fileupload/
说明:jsp的上传文件功能怎么做呢?
使用示例:
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}

6、Commons HttpClient
http://jakarta.apache.org/commons/httpclient/
说明:这个工具可以方便通过编程的方式去访问网站。
使用示例:最简单的Get操作
GetMethod get = new GetMethod("http://jakarta.apache.org");
// execute method and handle any error responses.
...
InputStream in = get.getResponseBodyAsStream();
// Process the data from the input stream.
get.releaseConnection();

Commons IO http://jakarta.apache.org/commons/io/ 说明:可以看成是java.io的扩展,我觉得用起来非常方便。
使用示例:
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:/");

7、Commons JXPath
http://jakarta.apache.org/commons/jxpath/
说明:Xpath你知道吧,那么JXpath就是基于Java对象的Xpath,也就是用Xpath对Java对象进行查询。这个东西还是很有想像力的。
使用示例:
Address address = (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;
}
}

8、Commons Lang http://jakarta.apache.org/commons/lang/ 说明:这个工具包可以看成是对java.lang的扩展。提供了诸如StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils等工具类。

9、Commons Logging
http://jakarta.apache.org/commons/logging/
说明:你知道Log4j吗?

10、Commons Math
http://jakarta.apache.org/commons/math/
说明:看名字你就应该知道这个包是用来干嘛的了吧。这个包提供的功能有些和Commons Lang重复了,但是这个包更专注于做数学工具,功能更强大。

11、Commons Net
http://jakarta.apache.org/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();

12、Commons Validator http://jakarta.apache.org/commons/validator/ 说明:用来帮助进行验证的工具。比如验证Email字符串,日期字符串等是否合法。
使用示例:
// Get the Date validator
DateValidator validator = DateValidator.getInstance();
// Validate/Convert the date
Date fooDate = validator.validate(fooString, "dd/MM/yyyy");
if (fooDate == null) {
// error...not a valid date
return;
}

13、Commons Virtual File System
http://jakarta.apache.org/commons/vfs/
说明:提供对各种资源的访问接口。支持的资源类型包括
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
这个包的功能很强大,极大的简化了程序对资源的访问。
使用示例:
从jar中读取文件
// Locate the Jar file
FileSystemManager fsManager = VFS.getManager();
FileObject jarFile = fsManager.resolveFile( "jar:lib/aJarFile.jar" );

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: