2015-05-29 583 views
0

我已经使用这个代码,代码的SFTP的Java上传Java的SFTP指定绝对路径

package com.as400samplecode; 

import java.io.File; 
import java.io.FileInputStream; 
import java.util.Properties; 

import org.apache.commons.vfs2.FileObject; 
import org.apache.commons.vfs2.FileSystemOptions; 
import org.apache.commons.vfs2.Selectors; 
import org.apache.commons.vfs2.impl.StandardFileSystemManager; 
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder; 

public class SendMyFiles { 

static Properties props; 

public static void main(String[] args) { 

    SendMyFiles sendMyFiles = new SendMyFiles(); 
    if (args.length < 1) 
    { 
    System.err.println("Usage: java " + sendMyFiles.getClass().getName()+ 
    " Properties_file File_To_FTP "); 
    System.exit(1); 
    } 

    String propertiesFile = args[0].trim(); 
    String fileToFTP = args[1].trim(); 
    sendMyFiles.startFTP(propertiesFile, fileToFTP); 

} 

public boolean startFTP(String propertiesFilename, String fileToFTP){ 

    props = new Properties(); 
    StandardFileSystemManager manager = new StandardFileSystemManager(); 

    try { 

    props.load(new FileInputStream("properties/" + propertiesFilename)); 
    String serverAddress = props.getProperty("serverAddress").trim(); 
    String userId = props.getProperty("userId").trim(); 
    String password = props.getProperty("password").trim(); 
    String remoteDirectory = props.getProperty("remoteDirectory").trim(); 
    String localDirectory = props.getProperty("localDirectory").trim(); 

    //check if the file exists 
    String filepath = localDirectory + fileToFTP; 
    File file = new File(filepath); 
    if (!file.exists()) 
    throw new RuntimeException("Error. Local file not found"); 

    //Initializes the file manager 
    manager.init(); 

    //Setup our SFTP configuration 
    FileSystemOptions opts = new FileSystemOptions(); 
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
    opts, "no"); 
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); 
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); 

    //Create the SFTP URI using the host name, userid, password, remote path and file name 
    String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + 
    remoteDirectory + fileToFTP; 

    // Create local file object 
    FileObject localFile = manager.resolveFile(file.getAbsolutePath()); 

    // Create remote file object 
    FileObject remoteFile = manager.resolveFile(sftpUri, opts); 

    // Copy local file to sftp server 
    remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); 
    System.out.println("File upload successful"); 

    } 
    catch (Exception ex) { 
    ex.printStackTrace(); 
    return false; 
    } 
    finally { 
    manager.close(); 
    } 

    return true; 
} 


} 

来源:http://www.mysamplecode.com/2013/06/sftp-apache-commons-file-download.html

,我需要这样的ftp不登录到

指定路径

/根/ CHOSENPATH

当连接到SERV

呃但是

/CHOSENPATH

使用这部分代码时

//Create the SFTP URI using the host name, userid, password, remote path and file name 
    String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + 
    remoteDirectory + fileToFTP; 

有没有办法如何指定相对的绝对路径,而不是吗?

预先感谢您。

回答

2

您有这行代码:

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); 

我会尝试该标志设置为false。

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); 
+0

我会给它一个镜头,并通知您有关结果。 –