Apache VFS(7): 文件管理器解析文件的方法

本文介绍了Apache VFS中StandardFileSystemManager如何通过解析URI获取文件对象的过程。解析过程涉及多种URI格式,包括FTP、HTTP和本地文件路径等。解析的核心方法为resolveFile,该方法根据提供的URI确定文件系统并返回相应的FileObject。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本系列文章导航

Apache VFS(1): 基本介绍

Apache VFS(2): 文件的监听和监控

Apache VFS(3): 文件过滤器和选择器

Apache VFS(4): 事件

Apache VFS(5): 使用它!

Apache VFS(6): 几个重要的概念性接口

Apache VFS(7): 文件管理器解析文件的方法

一般来说,我们使用Apache VFS时,直接从VFS对象获得的文件管理器是StandardFileSystemManager ,StandardFileSystemManager从DefaultFileSystemManager继承而来。而解析文件在DefaultFileSystemManager 中完成。
绝大部分时候,你会提供一个URI来定位你的文件系统,例如:ftp://yourftp/rootdir或者http://yourweb/rootdi r或者file://c:/rootdir
,然后你将这个字符串作为参数传给StandardFileSystemManger, 这时候DefaultFileSystemManger的resolveFile方法 负责处理URI的解析,并且最终返回一个FileObject文件对象。我们看一下这个方法:
这个方法有5种重载方式,分别接受不同的参数,但核心方法只有一个:

/**
* Resolves a URI, realtive to a base file with specified FileSystem
* configuration
*/
public FileObject resolveFile(final FileObject baseFile, final String uri,
final FileSystemOptions fileSystemOptions)
throws FileSystemException
{
final FileObject realBaseFile;
if (baseFile != null && VFS.isUriStyle()
&& baseFile.getName().getType() == FileType.FILE)
{
realBaseFile = baseFile.getParent();
}
else
{
realBaseFile = baseFile;
}
// TODO: use resolveName and use this name to resolve the fileObject

UriParser.checkUriEncoding(uri);

if (uri == null)
{
throw new IllegalArgumentException();
}

// Extract the scheme
final String scheme = UriParser.extractScheme(uri);
if (scheme != null)
{
// An absolute URI - locate the provider
final FileProvider provider = (FileProvider) providers.get(scheme);
if (provider != null)
{
return provider.findFile(realBaseFile, uri, fileSystemOptions);
}
// Otherwise, assume a local file
}

// Handle absolute file names
if (localFileProvider != null
&& localFileProvider.isAbsoluteLocalName(uri))
{
return localFileProvider.findLocalFile(uri);
}

if (scheme != null)
{
// An unknown scheme - hand it to the default provider
if (defaultProvider == null)
{
throw new FileSystemException("vfs.impl/unknown-scheme.error",
new Object[]
{ scheme, uri });
}
return defaultProvider.findFile(realBaseFile, uri,
fileSystemOptions);
}

// Assume a relative name - use the supplied base file
if (realBaseFile == null)
{
throw new FileSystemException("vfs.impl/find-rel-file.error", uri);
}

return realBaseFile.resolveFile(uri);
}

文章来源:http://alartin.javaeye.com/blog/92436

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值