我有一个有很多NIO方法的Java应用程序,比如Files.copy,Files.move,Files.delete,FileChannel.
我现在想要实现的是:我想访问远程WebDAV服务器,并使用上传、删除或更新远程WebDAV数据等基本功能修改该服务器上的数据--而不更改我的应用程序上的所有方法。所以我的想法来了:
我认为WebDAV FileSystem实现可以做到这一点。添加自定义的WebDAV FileSystemProvider,用于管理远程数据上提到的文件操作。我已经搜索了很多,而且带有Sardine实现的Apache看起来不错,但是Apache似乎与NIO不兼容吗?
下面是一些示例代码,正如我想象的那样:
public class WebDAVManagerTest {
private static DefaultFileSystemManager fsManager;
private static WebdavFileObject testFile1;
private static WebdavFileObject testFile2;
private static FileSystem webDAVFileSystem1;
private static FileSystem webDAVFileSystem2;
@Before
public static void initWebDAVFileSystem(String webDAVServerURL) throws FileSystemException, org.apache.commons.vfs2.FileSystemException {
try {
fsManager = new DefaultFileSystemManager();
fsManager.addProvider("webdav", new WebdavFileProvider());
fsManager.addProvider("file", new DefaultLocalFileProvider());
fsManager.init();
} catch (org.apache.commons.vfs2.FileSystemException e) {
throw new FileSystemException("Exception initializing DefaultFileSystemManager: " + e.getMessage());
}
String exampleRemoteFile1 = "/foo/bar1.txt";
String exampleRemoteFile2 = "/foo/bar2.txt";
testFile1 = (WebdavFileObject) fsManager.resolveFile(webDAVServerURL + exampleRemoteFile1);
webDAVFileSystem1 = (FileSystem) fsManager.createFileSystem(testFile1);
Path localPath1 = webDAVFileSystem1.getPath(testFile1.toString());
testFile2 = (WebdavFileObject) fsManager.resolveFile(webDAVServerURL + exampleRemoteFile2);
webDAVFileSystem2 = (FileSystem) fsManager.createFileSystem(testFile2);
Path localPath2 = webDAVFileSystem1.getPath(testFile1.toString());
}
}之后,我想在我的应用程序中使用localPath1 + localPath2。这样,例如Files.copy(localPath1,newRemotePath)就可以将WebDAV服务器上的文件复制到一个新目录中。
这是正确的行动方针吗?或者还有其他库来实现这一目标?
发布于 2016-12-07 20:07:00
Apache使用它自己的FileSystem接口,而不是NIO接口。您有三个选择,不同的努力水平。
选项3已经完成,因此您可以自定义其他人已经编写的内容,查看nio-fs-提供者或nio webdav。我肯定还有其他的,但这两个很容易在谷歌上找到。
从零开始实现WebDav NIO FileSystem将是相当多的工作,所以我不建议从那里开始,我可能会接受别人已经做过的事情,并使它为我(即选项2 )工作。
https://stackoverflow.com/questions/40527347
复制相似问题