You are on page 1of 3

FTP : File Transfer Protocol

FTP uses for transferring files between ftp Client and ftp Server. It is most easiest way to
transfering files between computers (FTP Client and Ftp Server).A basic FTP connection
need a remote computer (the Ftpclient) calling an FTP server. FTP client used to download
content from the server.

Steps to follow for Download files from Ftp Server :

1 We have need to Login ftp server with configured username and password to gain access
to the Remote System.

2 enterLocalPassiveMode switches data connection mode from server-to-client to


client-to-server. There might be some connection issues if this method is not invoked.

3 Specified a remote server directory path that uses download files from the server.

4 Download file from the ftp server.

Sample Code for Downloading files to local computer from the ftp Server :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class FtpFileDownload{

public static void main(String[] args) {


String serverAddress = "www.ftpserveraddress.com"; // ftp server address
int port = 21; // ftp uses default port Number 21
String username = "xyz";// username of ftp server
String password = "xyz"; // password of ftp server
FTPClient ftpClient = new FTPClient();
try {

ftpClient.connect(serverAddress, port);
ftpClient.login(username,password);

ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE/FTP.ASCII_FILE_TYPE);
String remoteFilePath = "/filename.txt";
File localfile = new File("E:/ftpServerFile.txt");
OutputStream outputStream = new BufferedOutputStream(new
FileOutputStream(localfile));
boolean success = ftpClient.retrieveFile(remoteFilePath, outputStream);
outputStream.close();

if (success) {
System.out.println("Ftp file successfully download.");
}

} catch (IOException ex) {


System.out.println("Error occurs in downloading files from ftp Server : " +
ex.getMessage());
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

You might also like