Edward Humphreys
- Forum posts: 2
Mar 31, 2013, 10:14:15 PM via Website
Mar 31, 2013 10:14:15 PM via Website
I am trying to use FTP to in my application.
Here's some variables
I have a method to connect, disconnect and copy a file from src to dest
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.
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?
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;
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 }
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 }}
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
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.