com.sshtools.j2ssh.SftpClient Java Examples

The following examples show how to use com.sshtools.j2ssh.SftpClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Put file to remote host.
 *
 * @param host the hostname of the client
 * @param port the port number the user want to use for connection
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @param localfilePath the localfile path where the file is situated
 * @param remotePath the remote path where the file is going to be put
 * @param fileName the file name you want to put
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unchecked")
public static void putFileToRemoteHost(String host, int port,String username,String password, String localfilePath,String remotePath,String fileName)throws IOException {
	SftpClient sftp = getSftpClient(host,port, username, password);
	sftp.lcd(localfilePath);
	sftp.cd(remotePath);
	sftp.put(fileName);
	List<SftpFile> dirContents = sftp.ls(remotePath);
    Iterator<SftpFile> it = dirContents.iterator();
    while(it.hasNext()){    		  
    	if(it.next().getFilename().equals(fileName)){
    		 SSH_LOG.info("The file " + fileName + " was tranferred successfully to " + remotePath);
    		 return;
    	}
    }
    SSH_LOG.error("The file " + fileName + " was not tranferred  to " + remotePath);	
}
 
Example #2
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Gets the file from remote host.
 *
 * @param host the hostname of the client
 * @param port the port number the user want to use for connection
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @param remoteFilePath remotePath the remote path from where the file is going to be get
 * @param localfilePath the local file path where the file is going to be put
 * @param fileName the name of the file you want to transfer
 * @return the file from the remote host
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void getFileFromRemoteHost(String host, int port,String username,String password, String remoteFilePath,String localPath,String fileName) throws IOException {
	SftpClient sftp = getSftpClient(host, port,username, password);
	sftp.cd(remoteFilePath);
	sftp.lcd(localPath);
	sftp.get(fileName);
	File f = new File(localPath+System.getProperty("file.separator")+fileName);
	if (!f.exists()){
		SSH_LOG.error("The file " + fileName + " was not tranferred  to " + localPath);	
	}
	else{
		SSH_LOG.info("The file " + fileName + " was tranferred successfully to " + localPath);
	}		
}
 
Example #3
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 5 votes vote down vote up
public static void sftpUpload(FTPConfig config, File file, String remoteFileName) throws IOException
{
  SshClient ssh = new SshClient();
  SftpClient sftp = sshLogin(config, ssh);
  sftp.mkdirs(remoteFileName.substring(0, remoteFileName.lastIndexOf("/")));
  sftp.put(new FileInputStream(file), remoteFileName);
  sftp.quit();
  ssh.disconnect();
}
 
Example #4
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 5 votes vote down vote up
private static SftpClient sshLogin(FTPConfig config, SshClient ssh) throws IOException
{
  ssh.setSocketTimeout(60000);
  ssh.connect(config.host, new IgnoreHostKeyVerification());
  PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
  pwd.setUsername(config.userName);
  pwd.setPassword(config.password);
  ssh.authenticate(pwd);
  SftpClient sftp = ssh.openSftpClient();
  return sftp;
}
 
Example #5
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 5 votes vote down vote up
public static File sftpDownload(FTPConfig config, File file, String remoteFileName) throws IOException
{
  SshClient ssh = new SshClient();
  SftpClient sftp = sshLogin(config, ssh);
  sftp.get(remoteFileName, new FileOutputStream(file));
  sftp.quit();
  ssh.disconnect();
  return file;
}
 
Example #6
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Create an SFTP connection to a client
 * 
 * @param host the hostname of the Client.The default port used for connection is 22
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @return the client 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static SftpClient getSftpClient(String host, String username,String password) throws IOException {
	return getSftpClient(host, SSH_PORT, username, password);
}
 
Example #7
Source File: SSHUtils.java    From stevia with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Create an SFTP connection to a client
 *
 * @param host the hostname of the client
 * @param port the port the user want to use for connection
 * @param username the username required for authentication
 * @param password the password required for authentication
 * @return the client
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static SftpClient getSftpClient(String host,int port, String username,String password) throws IOException {
	SshClient ssh = connect(host, port,username, password);
	return ssh.openSftpClient();
}