我有很多的NIO方法,如Files.copy
,Files.move
,Files.delete
,FileChannel
的Java應用程序...的WebDAV FileSystemProvider - 的Java NIO
我現在想要實現:我想訪問遠程WebDAV服務器並修改該服務器上的數據具有上傳,刪除或更新遠程WebDAV數據等基本功能 - 無需更改我的應用程序上的每種方法。所以這裏來我的想法:
我認爲一個WebDAV文件系統的實現會做的伎倆。添加一個自定義的WebDAV FileSystemProvider,用於管理遠程數據上提到的文件操作。我搜索了很多,Apache SFS的沙盒實現看起來不錯 - 但看起來Apache VFS與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服務器上的文件複製到新目錄。
這是正確的行爲嗎?還是有其他圖書館來實現這一目標?