FTP connection problem, connection exception

  • Replies:2
  • OpenNot stickiedUnanswered

Mar 31, 2013 10:14:15 PM

I am trying to use FTP to in my application.

Here's some variables

1public FTPClient mFTPClient = null;
2
3 private String username = "user";
4 private String password = "password";
5 private String ftpServer = "10.0.2.2";
6 private Integer ftpPort = 21;

I have a method to connect, disconnect and copy a file from src to dest

1public boolean ftpConnect(String host, String username,
2 String password, int port)
3 {
4 try {
5 mFTPClient = new FTPClient();
6 // connecting to the host
7 mFTPClient.connect(host, port);
8
9 // now check the reply code, if positive mean connection success
10 if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
11 // login using username & password
12 boolean status = mFTPClient.login(username, password);
13
14 /* Set File Transfer Mode
15 *
16 * To avoid corruption issue you must specified a correct
17 * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
18 * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
19 * for transferring text, image, and compressed files.
20 */
21 mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
22 mFTPClient.enterLocalPassiveMode();
23
24 return status;
25 }
26 } catch(Exception e) {
27 System.out.println("Exception connecting to FTP server:" + e);
28 }
29
30 return false;
31 }
32 /* Method to disconnect from FTP server */
33 public boolean ftpDisconnect()
34 {
35 try {
36 mFTPClient.logout();
37 mFTPClient.disconnect();
38 return true;
39 } catch (Exception e) {
40 System.out.println("Exception disconnecting from FTP server:" + e);
41 }
42
43 return false;
44 }
45 /* Method to get current working directory */
46 public String ftpGetCurrentWorkingDirectory()
47 {
48 try {
49 String workingDir = mFTPClient.printWorkingDirectory();
50 return workingDir;
51 } catch(Exception e) {
52 System.out.println("Exception getting current working directory:" + e);
53 }
54
55 return null;
56 }
57
58public boolean ftpDownload(String srcFilePath, String desFilePath)
59 {
60 boolean status = false;
61 try {
62 //FileOutputStream desFileStream = new FileOutputStream(desFilePath);
63 FileOutputStream desFileStream = openFileOutput(desFilePath, Context.MODE_PRIVATE);
64
65 status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
66 desFileStream.close();
67
68 return status;
69 } catch (Exception e) {
70 System.out.println("download failed: "+e);
71 }
72
73 return status;
74 }

I am bringing these together in a Async task in order to be able to connect to the ftp server, copy a file from it's root directory, which is /home/username, to the SD card and then disconnect.

1private class DownloadFileFromFTP extends AsyncTask<String, Void, String> {
2
3 @Override
4 protected String doInBackground(String... empty) {
5 String strResponce = "";
6
7 Boolean connectStatus = ftpConnect(ftpServer, username,
8 password, ftpPort);
9 if (connectStatus == true){
10 String src = ftpGetCurrentWorkingDirectory().toString()+"/ftpserverfile.txt";
11 String dest = "ftpserverfile.txt";
12
13 ftpPrintFilesList(ftpGetCurrentWorkingDirectory());
14 System.out.println("Downloading "+src+" to "+dest);
15 Boolean ftpDownloadStatus = ftpDownload(src, dest);
16
17 if (ftpDownloadStatus == true){
18 strResponce = "true";
19 }
20 else{
21 strResponce = "false";
22 }
23 }
24 ftpDisconnect();
25
26 return strResponce;
27 }
28 @Override
29 protected void onPostExecute(String result) {
30
31 }}

After connectStatus returns true I get a connection exception, failed to connect to /127.0.0.1 (port 61360).

I can't work this out becasue I have my ftp server set as 10.0.2.2, why is the loopback address being used? The ftpConnect() seems to work find, but then there are connection issues?

— modified on Mar 31, 2013 10:16:40 PM

Apr 2, 2013 12:08:09 PM

I have found the solution to my problem.

The soultion was to use the IP address of the FTP server (which was my localhost IP 192.168.x.x etc), the FTP operations where failing when using 10.0.2.2 to refer to the localhost. At somewhere along the way this was being used as 127.0.0.1 which was casuing errors

Apr 3, 2013 10:35:27 PM

Dear Friend,
Dear I need your help. Hope you will help me. I am develop android FTP client.
I am facing a problem that I am unable to get local host directory file list. login
function is ok. local host enters passive mode but I am unable to get file list.
Can u please let me know how to fix it.
Can you please help me. I will be thankful to you.