Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training Big Data and Hadoop Training Java Training  
 

Java SFTP

Ads

 
 

Share on Google+Share on Google+

prasanna.b93
Java SFTP
0 Answer(s)      7 years ago
Posted in : Java Beginners
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.


Ads
View Answers
Ads









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
Version of com.krux>hyperion-sftp-activity_2.10 dependency
List of Version of com.krux>hyperion-sftp-activity_2.10 dependency
Version of com.krux>hyperion-sftp-activity_2.11 dependency
List of Version of com.krux>hyperion-sftp-activity_2.11 dependency
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.1
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.1 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.11
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.11 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.12
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.12 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.3
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.3 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.4
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.4 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.6
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.6 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.7
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.7 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.0.9
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.0.9 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.1.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.1.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.10.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.10.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.11.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.11.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.11.2
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.11.2 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.11.3
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.11.3 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.12.1
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.12.1 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.12.2
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.12.2 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.13.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.13.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.13.1
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.13.1 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.14.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.14.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.14.1
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.14.1 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.15.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.15.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.16.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.16.0 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.16.2
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.16.2 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.16.3
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.16.3 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.16.5
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.16.5 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.16.6
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.16.6 in your project
Maven Dependency hyperion-sftp-activity_2.10 >> 2.2.0
You should include the dependency code given in this page to add Maven Dependency of com.krux >> hyperion-sftp-activity_2.10 version2.2.0 in your project