Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
FTP the files using Apache FTP API
import java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import java.io.InputStream;
import java.util.*;
import
org.apache.commons.net.ftp.FTPClient;
import
org.apache.commons.net.ftp.FTPReply;
class
FtpFilesToServer {
/**
*
Method to ftp the files to the destination server, and move the
file to
* an archive directory
*/
protected void ftpFiles() throws
ICCException
{
String ftpURL = "";
String userName =
"";
String pwd
="";
String
destDir ="";
List<File> fileList = new ArrayList(2);
File yourFile1 =
new File(yourFile1);
File yourFile2
= new File(yourFile2);
fileList.add(yourFile1);
fileList.add(yourFile2);
ftpFilesToServer(ftpURL, userName, pwd,
destDir, fileList);
}
/**
* Method to ftp files to
server
* @param
String ftpURL
*
@param String userName
* @param String pwd
* @param String
destDir - directory on remote machine, or null if root
* @param
List<File> filesToFTP - List of files to be
ftp'ed
*/
public static void
ftpFilesToServer(String ftpURL, String userName, String pwd,
String destDir, List<File>
filesToFTP)
{
InputStream input = null;
try
{
FTPClient ftp = new FTPClient();
ftp.connect(ftpURL);
// verify login was successful
if (!ftp.login(userName, pwd))
{
ftp.logout();
}
//verify that destination directory exists
if(destDir != null && destDir.trim().length() !=
0)
{
if(!FTPReply.isPositiveCompletion(ftp.cwd(destDir)))
{
}
}
//transfer the files
if(filesToFTP != null)
{
ListIterator li=filesToFTP.listIterator();
while(li.hasNext())
{
File fileToFTP=(File)li.next();
String fileName=fileToFTP.getName();
input = new FileInputStream(fileToFTP);
//ftp.storeFile(remote, local)
if (!ftp.storeFile(fileName, input))
{
ftp.logout();
throw new IOException("File failed to transfer to ftp URL
[" + ftpURL + "].");
}
}
}
ftp.logout();
}
catch(IOException ioex)
{
}finally {
if (input != null) {
try {
input.close();
} catch (IOException
e) {
}
}
}
}
}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.