2017-09-05 139 views
1

同時正與VFS2庫,我注意到在FTP服務器上,我不得不啓用VFS.setUriStyle(真),所以該庫將改變工作目錄的父目錄我正在運行的目標文件(cwd directoryName)。阿帕奇VFS2 uriStyle - 根絕對路徑用雙斜線結束

但如果啓用UriStyle,一切都被relativly解決的根源。如果根目錄不是「//」,那麼這不會成爲問題。

GenericFileName類將根的絕對路徑設置爲「/」,這使得方法getPath()返回「/」+ getUriTrailer(),在根的情況下總是返回「//」。即relativly決心//任何事情都有兩個點出發到他們的路徑。

這意味着,如果我執行下面的代碼:

public class RemoteFileTest { 
public static void main(String[] args) { 
    // Options for a RemoteFileObject connection 
    VFS.setUriStyle(true); 
    FileSystemOptions options = new FileSystemOptions(); 
    // we doing an ftp connection, hence we use the ftpConfigBuilder 
    // we want to work in passive mode 
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(options, true); 
    FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false); 
    // DefaultFileSystemConfigBuilder.getInstance().setRootURI(options, "/newRoot/"); 
    // System.out.println(DefaultFileSystemConfigBuilder.getInstance().getRootURI(options)); 
    // ftp://localhost:21/ 

    StaticUserAuthenticator auth = new StaticUserAuthenticator("", "user", "pass"); 
    try { 
     DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, auth); 
    } catch (FileSystemException e) { 
     e.printStackTrace(); 
     return; 
    } 

    // A FileSystemManager creates an abstract FileObject linked to are desired RemoteFile. 
    // That link is just simulated and not yet real. 
    FileSystemManager manager; 
    try { 
     manager = VFS.getManager(); 
    } catch (FileSystemException e) { 
     e.printStackTrace(); 
     return; 
    } 

    try (FileObject remoteFile = manager.resolveFile("ftp://localhost:21/sub_folder/test.txt", options)) { 

     System.out.println("Is Folder " + remoteFile.isFolder()); 
     System.out.println("Is File " + remoteFile.isFile()); 

    } catch (FileSystemException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return; 
    } 

}} 

我收到與FTP服務器這種互動:

USER user 
PASS **** 
TYPE I 
CWD // 
SYST 
PASV 
LIST ..sub_folder/ 
PWD 
CWD ..sub_folder/ 

我想要的互動成爲像這一點,但無兩點目錄的infront。

親切的問候 巴里

回答

0

固定它,如下所述:再次

殘疾人uriStyle。 寫了創建我的自定義編寫的經理我自己的VFS類。 該經理覆蓋FtpFileProvider與我自定義的,它只是將根設置選擇一個自定義的,這會導致所需的行爲。

import org.apache.commons.vfs2.FileName; 
import org.apache.commons.vfs2.FileObject; 
import org.apache.commons.vfs2.FileSystem; 
import org.apache.commons.vfs2.FileSystemException; 
import org.apache.commons.vfs2.FileSystemOptions; 
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder; 
import org.apache.commons.vfs2.provider.ftp.FtpFileProvider; 

public class AdvancedFtpFileProvider extends FtpFileProvider { 
    public AdvancedFtpFileProvider() { 
     super(); 
     // setFileNameParser(AdvancedFtpFileNameParser.getInstance()); 
    } 

    @Override 
    protected FileObject findFile(FileName name, FileSystemOptions fileSystemOptions) throws FileSystemException { 
     // Check in the cache for the file system 
     //getContext().getFileSystemManager().resolveName... resolves the configured RootUri relative to the selected root (name.getRoot()). This calls cwd to the selectedRoot and operates from there with relatives urls towards the new root! 
     final FileName rootName = getContext().getFileSystemManager().resolveName(name.getRoot(), DefaultFileSystemConfigBuilder.getInstance().getRootURI(fileSystemOptions)); 

     final FileSystem fs = getFileSystem(rootName, fileSystemOptions); 

     // Locate the file 
     // return fs.resolveFile(name.getPath()); 
     return fs.resolveFile(name); 
    } 
}