There are requirements when we need to upload/copy files to SFTP server and perform several tasks(e.g. run some commands or scripts, comping files..etc) after uploading the file.
As this is Secure FTP server we cannot use simple multipart request to upload file in the server.
Manual approach to copy files and run scripts/commands on the file in remote SFTP server is time consuming as we need to write all the script commands manually.
To overcome manual efforts JAVa provides API called JSCH (Java Secure Channel).
This API is used for the copying (uploading) any file and executing tasks such as commands/scripts to Remote Server which is enabled with a Secure FTP.
import com.jcraft.jsch.ChannelSftp;
To get the Sesssion for remote SFTP server,
jsch.getSession(username, host, 22);
/* username= sever User name which is used to connect the server */
/* host = IP_Address or Servername*/
session.setPassword(pass);
/*pass = password to connect the remote server*/
Once we get the Session for the Remote Server, the next step is to
open the SFTP channel by,
session.connect();
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
once we get the SFTP channel we can perform tasks which were perfromed manually
c.rm("fileName");
c.put("source","destination");
To execute commands on the SFTP server,the exec channel needs to be opened by
Channel channel=session.openChannel("exec");
and then we can run single/multiple commnands by below statement.
((ChannelExec) channel).setCommand(cmd1+"\n"+cmd2+"\n"+cmd3);
where cmd1, cmd2 and cmd3 can be any commands like
String cmd1="cd /Test/Scripts/";
String cmd2 ="chmod 777 test.sh";
String cmd3 = "./test.sh "+Command Argument;
once the files are copied/uploaded successfully, at the end,sftp
session and channel needs to be disconnected.
As this is Secure FTP server we cannot use simple multipart request to upload file in the server.
Manual approach to copy files and run scripts/commands on the file in remote SFTP server is time consuming as we need to write all the script commands manually.
To overcome manual efforts JAVa provides API called JSCH (Java Secure Channel).
This API is used for the copying (uploading) any file and executing tasks such as commands/scripts to Remote Server which is enabled with a Secure FTP.
import com.jcraft.jsch.ChannelSftp;
To get the Sesssion for remote SFTP server,
jsch.getSession(username, host, 22);
/* username= sever User name which is used to connect the server */
/* host = IP_Address or Servername*/
session.setPassword(pass);
/*pass = password to connect the remote server*/
Once we get the Session for the Remote Server, the next step is to
open the SFTP channel by,
session.connect();
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
once we get the SFTP channel we can perform tasks which were perfromed manually
c.rm("fileName");
c.put("source","destination");
To execute commands on the SFTP server,the exec channel needs to be opened by
Channel channel=session.openChannel("exec");
and then we can run single/multiple commnands by below statement.
((ChannelExec) channel).setCommand(cmd1+"\n"+cmd2+"\n"+cmd3);
where cmd1, cmd2 and cmd3 can be any commands like
String cmd1="cd /Test/Scripts/";
String cmd2 ="chmod 777 test.sh";
String cmd3 = "./test.sh "+Command Argument;
once the files are copied/uploaded successfully, at the end,sftp
session and channel needs to be disconnected.