Apache VFS:基本介绍
http://alartin.iteye.com/blog/92053
Apache VFS提供了一種虛擬文件系統(tǒng),能夠讓你通過程序很方便的和位于本地文件系統(tǒng),FTP文件系統(tǒng),HTTP文件打交道。在Apache VFS中核心是FileObject接口。它代表一個文件,和Java File不同,它具有更多延伸的功能和信息。實現(xiàn)FileObject接口的文件對象有很多:例如AbstractFileObjet抽象類提供了部分FileObject實現(xiàn)。大部分FileObject實現(xiàn)類都繼承AbstractFileObject類:
文件有兩種類型: 目錄和 普通文件。普通文件擁有數(shù)據(jù)或者說內(nèi)容,而目錄不含有內(nèi)容,只能包含其他文件,普通文件不能包含其他文件。
文件名
FileObject擁有一個FileName對象,將文件名字作為一個獨立的對象處理。文件名是不可變的。文件名對象有很多方法:
文件內(nèi)容的讀取
注意:和Java File不同的是FileObject擁有一個 FileContent對象,如果想訪問文件內(nèi)容的話,需要使用FileObject.getFileContent()方法,這個方法返回一個FileContent對象。FileContent代表一個文件的內(nèi)容。
簡單的說讀取文件內(nèi)容使用:FileContent. getInputStream()
寫內(nèi)容使用:FileContent. getOutputStream()
FileContent內(nèi)部有一個 final AbstractFileObject的引用。
FileContent的重要方法有:
文件的操作
基本的文件操作例如創(chuàng)建,刪除,重命名,拷貝都可以通過FileObject來完成。
在這里批判一下VFS: VFS的開發(fā)有些地方非常不合理,例如FileObject號稱支持對文件設(shè)置屬性,后來發(fā)現(xiàn)其AbstractFileObject的getAttributes()方法只返回一個空Map,而setAttribute()更離譜,干脆直接拋出不支持的異常。這個功能非常簡單啊,如果你需要的話,自己改一下吧。因此,使用VFS時,千萬注意這些陷阱,誰讓這是開源軟件那 ON YOUR OWN RISK!
?
=============
Commons VFS currently supports the following file systems:
·???????? FTP
·???????? Local Files
·???????? HTTP and HTTPS
·???????? SFTP
·???????? Temporary Files
·???????? Zip, Jar and Tar (uncompressed, tgz or tbz2)
·???????? gzip and bzip2
·???????? res
·???????? ram
·???????? mime
Things from the sandbox
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
Naming
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
Local Files
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
Zip, Jar and Tar
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
gzip and bzip2
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
HTTP and HTTPS
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
WebDAV
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協(xié)議的擴展,把web變成一個可讀寫的媒體,作為當今基本的只讀媒體的替代。原理就是目錄和文件時可以共享的?都是可讀寫的對象?通過web。RFCs2518和3253描述了WebDAV/DeltaV?對于HTTP的擴展,存放于(隨之有許多其它有用的信息)http://www.webdav.org/。
已經(jīng)有一些操作系統(tǒng)文件瀏覽器可以使用WebDAV裝配網(wǎng)絡(luò)目錄,在Win32中,Windows瀏覽器可以像普通共享文件夾一樣瀏覽叫做網(wǎng)絡(luò)文件夾(只是一個設(shè)置好WebDAV的網(wǎng)絡(luò)位置)的目錄,在Mac?OS?X也有這個能力,就像Nautilus和Konqueror作的(分別對應(yīng)GNOME和KDE)。?
FTP
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
SFTP
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
CIFS
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
Temporary Files
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
res
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
ram
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
mime
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
?
?
總結(jié)
以上是生活随笔為你收集整理的Apache VFS:基本介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: split | notes in jav
- 下一篇: winCVS 使用方法