http://alartin.iteye.com/blog/92053
在Apache VFS中核心是 FileObject接口。它代表一个文件,和Java File不同,它具有更多延伸的功能和信息。实现FileObject接口的文件对象有很多:例如AbstractFileObjet抽象类提供了部分FileObject实现。大部分FileObject实现类都继承AbstractFileObject类:
- LocalFile:(很奇怪为什么不是LocalFileObject)
- FtpFileObject
- HttpFileObject
- SftpFileObject
- ZipFileObject
- TarFileObject
- RamFileObject
- UrlFileObject
- DelegateFileObject
- CompressedFileObject
文件有两种类型: 目录和 普通文件。普通文件拥有数据或者说内容,而目录不含有内容,只能包含其他文件,普通文件不能包含其他文件。
文件名
FileObject拥有一个FileName对象,将文件名字作为一个独立的对象处理。文件名是不可变的。文件名对象有很多方法:
- getBaseName
- getDepth
- getExtension
- getFriendlyURI
- getParent
- getPath
- getRoot
- 等等
文件内容的读取
注意:和Java File不同的是FileObject拥有一个 FileContent对象,如果想访问文件内容的话,需要使用FileObject.getFileContent()方法,这个方法返回一个FileContent对象。FileContent代表一个文件的内容。
简单的说读取文件内容使用:FileContent. getInputStream()
写内容使用:FileContent. getOutputStream()
FileContent内部有一个 final AbstractFileObject的引用。
FileContent的重要方法有:
- close 关闭文件内容使用的所有资源,包括所有打开的文件流。当心!!
- get/set Attribute/Attributes: 不能用目前,除非自己重写,其实调用AbstractFileObject的doGet/SetAttribute方法
- getContentInfo 返回一个ContentInfo对象, ContentInfo对象记录了内容的类型和编码信息
- getInputStream 读文件内容
- getOutputStream 写文件内容
- isOpen 检查看看该文件有没有打开的文件流
- getFile 返回FileObject对象
- get/setLastModifiedTime
文件的操作
基本的文件操作例如创建,删除,重命名,拷贝都可以通过FileObject来完成。
- copyFrom(FileObject src,FileSelector selector) 从其他地方的源文件拷贝到这个文件中,包括源文件的子文件,有一个文件选择器参数和源文件参数
- delete() 删除此文件
- delete(FileSelector selector) 删除符合文件选择器的所有该文件的子文件
- createFile() 如果这个文件不存在,创建它
- createFolder() 如果这个目录不存在,创建它
在这里批判一下VFS: VFS的开发有些地方非常不合理,例如FileObject号称支持对文件设置属性,后来发现其AbstractFileObject的getAttributes()方法只返回一个空Map,而setAttribute()更离谱,干脆直接抛出不支持的异常。这个功能非常简单啊,如果你需要的话,自己改一下吧。因此,使用VFS时,千万注意这些陷阱,谁让这是开源软件那 ON YOUR OWN RISK!
=============
Commons VFS currently supports the following file systems:
· FTP
· SFTP
· Zip, Jar and Tar (uncompressed, tgz or tbz2)
· res
· ram
· mime
The following file systems are in development:
· WebDAV
We can't release WebDAV as we depend on an snapshot, thus it is in our sandbox.
· CIFS
· mime
All filenames are treated as URIs. One of the consequences of this is you have to encode the '%' character using %25.
Depending on the filesystem additional characters are encoded if needet. This is done automatically, but might be reflected in the filename.
Examples
· file:///somedir/some%25file.txt
Provides access to the files on the local physical file system.
URI Format
[file://] absolute-path
Where absolute-pathis a valid absolute file name for the local platform. UNC names are supported under Windows.
Examples
· file:///home/someuser/somedir
· file:///C:/Documents and Settings
· file:/somehost/someshare/afile.txt
· /home/someuser/somedir
· c:\program files\some dir
· c:/program files/some dir
Provides read-only access to the contents of Zip, Jar and Tar files.
URI Format
zip:// arch-file-uri [! absolute-path ]
jar:// arch-file-uri [! absolute-path ]
tar:// arch-file-uri [! absolute-path ]
tgz:// arch-file-uri [! absolute-path ]
tbz2:// arch-file-uri [! absolute-path ]
Where arch-file-uri refers to a file of any supported type, including other zip files. Note: if you would like to use the ! as normal character it must be escaped using %21.
tgz and tbz2 are convenience for tar:gz and tar:bz2.
Examples
· jar:../lib/classes.jar!/META-INF/manifest.mf
· zip:http://somehost/downloads/somefile.zip
· jar:zip:outer.zip!/nested.jar!/somedir
· jar:zip:outer.zip!/nested.jar!/some%21dir
· tar:gz:http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt
· tgz:file://anyhost/dir/mytar.tgz!/somepath/somefile
Provides read-only access to the contents of gzip and bzip2 files.
URI Format
gz:// compressed-file-uri
bz2:// compressed-file-uri
Where compressed-file-uri refers to a file of any supported type. There is no need to add a ! part to the uri if you read the content of the file you always will get the uncompressed version.
Examples
· gz:/my/gz/file.gz
Provides access to files on an HTTP server.
URI Format
http://[ username [: password ]@] hostname [: port ][ absolute-path ]
https://[ username [: password ]@] hostname [: port ][ absolute-path ]
Examples
· http://somehost:8080/downloads/somefile.jar
· http://myusername@somehost/index.html
Provides access to files on a WebDAV server.
URI Format
webdav://[ username [: password ]@] hostname [: port ][ absolute-path ]
Examples
· webdav://somehost:8080/dist
|
- WebDAV(“Web-based Distributed Authoring and Versioning” 的缩写)是一个标准HTTP协议的扩展,把web变成一个可读写的媒体,作为当今基本的只读媒体的替代。原理就是目录和文件时可以共享的?都是可读写的对象?通过web。RFCs2518和3253描述了WebDAV/DeltaV 对于HTTP的扩展,存放于(随之有许多其它有用的信息)http://www.webdav.org/。
已经有一些操作系统文件浏览器可以使用WebDAV装配网络目录,在Win32中,Windows浏览器可以像普通共享文件夹一样浏览叫做网络文件夹(只是一个设置好WebDAV的网络位置)的目录,在Mac OS X也有这个能力,就像Nautilus和Konqueror作的(分别对应GNOME和KDE)。
Provides access to the files on an FTP server.
URI Format
ftp://[ username [: password ]@] hostname [: port ][ absolute-path ]
Examples
· ftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
Provides access to the files on an SFTP server (that is, an SSH or SCP server).
URI Format
sftp://[ username [: password ]@] hostname [: port ][ absolute-path ]
Examples
· sftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz
Provides access to the files on a CIFS server, such as a Samba server, or a Windows share.
URI Format
smb://[ username [: password ]@] hostname [: port ][ absolute-path ]
Examples
· smb://somehost/home
Provides access to a temporary file system, or scratchpad, that is deleted when Commons VFS shuts down. The temporary file system is backed by a local file system.
URI Format
tmp://[ absolute-path ]
Examples
· tmp://dir/somefile.txt
This is not really a filesystem, it just tries to lookup a resource using javas ClassLoader.getResource() and creates a VFS url for further processing.
URI Format
res://[ path ]
Examples
· res:path/in/classpath/image.png
might result in jar:file://my/path/to/images.jar!/path/in/classpath/image.png
A filesystem which stores all the data in memory. You can configure the max size and a predicate (FileSelector). The predicate will be used to check if it is allowed to add a given file.
URI Format
ram://[ path ]
Examples
· ram:///any/path/to/file.txt
This filesystem can read mails and its attachements like archives.
If a part in the parsed mail has no name, a dummy name will be generated. The dummy name is: _body_part_X where X will be replaced by the part number.
URI Format
mime:// mime-file-uri [! absolute-path ]
Examples
· mime:file:///your/path/mail/anymail.mime!/
· mime:file:///your/path/mail/anymail.mime!/filename.pdf
· mime:file:///your/path/mail/anymail.mime!/_body_part_0