2016-11-10 44 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