Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. server = "192.168.0.100";
  2. port = 21;
  3. user = "anonymous";
  4. pass = "anonymous";
  5.  
  6.  
  7. ftpConnect(server, user, pass, port);
  8. ftpPrintFilesList("/");
  9. ftpChangeDirectory("/datalogger/");
  10. ftpPrintFilesList("/datalogger/");
  11. ftpDownload("/datalogger/", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
  12. ftpDisconnect();
  13.  
  14.  
  15.  
  16. public boolean ftpConnect(String host, String username, String password, int port)
  17. {
  18. try {
  19. ftpClient = new FTPClient();
  20. // connecting to the host
  21. ftpClient.connect(host, port);
  22.  
  23. // now check the reply code, if positive mean connection success
  24. if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
  25. ftpClient.enterLocalPassiveMode();
  26. // login using username & password
  27. boolean status = ftpClient.login(username, password);
  28.  
  29. /* Set File Transfer Mode
  30. *
  31. * To avoid corruption issue you must specified a correct
  32. * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
  33. * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
  34. * for transferring text, image, and compressed files.
  35. */
  36. ftpClient.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
  37. ftpClient.enterLocalPassiveMode();
  38. Log.d(TAG, "Buffer Size:" + ftpClient.getBufferSize());
  39. this.ftpClient.setBufferSize(1024 * 1024);
  40. Log.d(TAG, "Buffer Size:" + ftpClient.getBufferSize());
  41. ftpClient.setAutodetectUTF8(true);
  42. return status;
  43. }
  44. } catch(Exception e) {
  45. Log.d(TAG, "Error: could not connect to host " + host );
  46. }
  47.  
  48.  
  49. return false;
  50. }
  51.  
  52.  
  53. public void ftpPrintFilesList(String dir_path)
  54. {
  55. try {
  56. FTPFile[] ftpFiles = ftpClient.listFiles(dir_path);
  57. int length = ftpFiles.length;
  58.  
  59. for (int i = 0; i < length; i++) {
  60. String name = ftpFiles[i].getName();
  61. boolean isFile = ftpFiles[i].isFile();
  62.  
  63. if (isFile) {
  64. Log.i(TAG, "File : " + name);
  65. }
  66. else {
  67. Log.i(TAG, "Directory : " + name);
  68. }
  69. }
  70. } catch(Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement