1

I am trying to send a file via FTP using a ASync Task, here is what I have found so far but can not call it to send file:

package com.hdwinc.hdworderentry;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import android.os.AsyncTask;
import android.util.Log;

public abstract class HDWFTP_Upload extends AsyncTask{
//void FTP_DATA_UPLOAD(String FULL_PATH_TO_LOCAL_FILE)


 protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {
  // encapsulate FTP inside a A sync task

  {
   System.out.println("Entered FTP transfer function");

   FTPClient ftpClient = new FTPClient();
   int reply;
  try {
   System.out.println("Entered Data Upload loop!");
      ftpClient.connect(ServerName,21);
      ftpClient.login(Username, Password);
      ftpClient.sendCommand("QUOTE SITE NAMEFMT 1");
      ftpClient.changeWorkingDirectory("/OPTICON/");
      System.out.println("Entered Data Upload loop!");


      reply = ftpClient.getReplyCode();


      if(FTPReply.isPositiveCompletion(reply)){
       System.out.println("Connected Success");
      }else {
       System.out.println("Connection Failed");
       ftpClient.disconnect();
      }



      if (ftpClient.getReplyString().contains("250")) {
          ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
          BufferedInputStream buffIn = null;
          System.out.println("Created an input stream buffer");
          System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());

          buffIn = new BufferedInputStream(new          FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
          ftpClient.enterLocalPassiveMode();

          System.out.println("Entered binary and passive modes");
          //Handler progressHandler=null;
    //ProgressInputStream progressInput = new ProgressInputStream(buffIn,    progressHandler);

          //Code to Extract name from the string.
          //http://stackoverflow.com/questions/10549504/obtain-name-from-absolute-  path-substring-from-last-slash-java-android
          String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();


          boolean result = ftpClient.storeFile(Picture_File_name, buffIn);     //localAsset.getFileName()
          //ProgressInputStream progressInput = new ProgressInputStream(buffIn,   progressHandler);

          if (result){
           System.out.println("Success");
          }

          //boolean result = ftpClient.storeFile("TEST.jpg", progressInput);
          System.out.println("File saved");


          //buffIn.close();
          ftpClient.logout();
          ftpClient.disconnect();
      }

  } catch (SocketException e) {
      Log.e("Citizen Cam FTP", e.getStackTrace().toString());
      System.out.println("Socket Exception!");
  } catch (UnknownHostException e) {
      Log.e("Citizen Cam FTP", e.getStackTrace().toString());
  } catch (IOException e) {
      Log.e("Citizen Cam FTP", e.getStackTrace().toString());
      System.out.println("IO Exception!");
  }

  return null;
 }


 } 


}

How would I call it in another Activity? I have my order activity creates a file and I need to send it to the server.

Garling Beard
  • 101
  • 3
  • 14
  • Why have you declared your `AsyncTask` as `abstract`? That makes no sense. – Squonk Mar 25 '14 at 16:32
  • The [developer site](http://developer.android.com/reference/android/os/AsyncTask.html) has a good explanation of how to use `AsyncTask`. Also, [this SO posting](http://stackoverflow.com/questions/22433509/can-somebody-explain-how-to-use-asynctask-with-android) has some illuminating stuff. – Mike M. Mar 25 '14 at 16:52

1 Answers1

2

I removed the abstract reference per #Squonk and looked at the two pages that #Mike M provided and came up with a solution.

Thanks guys.

public class HDWFTP_Upload extends AsyncTask <String, Void, Long>{



 protected Long doInBackground(String... FULL_PATH_TO_LOCAL_FILE ) {

  {


   FTPClient ftpClient = new FTPClient();
   int reply;
  try {
   System.out.println("Entered Data Upload loop!");
      ftpClient.connect(servername,21);
      ftpClient.login(user, pass);
      ftpClient.sendCommand("QUOTE SITE NAMEFMT 1");
      ftpClient.changeWorkingDirectory("/OPTICON/");          


      reply = ftpClient.getReplyCode();


      if(FTPReply.isPositiveCompletion(reply)){
       System.out.println("Connected Success");
      }else {
       System.out.println("Connection Failed");
       ftpClient.disconnect();
      }



      if (ftpClient.getReplyString().contains("250")) {
          ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
          BufferedInputStream buffIn = null;
          System.out.println("Created an input stream buffer");
          System.out.println(FULL_PATH_TO_LOCAL_FILE.toString());

          buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE[0]));
          ftpClient.enterLocalPassiveMode();

          System.out.println("Entered binary and passive modes");

          String Picture_File_name = new File(FULL_PATH_TO_LOCAL_FILE[0]).getName();


          boolean result = ftpClient.storeFile(Picture_File_name, buffIn);

          if (result){
           System.out.println("Success");
          }


          System.out.println("File saved");



          ftpClient.logout();
          ftpClient.disconnect();
      }

  } catch (SocketException e) {
      Log.e("HDW FTP", e.getStackTrace().toString());
      System.out.println("Socket Exception!");
  } catch (UnknownHostException e) {
      Log.e("HDW FTP", e.getStackTrace().toString());
  } catch (IOException e) {
      Log.e("HDW FTP", e.getStackTrace().toString());
      System.out.println("IO Exception!");
  }

  return null;

  }




 }

Called with:

new HDWFTP_Upload().execute(uploadFile);
Garling Beard
  • 101
  • 3
  • 14