/Android/FtpClient/src/com/phonegap/plugins/ftpclient/FtpClient.java

http://github.com/phonegap/phonegap-plugins · Java · 159 lines · 86 code · 25 blank · 48 comment · 8 complexity · d58c19a2cac4499e18281ed79ef4226d MD5 · raw file

  1. /*
  2. * PhoneGap is available under *either* the terms of the modified BSD license *or* the
  3. * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
  4. *
  5. * Copyright (c) 2005-2010, Nitobi Software Inc.
  6. * Copyright (c) 2010, IBM Corporation
  7. */
  8. package com.phonegap.plugins.ftpclient;
  9. import java.io.BufferedInputStream;
  10. import java.io.BufferedOutputStream;
  11. import java.io.FileInputStream;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.util.StringTokenizer;
  17. import org.apache.commons.net.ftp.FTP;
  18. import org.apache.commons.net.ftp.FTPClient;
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import android.util.Log;
  22. import com.phonegap.api.Plugin;
  23. import com.phonegap.api.PluginResult;
  24. public class FtpClient extends Plugin {
  25. private static final String LOG_TAG = "FtpClient";
  26. /**
  27. * Executes the request and returns PluginResult.
  28. *
  29. * @param action The action to execute.
  30. * @param args JSONArry of arguments for the plugin.
  31. * @param callbackId The callback id used when calling back into JavaScript.
  32. * @return A PluginResult object with a status and message.
  33. */
  34. @Override
  35. public PluginResult execute(String action, JSONArray args, String callbackId) {
  36. PluginResult.Status status = PluginResult.Status.OK;
  37. JSONArray result = new JSONArray();
  38. try {
  39. String filename = args.getString(0);
  40. URL url = new URL(args.getString(1));
  41. if (action.equals("get")) {
  42. get(filename, url);
  43. }
  44. else if (action.equals("put")) {
  45. put(filename, url);
  46. }
  47. return new PluginResult(status, result);
  48. } catch (JSONException e) {
  49. return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
  50. } catch (MalformedURLException e) {
  51. return new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION);
  52. } catch (IOException e) {
  53. return new PluginResult(PluginResult.Status.IO_EXCEPTION);
  54. }
  55. }
  56. /**
  57. * Uploads a file to a ftp server.
  58. * @param filename the name of the local file to send to the server
  59. * @param url the url of the server
  60. * @throws IOException
  61. */
  62. private void put(String filename, URL url) throws IOException {
  63. FTPClient f = setup(url);
  64. BufferedInputStream buffIn=null;
  65. buffIn=new BufferedInputStream(new FileInputStream(filename));
  66. f.storeFile(extractFileName(url), buffIn);
  67. buffIn.close();
  68. teardown(f);
  69. }
  70. /**
  71. * Downloads a file from a ftp server.
  72. * @param filename the name to store the file locally
  73. * @param url the url of the server
  74. * @throws IOException
  75. */
  76. private void get(String filename, URL url) throws IOException {
  77. FTPClient f = setup(url);
  78. BufferedOutputStream buffOut=null;
  79. buffOut=new BufferedOutputStream(new FileOutputStream(filename));
  80. f.retrieveFile(extractFileName(url), buffOut);
  81. buffOut.flush();
  82. buffOut.close();
  83. teardown(f);
  84. }
  85. /**
  86. * Tears down the FTP connection
  87. * @param f the FTPClient
  88. * @throws IOException
  89. */
  90. private void teardown(FTPClient f) throws IOException {
  91. f.logout();
  92. f.disconnect();
  93. }
  94. /**
  95. * Creates, connects and logs into a FTP server
  96. * @param url of the FTP server
  97. * @return an instance of FTPClient
  98. * @throws IOException
  99. */
  100. private FTPClient setup(URL url) throws IOException {
  101. FTPClient f = new FTPClient();
  102. f.connect(url.getHost(), extractPort(url));
  103. StringTokenizer tok = new StringTokenizer(url.getUserInfo(), ":");
  104. f.login(tok.nextToken(), tok.nextToken());
  105. f.enterLocalPassiveMode();
  106. f.setFileType(FTP.BINARY_FILE_TYPE);
  107. return f;
  108. }
  109. /**
  110. * Extracts the port of the FTP server. Returns 21 by default.
  111. * @param url
  112. * @return
  113. */
  114. private int extractPort(URL url) {
  115. if (url.getPort() == -1) {
  116. return url.getDefaultPort();
  117. }
  118. else {
  119. return url.getPort();
  120. }
  121. }
  122. /**
  123. * Extracts the file name from the URL.
  124. * @param url of the ftp server, includes the file to upload/download
  125. * @return the filename to upload/download
  126. */
  127. private String extractFileName(URL url) {
  128. String filename = url.getFile();
  129. if (filename.endsWith(";type=i") || filename.endsWith(";type=a")) {
  130. filename = filename.substring(0, filename.length() - 7);
  131. }
  132. return filename;
  133. }
  134. }