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 BeginnersJava 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
Advertisements
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