In the example code below, the FTPClient object is created and its connect(String ftphostname) is invoked to connect to the remote FTP server. You need to supply the name of your FTP server to this method. The login(String username,String password) method will log in to the remote server. Normally, for file transferring between the client and server, you need to set the connection mode to passive mode by using the enterLocalPassiveMode() method. For the file transferring between a server and another server, you will use the enterRemoteActiveMode() instead. The default directory of the server to upload the file to is the root directory (/). You can specify a directory to upload the file to by using the changeWorkingDirectory(String dirpath).
Before transferring the files to the server, you should check whether you connect and login to the server successfully by using the isPositiveCompletion(int replyCode) method. If this method return true, you can upload the files to the server by using the storeFile(String remoteFileName,InputStream is) method. This method will copy data from the InputStream object and place it on the server with the name specified by the remoteFileName argument. After the upload task completes, you can log out and disconnect from the server by using the logout() and disconnect() method.
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class HttpHeaders {
public static void main(String[] args){
ftpUpload();
}
public static void ftpUpload(){
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
//connect to the remove ftp server
client.connect("ftp.worldbestlearningcenter.com");
//log in to the server with user name and password
client.login("yourusername", "yourpassword");
//set the passive mode for the file transfer
client.enterLocalPassiveMode();
//upload file to this directory
client.changeWorkingDirectory("/www");
System.out.println(client.getReplyString());
int reply=client.getReplyCode();
String filename = "d:/SQLiteJava.txt";
fis = new FileInputStream(filename);
if(FTPReply.isPositiveCompletion(reply)){ //connect and login successfully
//upload file to the server
client.storeFile("SQLiteJava.txt", fis);
}
else{
System.out.println("Can't upload to the FTP server.");
}
//log out from the server
client.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (client.isConnected()) {
//disconnect from the server
client.disconnect();
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
If you use Java 7 and Windows 7 or Vista, there is a common problem to transfer the file between the client and the remove FTP server. This problem causes the file transfer failed. You will get the socket exception as shown below.import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class HttpHeaders {
public static void main(String[] args){
ftpUpload();
}
public static void ftpUpload(){
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
//connect to the remove ftp server
client.connect("ftp.worldbestlearningcenter.com");
//log in to the server with user name and password
client.login("yourusername", "yourpassword");
//set the passive mode for the file transfer
client.enterLocalPassiveMode();
//upload file to this directory
client.changeWorkingDirectory("/www");
System.out.println(client.getReplyString());
int reply=client.getReplyCode();
String filename = "d:/SQLiteJava.txt";
fis = new FileInputStream(filename);
if(FTPReply.isPositiveCompletion(reply)){ //connect and login successfully
//upload file to the server
client.storeFile("SQLiteJava.txt", fis);
}
else{
System.out.println("Can't upload to the FTP server.");
}
//log out from the server
client.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (client.isConnected()) {
//disconnect from the server
client.disconnect();
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
To avoid this problem or error, you need to turn off the Windows Firewall (Control Panel-> Windows Firewall). Then run the the Java program again.
Thanks for your informative article on ios mobile application development. Your article helped me to explore the future of mobile apps developers. Having sound knowledge on mobile application development will help you to float in mobile application development. iOS Training in Chennai | iOS Training Institutes in Chennai| Android Training in Chennai
ReplyDeleteNice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.
ReplyDeleteAndroid Training in chennai |
Android course in chennai |
IOS Training in chennai
Great post! I didn’t knowral of these resources and I will dowload it now!
ReplyDelete