Apache VFS:基本介绍

http://alartin.iteye.com/blog/92053

Apache VFS提供了一种虚拟文件系统,能够让你通过程序很方便的和位于本地文件系统,FTP文件系统,HTTP文件打交道。
在Apache VFS中核心是 FileObject接口。它代表一个文件,和Java File不同,它具有更多延伸的功能和信息。实现FileObject接口的文件对象有很多:例如AbstractFileObjet抽象类提供了部分FileObject实现。大部分FileObject实现类都继承AbstractFileObject类:
  1. LocalFile:(很奇怪为什么不是LocalFileObject)
  2. FtpFileObject
  3. HttpFileObject
  4. SftpFileObject
  5. ZipFileObject
  6. TarFileObject
  7. RamFileObject
  8. UrlFileObject
  9. DelegateFileObject
  10. CompressedFileObject
FileObject代表一个文件,能够用来访问文件内容和结构。文件是以层次结构组织的,每个层次组织形成一个文件系统。一个文件系统就像本地操作系统中的文件系统一样,例如Windows文件系统,或者一个Http服务器,或者一个Zip文件包。

文件有两种类型: 目录普通文件。普通文件拥有数据或者说内容,而目录不含有内容,只能包含其他文件,普通文件不能包含其他文件。

文件名
FileObject拥有一个FileName对象,将文件名字作为一个独立的对象处理。文件名是不可变的。文件名对象有很多方法:
  1. getBaseName
  2. getDepth
  3. getExtension
  4. getFriendlyURI
  5. getParent
  6. getPath
  7. getRoot
  8. 等等

文件内容的读取
注意:和Java File不同的是FileObject拥有一个 FileContent对象,如果想访问文件内容的话,需要使用FileObject.getFileContent()方法,这个方法返回一个FileContent对象。FileContent代表一个文件的内容。
简单的说读取文件内容使用:FileContent. getInputStream()
写内容使用:FileContent. getOutputStream()
FileContent内部有一个 final AbstractFileObject的引用。

FileContent的重要方法有:
  1. close 关闭文件内容使用的所有资源,包括所有打开的文件流。当心!!
  2. get/set Attribute/Attributes: 不能用目前,除非自己重写,其实调用AbstractFileObject的doGet/SetAttribute方法
  3. getContentInfo 返回一个ContentInfo对象, ContentInfo对象记录了内容的类型和编码信息
  4. getInputStream 读文件内容
  5. getOutputStream 写文件内容
  6. isOpen 检查看看该文件有没有打开的文件流
  7. getFile 返回FileObject对象
  8. get/setLastModifiedTime

文件的操作
基本的文件操作例如创建,删除,重命名,拷贝都可以通过FileObject来完成。
  1. copyFrom(FileObject src,FileSelector selector) 从其他地方的源文件拷贝到这个文件中,包括源文件的子文件,有一个文件选择器参数和源文件参数
  2. delete() 删除此文件
  3. delete(FileSelector selector) 删除符合文件选择器的所有该文件的子文件
  4. createFile() 如果这个文件不存在,创建它
  5. createFolder() 如果这个目录不存在,创建它
VFS为了支持高级的操作例如和版本控制工具配合,提供了FileOperation对象,不过VFS根本没有任何实现,如果你想玩,自己玩去吧。
在这里批判一下VFS: VFS的开发有些地方非常不合理,例如FileObject号称支持对文件设置属性,后来发现其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协议的扩展,把web变成一个可读写的媒体,作为当今基本的只读媒体的替代。原理就是目录和文件时可以共享的?都是可读写的对象?通过web。RFCs2518和3253描述了WebDAV/DeltaV 对于HTTP的扩展,存放于(随之有许多其它有用的信息)http://www.webdav.org/。

    已经有一些操作系统文件浏览器可以使用WebDAV装配网络目录,在Win32中,Windows浏览器可以像普通共享文件夹一样浏览叫做网络文件夹(只是一个设置好WebDAV的网络位置)的目录,在Mac OS X也有这个能力,就像Nautilus和Konqueror作的(分别对应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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"VFS: Close: file count is" 是来自 Linux 内核的一条错误消息。它表示在关闭文件描述符时,文件计数器的值不正确。 在 Linux 中,每个进程都有一个文件描述符表,其中保存着打开的文件的信息。每个打开的文件都对应一个文件描述符,在进行文件操作时使用。当一个文件描述符不再需要时,应该将其关闭,以释放相关资源并维护文件计数器的正确状态。 这个错误消息可能意味着在关闭文件描述符时发生了错误或异常情况。可能的原因包括: 1. 文件描述符已经被关闭或无效,但尝试再次关闭它。 2. 文件描述符计数器的值被损坏或不正确地修改。 3. 在多线程环境中,存在并发问题导致文件描述符计数器的错误状态。 要解决这个问题,可以考虑以下步骤: 1. 检查代码逻辑,确保不会重复关闭已经关闭的文件描述符。 2. 确保在多线程环境中正确处理并发访问文件描述符的情况,例如使用互斥锁或其他同步机制来保护文件描述符表的访问。 3. 检查是否有其他代码修改了文件描述符计数器的值,导致不正确的状态。在这种情况下,需要找到并修复这些问题。 4. 如果问题持续存在或无法解决,可以考虑检查操作系统和文件系统的相关文档、查看内核日志或寻求更高级的支持来进一步调试和解决问题。 需要注意的是,这只是一个错误消息的分析,具体解决方法可能因具体情况而异。如果有更多上下文或错误消息的详细信息,可能会有助于更准确地确定问题原因和解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值