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
This is really interesting, You're a very skilled blogger. I've joined your feed and look forward to seeking more of your great post. Also, I've shared your website in my social networks! solar window film
ReplyDeleteGreat post! I didn’t knowral of these resources and I will dowload it now!
ReplyDeleteTaldeen is one of the best plastic manufacturing company in Saudi Arabia. They are manufacturing Handling Solutions Plastic products like Plastic Pallets and plastic crates. Here is the link of the product
ReplyDeleteHandling Solutions
Plastic Pallets
GrueBleen is one of the Branding and Marketing agency Based in Riyadh- Saudi Arabia. The main functions of GrueBleen is Advertising, Branding, Marketing, Office Branding, Exhibition Management and Digital Marketing. Visit the below link to know more about GrueBleen Creative Club.
Branding Agency Riyadh
Marketing Agency Riyadh
Agriculture Solutions – Taldeen is a plastic manufacturing company in Saudi Arabia. They are manufacturing agricultural plastic products like greenhouse cover and hay cover. Visit the below link to know more details
Agriculture Solutions
Greenhouse Cover
GrueBleen – One of the best social media marketing agency in Riyadh- Saudi Arabia. Visit here for the all service details of GrueBleen.
Social Media Marketing Agency | Social Media Agency In Saudi Arabia | Social Media Agency In Riyadh | Social Media Agency in Jeddah |
Here is the colleges details to study in Bangalore. You can select the best college details from the below mentioned courses that you love to study. The list of colleges are only in Bangalore, so, if you are looking to study in Bangalore, just click the below mentioned links.
ReplyDeleteBSc Medical Imaging Technology Colleges in Bangalore | Medical Imaging Technology Colleges in Bangalore | BSc Optometry Colleges in Bangalore | Optometry Colleges in Bangalore |BSc Renal Dialysis Colleges in Bangalore | Renal Dialysis Technology Colleges in Bangalore |BSc Respiratory Care Technology Colleges in Bangalore | Respiratory Care Colleges in Bangalore |BSc Cardiac Care Technology Colleges in Bangalore | Cardiac Care Colleges in Bangalore |BSc Perfusion Technology Colleges in Bangalore | Perfusion Technology Colleges in Bangalore |