Java SFTP

Java SFTP

Hi I want to perform SFTP through Jsch package. I could able to sftp one file but I could not able to sftp multiple files.

PFB my code

package xml_handson;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author 604734868
*/
import com.jcraft.jsch.*;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import java.io.File;
import java.util.ArrayList;
import xml_handson.BulkExecution;

/**
* This class is used to demonstrate the usage of the
* JCraft JSch package to SFTP files.
*

*/
public class SftpClient_Mars {
Files f =new Files();
Session session = null;
Channel channel = null;
public ChannelSftp c;
String path;
File[] files_in_folder;
String dest_new_xml="/cyclone/common/vesta/cais_common/";
String dest_new_xml_mmd="/cyclone/common/vesta/cais_common/out";
ArrayList<String> foldernames = new ArrayList();
ArrayList<String> allfilenames = new ArrayList();
String[] arrr;


/** Creates a new instance of TestCommonsNet */
public SftpClient_Mars() {
}

public static void main(String[] args){
SftpClient_Mars ref =new SftpClient_Mars();
try {
ref.sftp_mars();
} catch (SftpException ex) {
java.util.logging.Logger.getLogger(SftpClient_Mars.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
java.util.logging.Logger.getLogger(SftpClient_Mars.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
java.util.logging.Logger.getLogger(SftpClient_Mars.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* main - Unit test program
* @param args Command line arguments
*
*/
public void sftp_mars() throws SftpException, FileNotFoundException, Exception {


String ftpHost = "10.81.33.15";
//String
int ftpPort = 22;
String ftpUserName = "b2bftp";
String ftpPassword = "ftpus3r";
String ftpRemoteDirectory

//
//First Create a JSch session
//
System.out.println("Creating session.");
JSch jsch = new JSch();
session = null;
channel = null;
// ChannelSftp c = null;

//
//Now connect and SFTP to the SFTP Server
//
try {
//Create a session sending through our username and password
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
System.out.println("Session created.");
session.setPassword(ftpPassword);
//Security.addProvider(new com.sun.crypto.provider.SunJCE());

//
//Setup Strict HostKeyChecking to no so we dont get the
//unknown host key exception
//
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Session connected.");

//
//Open the SFTP channel
//
System.out.println("Opening Channel.");
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp)channel;
} catch (Exception e) {
System.err.println("Unable to connect to FTP server. "+e.toString());
throw e;
}

//
//Change to the remote directory

// System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory);
c.cd(ftpRemoteDirectory);
String s = c.pwd().toString();
System.out.println(s);

/*Picking all the files from one location*/

File folder = new File("D:\\Test_final");
File[] listOfFiles = folder.listFiles();


for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {

} else if (listOfFiles[i].isDirectory()) {

foldernames.add(listOfFiles[i].getAbsolutePath());


}

}
System.out.println( foldernames.size());
for(int list=0;list<=foldernames.size()-1;list++){

File f = new File(foldernames.get(list).toString());
System.out.println(foldernames.get(list).toString());
// System.out.println("The number of folder is"+ f.listFiles());
files_in_folder=f.listFiles();
for(int all_files=0; all_files<=files_in_folder.length-1;all_files++ ){
allfilenames.add(files_in_folder[all_files].getAbsolutePath());
}


// c.quit();
}

/* After collecting all the files in a array list,uploading the files to the sever*/

//Here I could able to upload only the last two files in the array list

for(int k = 0; k<= allfilenames.size()-1;k++){
// System.out.println(allfilenames.get(k).toString());
String filename = allfilenames.get(k).toString();
if(!(filename.endsWith("_mmd.xml"))){
try {
System.out.println("The file to upload is "+ filename +" "+dest_new_xml);
c.put(filename, dest_new_xml);
Thread.sleep(5000);
// c.put(filename, dest_new_xml_mmd);
} catch (SftpException ex) {
System.out.println("Exception in xml");
java.util.logging.Logger.getLogger(SftpClient_Mars.class.getName()).log(Level.SEVERE, null, ex);
}

}
else{
try {
System.out.println("The file to upload is "+ filename + " "+dest_new_xml_mmd);
c.put(filename, dest_new_xml_mmd);
Thread.sleep(5000);
} catch (SftpException ex) {
java.util.logging.Logger.getLogger(SftpClient_Mars.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception in mmd");
}
}
}


}
}
}



In the above program.Only the last two file in the array list is getting posted to server.

I could not able to do a bulk transfer of all the file.

PLease help me in this regard.


View Answers









Related Tutorials/Questions & Answers:
Java SFTP - Java Beginners
Java SFTP  Hi I want to perform SFTP through Jsch package. I could able to sftp one file but I could not able to sftp multiple files. PFB my... to SFTP files. * */ public class SftpClient_Mars { Files f =new Files
how to copy files from remote to local machine using SFTP SSL in java?
how to copy files from remote to local machine using SFTP SSL in java?  I want to copy files from remote server to local machine using SFTP and SSL in java. Plz help
Advertisements
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.1.1 is released. Learn to use hyperion-sftp-activity_2.10 version 3.1.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.1.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.4.0 is released. Learn to use hyperion-sftp-activity_2.10 version 3.4.0 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.4.0 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.2.4 is released. Learn to use hyperion-sftp-activity_2.10 version 3.2.4 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.2.4 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.8.1 is released. Learn to use hyperion-sftp-activity_2.10 version 2.8.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.8.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.14.1 is released. Learn to use hyperion-sftp-activity_2.10 version 2.14.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.14.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.7.3 is released. Learn to use hyperion-sftp-activity_2.11 version 2.7.3 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.7.3 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 3.2.7 is released. Learn to use hyperion-sftp-activity_2.11 version 3.2.7 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.2.7 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 6.0.0 is released. Learn to use hyperion-sftp-activity_2.11 version 6.0.0 in Maven based Java projects
Maven dependency for  com.krux  - Version 6.0.0 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2020, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.16.3 is released. Learn to use hyperion-sftp-activity_2.11 version 2.16.3 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.16.3 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 5.1.7 is released. Learn to use hyperion-sftp-activity_2.11 version 5.1.7 in Maven based Java projects
Maven dependency for  com.krux  - Version 5.1.7 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2018, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 4.11.0 is released. Learn to use hyperion-sftp-activity_2.11 version 4.11.0 in Maven based Java projects
Maven dependency for  com.krux  - Version 4.11.0 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2017, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.11.1 is released. Learn to use hyperion-sftp-activity_2.10 version 2.11.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.11.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.2.8 is released. Learn to use hyperion-sftp-activity_2.10 version 3.2.8 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.2.8 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.2.7 is released. Learn to use hyperion-sftp-activity_2.10 version 3.2.7 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.2.7 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 3.4.2 is released. Learn to use hyperion-sftp-activity_2.11 version 3.4.2 in Maven based Java projects
Maven dependency for  com.krux  - Version 3.4.2 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.8.1 is released. Learn to use hyperion-sftp-activity_2.11 version 2.8.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.8.1 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.0.0 is released. Learn to use hyperion-sftp-activity_2.10 version 2.0.0 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.0.0 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.10.0 is released. Learn to use hyperion-sftp-activity_2.10 version 2.10.0 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.10.0 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.0.3 is released. Learn to use hyperion-sftp-activity_2.10 version 2.0.3 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.0.3 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.0.6 is released. Learn to use hyperion-sftp-activity_2.11 version 2.0.6 in Maven based Java projects
Maven dependency for  com.krux  - Version 2.0.6 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2015, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 5.7.2 is released. Learn to use hyperion-sftp-activity_2.11 version 5.7.2 in Maven based Java projects
Maven dependency for  com.krux  - Version 5.7.2 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp... 2019, the released version of  com.krux - hyperion-sftp-activity_2.11
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 4.5.1 is released. Learn to use hyperion-sftp-activity_2.10 version 4.5.1 in Maven based Java projects
Maven dependency for  com.krux  - Version 4.5.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp... 2016, the released version of  com.krux - hyperion-sftp-activity_2.10
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.0.11 is released. Learn to use hyperion-sftp-activity_2.11 version 2.0.11 in Maven based Java projects
; com.krux - hyperion-sftp-activity_2.11 version 2.0.11 java library in your...Maven dependency for  com.krux  - Version 2.0.11 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.6.1 is released. Learn to use hyperion-sftp-activity_2.10 version 2.6.1 in Maven based Java projects
- hyperion-sftp-activity_2.10 version 2.6.1 java library in your project...Maven dependency for  com.krux  - Version 2.6.1 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 4.9.1 is released. Learn to use hyperion-sftp-activity_2.11 version 4.9.1 in Maven based Java projects
- hyperion-sftp-activity_2.11 version 4.9.1 java library in your project...Maven dependency for  com.krux  - Version 4.9.1 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 2.13.2 is released. Learn to use hyperion-sftp-activity_2.10 version 2.13.2 in Maven based Java projects
; com.krux - hyperion-sftp-activity_2.10 version 2.13.2 java library in your...Maven dependency for  com.krux  - Version 2.13.2 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.0.4 is released. Learn to use hyperion-sftp-activity_2.10 version 3.0.4 in Maven based Java projects
- hyperion-sftp-activity_2.10 version 3.0.4 java library in your project...Maven dependency for  com.krux  - Version 3.0.4 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.10 version 3.3.3 is released. Learn to use hyperion-sftp-activity_2.10 version 3.3.3 in Maven based Java projects
- hyperion-sftp-activity_2.10 version 3.3.3 java library in your project...Maven dependency for  com.krux  - Version 3.3.3 of hyperion-sftp-activity_2.10 released The developers of   com.krux - hyperion-sftp
Maven dependency for com.krux - hyperion-sftp-activity_2.11 version 2.0.10 is released. Learn to use hyperion-sftp-activity_2.11 version 2.0.10 in Maven based Java projects
; com.krux - hyperion-sftp-activity_2.11 version 2.0.10 java library in your...Maven dependency for  com.krux  - Version 2.0.10 of hyperion-sftp-activity_2.11 released The developers of   com.krux - hyperion-sftp
ModuleNotFoundError: No module named 'cheesefactory-sftp'
ModuleNotFoundError: No module named 'cheesefactory-sftp'  Hi, My... named 'cheesefactory-sftp' How to remove the ModuleNotFoundError: No module named 'cheesefactory-sftp' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-sftp'
ModuleNotFoundError: No module named 'django-sftp'  Hi, My Python... 'django-sftp' How to remove the ModuleNotFoundError: No module named 'django-sftp' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'ligament-sftp'
ModuleNotFoundError: No module named 'ligament-sftp'  Hi, My... 'ligament-sftp' How to remove the ModuleNotFoundError: No module named 'ligament-sftp' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'papermill-sftp'
ModuleNotFoundError: No module named 'papermill-sftp'  Hi, My... named 'papermill-sftp' How to remove the ModuleNotFoundError: No module named 'papermill-sftp' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sftp-client'
ModuleNotFoundError: No module named 'sftp-client'  Hi, My Python... 'sftp-client' How to remove the ModuleNotFoundError: No module named 'sftp-client' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'sftp-cloudfs'
ModuleNotFoundError: No module named 'sftp-cloudfs'  Hi, My Python... 'sftp-cloudfs' How to remove the ModuleNotFoundError: No module named 'sftp-cloudfs' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'tap-sftp'
ModuleNotFoundError: No module named 'tap-sftp'  Hi, My Python...-sftp' How to remove the ModuleNotFoundError: No module named 'tap-sftp... to install padas library. You can install tap-sftp python with following
ModuleNotFoundError: No module named 'tap-sftp'
ModuleNotFoundError: No module named 'tap-sftp'  Hi, My Python...-sftp' How to remove the ModuleNotFoundError: No module named 'tap-sftp... to install padas library. You can install tap-sftp python with following
ModuleNotFoundError: No module named 'zoink-sftp'
ModuleNotFoundError: No module named 'zoink-sftp'  Hi, My Python... 'zoink-sftp' How to remove the ModuleNotFoundError: No module named 'zoink-sftp' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'cheesefactory-sftp'
ModuleNotFoundError: No module named 'cheesefactory-sftp'  Hi, My... named 'cheesefactory-sftp' How to remove the ModuleNotFoundError: No module named 'cheesefactory-sftp' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'django-sftp'
ModuleNotFoundError: No module named 'django-sftp'  Hi, My Python... 'django-sftp' How to remove the ModuleNotFoundError: No module named 'django-sftp' error? Thanks   Hi, In your python environment
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 3.3.3
-sftp-activity_2.11 version 3.3.3. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:3.3.3 Java library in your project... for hyperion-sftp-activity_2.11 version 3.3.3 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 3.0.5
-sftp-activity_2.11 version 3.0.5. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:3.0.5 Java library in your project... for hyperion-sftp-activity_2.11 version 3.0.5 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 2.11.3
-sftp-activity_2.11 version 2.11.3. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:2.11.3 Java library in your project... for hyperion-sftp-activity_2.11 version 2.11.3 In this section have given
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 2.0.10
-sftp-activity_2.11 version 2.0.10. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:2.0.10 Java library in your project... for hyperion-sftp-activity_2.11 version 2.0.10 In this section have given
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 2.0.9
-sftp-activity_2.11 version 2.0.9. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:2.0.9 Java library in your project... for hyperion-sftp-activity_2.11 version 2.0.9 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.10 version 4.8.0
-sftp-activity_2.10 version 4.8.0. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.10:4.8.0 Java library in your project... for hyperion-sftp-activity_2.10 version 4.8.0 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.10 version 3.5.2
-sftp-activity_2.10 version 3.5.2. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.10:3.5.2 Java library in your project... for hyperion-sftp-activity_2.10 version 3.5.2 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for hyperion-sftp-activity_2.11 version 5.1.5
-sftp-activity_2.11 version 5.1.5. You can add these depency in your project to get com.krux:hyperion-sftp-activity_2.11:5.1.5 Java library in your project... for hyperion-sftp-activity_2.11 version 5.1.5 In this section have given the dependency

Ads