Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


prasanna.b93
Java SFTP
0 Answer(s)      4 years and 7 months 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.



Advertisement
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
File Upload in J2ee on solaris machine using sftp - JSP-Servlet
the application in such a way that file upload can be done using SFTP on remote Solaris
Advertisements
java
java  diff bt core java and java
java
java  what is java
Java
Java   Whether Java is pure object oriented Language
JAVA
JAVA  how the name came for java language as "JAVA
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
java
java  is java open source
java
java  what is java reflection
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
java
java  sample code for RARP using java
java
java  RARP implementation using java socket
java
java   What is ?static? keyword
java
java  different between java & core java print("code sample
java
java code to search the nodes  how to write the java code to search the nodes using routers
java
java  create java program for delete and update the details,without using database, just a normal java program
java
java pattern code for a given words  java pattern code for a given words pattern
java
java  how can use sleep in java   which book learn of java language
java
java  different between java & core java print("code sample
java
java  how to edit text document by using java then how to edit starting and ending of text document by using java
java
java online telephone directory  i need coding for online telephone directory..by using java....pls help me
java
java  why methods in java raise exceptions   Have a look at the following link: Java Exceptions
java
java  dear, i want a field for date picker using java/java script
java
java   How to set java Policy for applet using jdk 6
java
java  hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
java
java  java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java programs
java
java  how to prepare the java   Hi Friend, If you want to learn how to install java, creating and running a java program then go through the following links: http://www.roseindia.net/java/beginners/index.shtml http
 

 

 

DMCA.com