Please download the jar file from the Apache commons Website given below and add to your project Java build path.
http://commons.apache.org/net/download_net.cgi
package com.as400samplecode; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class SendMyFiles { static Properties props; public static void main(String[] args) { SendMyFiles sendMyFiles = new SendMyFiles(); if (args.length < 1) { System.err.println("Usage: java " + sendMyFiles.getClass().getName()+ " Properties_file File_To_FTP "); System.exit(1); } String propertiesFile = args[0].trim(); String fileToFTP = args[1].trim(); sendMyFiles.startFTP(propertiesFile, fileToFTP); } public boolean startFTP(String propertiesFilename, String fileToFTP){ props = new Properties(); try { props.load(new FileInputStream("properties/" + propertiesFilename)); String serverAddress = props.getProperty("serverAddress").trim(); String userId = props.getProperty("userId").trim(); String password = props.getProperty("password").trim(); String remoteDirectory = props.getProperty("remoteDirectory").trim(); String localDirectory = props.getProperty("localDirectory").trim(); //new ftp client FTPClient ftp = new FTPClient(); //try to connect ftp.connect(serverAddress); //login to server if(!ftp.login(userId, password)) { ftp.logout(); return false; } int reply = ftp.getReplyCode(); //FTPReply stores a set of constants for FTP reply codes. if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return false; } //enter passive mode ftp.enterLocalPassiveMode(); //get system name System.out.println("Remote system is " + ftp.getSystemType()); //change current directory ftp.changeWorkingDirectory(remoteDirectory); System.out.println("Current directory is " + ftp.printWorkingDirectory()); //get input stream InputStream input; input = new FileInputStream(localDirectory + "/" + fileToFTP); //store the file in the remote server ftp.storeFile(fileToFTP, input); //close the stream input.close(); ftp.logout(); ftp.disconnect(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } }
Source for the properties file ftp.properties
#Properties File FTP to server serverAddress=ftp.someDomain.com userId=yourUserId password=yourPassword remoteDirectory=/remote/MyFolder localDirectory=/local/MyFolder
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.