2017-06-26 54 views
0

我想使用apache-commons-vfs API將文件從Windows上載到Linux。我可以使用這個工具上傳文件,但是當程序運行時,它會要求輸入證書,甚至可以在代碼中輸入它已經存在的證書。如果我們也在證書中輸入空白,則允許上傳。如何在使用commons-VFS的SFTP期間跳過密碼提示

是否可以跳過憑證提示?

如果SSH私有/公共是唯一的解決方案,那麼請分享步驟來做到這一點。

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 SSHUtility { 

    static Properties props; 

    public static void main(String[] args) { 

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

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

    } 

    public boolean startFTP(String propertiesFilename, String fileToFTP) { 

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

     try { 

      // props.load(new FileInputStream("properties/" + propertiesFilename)); 
      props.load(new FileInputStream(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()); 

      System.out.println("localFile::::" + 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; 
    } 

} 

回答

0

將setPreferredAuthentications設置爲「publickey,keyboard-interactive,password」可以解決此問題。

// Setup our SFTP configuration 
FileSystemOptions opts = new FileSystemOptions(); 
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); 
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); 
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); 
SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password"); 
相關問題