Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. package px.ks1702;
  2.  
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5. import android.util.Log;
  6. import android.webkit.WebView;
  7.  
  8. import org.apache.commons.net.ftp.FTP;
  9. import org.apache.commons.net.ftp.FTPClient;
  10. import org.apache.commons.net.ftp.FTPFile;
  11. import org.apache.commons.net.ftp.FTPReply;
  12.  
  13. import java.io.BufferedOutputStream;
  14. import java.io.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.FileReader;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20.  
  21. /**
  22. * Created by James on 10/08/17.
  23. */
  24.  
  25. public class FtpDownloader extends AsyncTask<String, Void, String>
  26. {
  27.  
  28. private static final String LOG_TAG = "FtpDownloader";
  29. private Context context;
  30. private FTPClient mFTPClient;
  31. private File localFile;
  32.  
  33. private WebView webView;
  34.  
  35. // need to provide it with context to make it work
  36. public FtpDownloader(Context context, WebView webView)
  37. {
  38. this.context = context;
  39. this.webView = webView;
  40. }
  41.  
  42.  
  43. @Override
  44. // args is an array
  45. // args are FTPServer, username, password, filePathOnFTPServer
  46. // example 192.168.0.10, Bob, Password1, test.txt
  47. protected String doInBackground(String...args)
  48. {
  49.  
  50. String ftpServer = args[0];
  51. String username = args[1];
  52. String password = args[2];
  53. String requestedFolderName = args[3];
  54. String requestedFolderPath = context.getFilesDir().getPath().toString() + "/" + requestedFolderName;
  55.  
  56.  
  57. try
  58. {
  59. mFTPClient = new FTPClient();
  60.  
  61.  
  62. // connecting to the host
  63. mFTPClient.connect(ftpServer, 21);
  64.  
  65. // Now check the reply code, if positive mean connection success
  66. if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode()))
  67. {
  68. Log.d(LOG_TAG, "Connected Success");
  69.  
  70. // Login using username & password
  71. mFTPClient.login(username, password);
  72. Log.d(LOG_TAG, "Logged in");
  73.  
  74. // set the file type to be a plain text file
  75. mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);
  76.  
  77. mFTPClient.enterLocalPassiveMode();
  78.  
  79. Log.d(LOG_TAG, "Downloading...");
  80.  
  81. RetrieveFolder(requestedFolderName);
  82. PrintDirectory(requestedFolderPath);
  83.  
  84. // Don't worry about this return file path yet. I'm working on it
  85.  
  86. return requestedFolderPath;
  87.  
  88. }
  89. } catch(Exception e) {
  90. Log.e(LOG_TAG, "FTP Broke", e);
  91. e.printStackTrace();
  92. }
  93.  
  94. return null;
  95. }
  96. // actions performed when finished doInBackground
  97. @Override
  98. protected void onPostExecute(String result)
  99. {
  100. if (result != null)
  101. {
  102. Log.d(LOG_TAG, "Result of Download: " + result);
  103. webView.loadUrl("file://" + result + "/index1.html");
  104. }
  105.  
  106. }
  107.  
  108. /* retrieves a folder and saves it relative to /data/user/0/px.ks1702/files/ */
  109.  
  110. public void RetrieveFolder(String folderPath)
  111. {
  112. String localFolderPath = context.getFilesDir().getPath();
  113. boolean in_cnt = false;
  114. String ClientFilePath, ServerFilePath;
  115. try
  116. {
  117. FTPFile Files[] = mFTPClient.listFiles(folderPath);
  118.  
  119.  
  120. for (int i = 0; i < Files.length; i++)
  121. {
  122. /* Skip parent and current directory */
  123.  
  124. if(Files[i].getName().charAt(0) == '.')
  125. {
  126. continue;
  127. }
  128.  
  129. ClientFilePath = localFolderPath + "/" + folderPath + "/" + Files[i].getName();
  130. ServerFilePath = folderPath + "/" + Files[i].getName();
  131.  
  132. /* If another folder is found, create it and download its contents using a recursive function */
  133.  
  134. if(Files[i].isDirectory())
  135. {
  136. CreateFolder(ClientFilePath);
  137. RetrieveFolder(ServerFilePath);
  138. continue;
  139. }
  140.  
  141. // Log.d(LOG_TAG, "Filepath is: " + ClientFilePath);
  142.  
  143.  
  144. OutputStream outputStream = null;
  145. outputStream = new BufferedOutputStream(new FileOutputStream(ClientFilePath));
  146. in_cnt = mFTPClient.retrieveFile(ServerFilePath, outputStream);
  147. if(in_cnt)
  148. {
  149. outputStream.close();
  150. }
  151. else
  152. {
  153. Log.d(LOG_TAG, "File " + Files[i] + " failed to transfer successfully");
  154. }
  155. }
  156. }
  157. catch(Exception e)
  158. {
  159. Log.e(LOG_TAG, "Retrieve folder broke", e);
  160. }
  161.  
  162. }
  163.  
  164. /* Creates a folder relative to /data/user/0/px.ks1702/files/ */
  165.  
  166. private void CreateFolder(String folderPath)
  167. {
  168. try
  169. {
  170. File folder = new File(folderPath);
  171. // Log.d(LOG_TAG, folder.toString());
  172. if(!folder.exists())
  173. {
  174. if(folder.mkdirs())
  175. {
  176. // Log.d(LOG_TAG, "Directory created in " + "/" + folder.toString());
  177. }
  178. else
  179. {
  180. Log.e(LOG_TAG, "Directory failed to create");
  181. }
  182. }
  183.  
  184.  
  185. }
  186. catch (Exception e)
  187. {
  188. Log.e(LOG_TAG, "Create Folder", e);
  189. }
  190. }
  191.  
  192. /* Prints a directory relative to /data/user/0/px.ks1702/files/ */
  193.  
  194. public void PrintDirectory(String folderPath)
  195. {
  196. Log.d(LOG_TAG,"Scanning what is inside directory: " + folderPath);
  197. File directory = new File(folderPath);
  198. File[] files = directory.listFiles();
  199. Log.d(LOG_TAG,"Number of files is: "+ files.length);
  200. for (int i = 0; i < files.length; i++)
  201. {
  202. Log.d(LOG_TAG, "Files inside " + folderPath + "/"+ files[i].getName());
  203. }
  204. }
  205.  
  206. public File GetDirectory(String folderPath)
  207. {
  208. folderPath = context.getFilesDir().getPath() + "/" + folderPath;
  209.  
  210. File directory = new File(folderPath);
  211.  
  212. return directory;
  213.  
  214. }
  215.  
  216. public void ReadFile(String filePath)
  217. {
  218.  
  219. File readLocalFile = new File(filePath);
  220. Log.d(LOG_TAG, "File Path: " + filePath);
  221.  
  222. StringBuilder text = new StringBuilder();
  223.  
  224. try {
  225. BufferedReader br = new BufferedReader(new FileReader(readLocalFile));
  226. String line;
  227. text.append("Contents of file: ");
  228.  
  229. while ((line = br.readLine()) != null) {
  230. text.append(line);
  231. text.append('\n');
  232. }
  233. br.close();
  234. }
  235. catch (IOException e) {
  236. Log.e(LOG_TAG, "Read Broken", e);
  237. e.printStackTrace();
  238. //You'll need to add proper error handling here
  239. }
  240. Log.d(LOG_TAG, text.toString());
  241. }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement