Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package px.ks1702;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.webkit.WebView;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import java.io.BufferedOutputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.OutputStream;
- /**
- * Created by James on 10/08/17.
- */
- public class FtpDownloader extends AsyncTask<String, Void, String>
- {
- private static final String LOG_TAG = "FtpDownloader";
- private Context context;
- private FTPClient mFTPClient;
- private File localFile;
- private WebView webView;
- // need to provide it with context to make it work
- public FtpDownloader(Context context, WebView webView)
- {
- this.context = context;
- this.webView = webView;
- }
- @Override
- // args is an array
- // args are FTPServer, username, password, filePathOnFTPServer
- // example 192.168.0.10, Bob, Password1, test.txt
- protected String doInBackground(String...args)
- {
- String ftpServer = args[0];
- String username = args[1];
- String password = args[2];
- String requestedFolderName = args[3];
- String requestedFolderPath = context.getFilesDir().getPath().toString() + "/" + requestedFolderName;
- try
- {
- mFTPClient = new FTPClient();
- // connecting to the host
- mFTPClient.connect(ftpServer, 21);
- // Now check the reply code, if positive mean connection success
- if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode()))
- {
- Log.d(LOG_TAG, "Connected Success");
- // Login using username & password
- mFTPClient.login(username, password);
- Log.d(LOG_TAG, "Logged in");
- // set the file type to be a plain text file
- mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);
- mFTPClient.enterLocalPassiveMode();
- Log.d(LOG_TAG, "Downloading...");
- RetrieveFolder(requestedFolderName);
- PrintDirectory(requestedFolderPath);
- // Don't worry about this return file path yet. I'm working on it
- return requestedFolderPath;
- }
- } catch(Exception e) {
- Log.e(LOG_TAG, "FTP Broke", e);
- e.printStackTrace();
- }
- return null;
- }
- // actions performed when finished doInBackground
- @Override
- protected void onPostExecute(String result)
- {
- if (result != null)
- {
- Log.d(LOG_TAG, "Result of Download: " + result);
- webView.loadUrl("file://" + result + "/index1.html");
- }
- }
- /* retrieves a folder and saves it relative to /data/user/0/px.ks1702/files/ */
- public void RetrieveFolder(String folderPath)
- {
- String localFolderPath = context.getFilesDir().getPath();
- boolean in_cnt = false;
- String ClientFilePath, ServerFilePath;
- try
- {
- FTPFile Files[] = mFTPClient.listFiles(folderPath);
- for (int i = 0; i < Files.length; i++)
- {
- /* Skip parent and current directory */
- if(Files[i].getName().charAt(0) == '.')
- {
- continue;
- }
- ClientFilePath = localFolderPath + "/" + folderPath + "/" + Files[i].getName();
- ServerFilePath = folderPath + "/" + Files[i].getName();
- /* If another folder is found, create it and download its contents using a recursive function */
- if(Files[i].isDirectory())
- {
- CreateFolder(ClientFilePath);
- RetrieveFolder(ServerFilePath);
- continue;
- }
- // Log.d(LOG_TAG, "Filepath is: " + ClientFilePath);
- OutputStream outputStream = null;
- outputStream = new BufferedOutputStream(new FileOutputStream(ClientFilePath));
- in_cnt = mFTPClient.retrieveFile(ServerFilePath, outputStream);
- if(in_cnt)
- {
- outputStream.close();
- }
- else
- {
- Log.d(LOG_TAG, "File " + Files[i] + " failed to transfer successfully");
- }
- }
- }
- catch(Exception e)
- {
- Log.e(LOG_TAG, "Retrieve folder broke", e);
- }
- }
- /* Creates a folder relative to /data/user/0/px.ks1702/files/ */
- private void CreateFolder(String folderPath)
- {
- try
- {
- File folder = new File(folderPath);
- // Log.d(LOG_TAG, folder.toString());
- if(!folder.exists())
- {
- if(folder.mkdirs())
- {
- // Log.d(LOG_TAG, "Directory created in " + "/" + folder.toString());
- }
- else
- {
- Log.e(LOG_TAG, "Directory failed to create");
- }
- }
- }
- catch (Exception e)
- {
- Log.e(LOG_TAG, "Create Folder", e);
- }
- }
- /* Prints a directory relative to /data/user/0/px.ks1702/files/ */
- public void PrintDirectory(String folderPath)
- {
- Log.d(LOG_TAG,"Scanning what is inside directory: " + folderPath);
- File directory = new File(folderPath);
- File[] files = directory.listFiles();
- Log.d(LOG_TAG,"Number of files is: "+ files.length);
- for (int i = 0; i < files.length; i++)
- {
- Log.d(LOG_TAG, "Files inside " + folderPath + "/"+ files[i].getName());
- }
- }
- public File GetDirectory(String folderPath)
- {
- folderPath = context.getFilesDir().getPath() + "/" + folderPath;
- File directory = new File(folderPath);
- return directory;
- }
- public void ReadFile(String filePath)
- {
- File readLocalFile = new File(filePath);
- Log.d(LOG_TAG, "File Path: " + filePath);
- StringBuilder text = new StringBuilder();
- try {
- BufferedReader br = new BufferedReader(new FileReader(readLocalFile));
- String line;
- text.append("Contents of file: ");
- while ((line = br.readLine()) != null) {
- text.append(line);
- text.append('\n');
- }
- br.close();
- }
- catch (IOException e) {
- Log.e(LOG_TAG, "Read Broken", e);
- e.printStackTrace();
- //You'll need to add proper error handling here
- }
- Log.d(LOG_TAG, text.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement