package fileuploads;
import com.jcraft.jsch.*;
import java.io.*;
import java.util.*;
public class TestJSch {
public TestJSch() {
}
public static void main(String[] args) {
try {
String ftpHost = "ipaddress";
int ftpPort = port;
String ftpUserName = "username";
String ftpPassword = "password";
String ftpRemoteDirectory = "//dropzone/inbound/moneymarket/mmconfirmations/dev";
String fileToTransmit = "C:/tmp/*.txt";
// First Create a JSch session
System.out.println("Creating session.");
JSch jsch = new JSch();
Session session = null;
Channel 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);
// 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);
// Send the file we generated
try {
File f = new File(fileToTransmit);
System.out.println("Storing file as remote filename: " + f.getName());
c.put(new FileInputStream(f), f.getName());
} catch (Exception e) {
System.err.println("Storing remote file failed." + e.toString());
throw e;
}
// Get the list of files in the remote server directory
Vector files = c.ls(ftpRemoteDirectory);
// Log if we have nothing to download
if (files.size() == 0) {
System.out.println("No files are available for download.");
}
}
catch (Exception e) {
System.err.println("Error: " + e.toString());
}
System.out.println("Process Complete.");
System.exit(0);
}
} |