2016-11-10 42 views
0

我有很多的NIO方法,如Files.copyFiles.moveFiles.deleteFileChannel的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服務器上的文件複製到新目錄。

這是正確的行爲嗎?還是有其他圖書館來實現這一目標?

回答

1

Apache VFS使用它自己的FileSystem接口而不是NIO接口。你有三種選擇,付出不同的努力。

  1. 更改您的代碼以使用現有的使用它自己的FileSystem(即Apache VFS)的webdav項目。
  2. 查找使用webdav並實現NIO文件系統等的現有項目。
  3. 自己實現NIO FileSystem接口。

選項3已經這樣做你可以自定義什麼別人已經寫的,看看nio-fs-providernio-fs-webdav。我確信還有其他人,但這兩個很容易找到使用谷歌。

從頭開始實施一個WebDAV NIO文件系統將是一個相當大量的工作,所以我不建議你從那裏,我想有可能採取什麼樣的人做了和使這項工作對我來說即選項2