Please figure this out. The code runs properly without any exception.
FTPClient ftp = new FTPClient();
ftp.connect(server);
if(!ftp.l
please add the apache library for this code this are the impoted class
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
rest import class is from java.io or java.net
public boolean upload(String server,String username,String password,File localfile ){
boolean Store=false;
try{
FTPClient ftp = new FTPClient();
// ftp.connect(server);
/* you can use either code which is written above above or below code as ftp port 20 is used for the data transfer and port 21 is used for command and controlls */
ftp.connect(InetAddress.getByName(server),21);
//here 'server' is your domain name of ftp server or url
if(!ftp.login(username, password))
{
ftp.logout();
return false;
}
ftp.sendNoOp();//used so server timeout exception will not rise
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return false;
}
ftp.enterLocalPassiveMode(); /* just include this line here and your code will work fine */
InputStream in = new FileInputStream(localfile);
// ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE);
Store = ftp.storeFile(destinationfile, in);
in.close();
//ftp.disconnect();
//here logout will close the connection for you
ftp.logout();
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
return Store;
}