user451872
user451872

Reputation: 9

j2ssh remote to remote file transfer

The following code is working fine for localhost to remote server. But my application runs on a separate server so I need to pick files from remote1 server and then put it in remote2 server. How would I do this with J2SSH?

    package com.test.sftp;
    
    import java.io.File;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Properties;
    
    import com.sshtools.daemon.SshServer;
    import com.sshtools.j2ssh.SftpClient;
    import com.sshtools.j2ssh.SshClient;
    import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
    import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
    import com.sshtools.j2ssh.configuration.SshConnectionProperties;
    import com.sshtools.j2ssh.sftp.SftpFile;
    import com.sshtools.j2ssh.transport.ConsoleKnownHostsKeyVerification;
    import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
    import com.sshtools.j2ssh.transport.InvalidHostFileException;
    import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
    
    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    
    public class SftpTest {
    
        /**
         * @param args
         */
     public String userName;
     
     public String uri;
     public Date date;
     public String sdate;
     public DateFormat formatter ;
     public String serviceStart;
     
        public String hostname=null;
        public String username=null;
        public String password=null;
        
        public String hostname1=null;
        public String username1=null;
        public String password1=null;
        
        public String remotepath=null;
        public String localpath=null;
        public String remoteFilename=null;
        public void con()
        {
        SshClient ssh=null;
        SftpClient sftp=null;
        SftpFile sstp = null;
        try
        {
       
      
     
        hostname="192.168.1.1";
        username="xxxxx";
        password="xxxxxxx";
        
       
        
        SshClient client = new SshClient();
        
        
        SshConnectionProperties connectionProperties = new SshConnectionProperties();
        
       connectionProperties.setTransportProvider(SshConnectionProperties.USE_STANDARD_SOCKET); // or USE_STANDARD_SOCKET or USE_SOCKS4_PROXY or USE_SOCKS5_PROXY
      connectionProperties.setProxyHost("proxy.example.com");
       
         connectionProperties.setProxyPort(22);
         connectionProperties.setHost(hostname);
         connectionProperties.setPort(22);
    
       client.connect(connectionProperties, new IgnoreHostKeyVerification());
    
        PasswordAuthenticationClient authenticationClient = new PasswordAuthenticationClient();
        authenticationClient.setUsername(username);
        authenticationClient.setPassword(password);
        int result = client.authenticate(authenticationClient);
        System.out.println("result value ::"+result);
        if (result == AuthenticationProtocolState.COMPLETE)
        System.out.println("success Authentication");
        else
        System.out.println("failed Authentication");
        System.out.println(client.isConnected());
        
        
        SftpClient sftpClient = client.openSftpClient();
        String localpath = "C:/Documents and Settings/user1/Desktop/images";
        sftpClient.lcd(localpath);
        //sftpClient.cd("/");
        
    
        
     
       
        File folder = new File("C:/Documents and Settings/user/Desktop/images");
        File[] listOfFiles = folder.listFiles();
    
        
        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
              String str ="C:/Documents and Settings/user1/Desktop/images/"+listOfFiles[i].getName();
           
           //  sftpClient.put(str,"/usr/project/images/");
              System.out.println("File " + listOfFiles[i].getName());
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }
     
        
        
    
     
        
        
       
        sftpClient.quit();
        client.disconnect();
        }
        catch(Exception e)
        {
        System.out.println("Exception while connecting to the remote server" + e);
        }
        }
        public static void main(String args[]) throws Exception
        {
        SftpTest obj = new SftpTest();
         obj.con();// calling the function
                   
        }

    }

Please help me understand how I can achieve this. Thank you.

Upvotes: 0

Views: 4866

Answers (1)

Not sure that I understood your question ...

If you only have SFTP access to both servers, and your code runs locally (on the client), then your only option is to download the file and upload it to another server. If you have SSH access to server1, then you can create a script which will upload/download file to/from server 2.

And if your code is running on server1 and you need to upload the file from server1 to server2, then how is it different (other than the local path is different) from your current situation when you upload the code from client1 to server2?

Upvotes: 0

Related Questions