2015-05-29 291 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

我會給它一個鏡頭,並通知您有關結果。 –