将 FTP 命令变成 ASync 任务?

发布于 2024-12-25 03:03:37 字数 4324 浏览 0 评论 0原文

首先,不要因为我没有搜索而责怪我,我一直在寻找这个问题的答案,虽然有答案,但我无法理解其中的任何一个。

现在,除此之外,我正在尝试将 ftp 命令放入 Android 的异步任务中。

代码

package com.dronnoc.ftp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.io.Util;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        FTPTransfer ftp = new FTPTransfer("hostname", "user", "pass");

        String data = Environment.getDataDirectory().toString();
        data += "/data/com.dronnoc.ftp/databases/test.db";

        boolean result = ftp.upload(data, "/ftp_dir/test.db");
        if(result)
        {
            Log.d("message", "Upload Successful.");
        }
        else
        {
            Log.e("message", ftp.error());
        }

    ftp.close();
    }
}

FTPTransfer.java

package com.dronnoc.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;

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


public class FTPTransfer {

    final FTPClient ftp = new FTPClient();
    private String error = null;
    private boolean connected = false;

    public FTPTransfer(String host, String username, String pass) {
        // TODO Auto-generated constructor stub
        try {
            ftp.connect(host);

            if(ftp.login(username, pass))
            {
                ftp.enterLocalPassiveMode();
                connected = true;
            }
        }
        catch (FTPConnectionClosedException e) { error = e.getMessage(); }
        catch (SocketException e) { error = e.getMessage(); }
        catch (IOException e) { error = e.getMessage(); }
    }


    public boolean upload(String localName, String remoteName)
    {
        if(ftp.isConnected() && connected)
        {
            try {
                FileInputStream file = new FileInputStream(localName);
                boolean result = ftp.storeFile(remoteName, file);
                if(result) { return true; }
                else { return false; }
            } 
            catch (FileNotFoundException e) { error = e.getMessage(); return false; } 
            catch (IOException e) { error = e.getMessage(); return false; }
        }
        return false;
    }

    public boolean upload(File localName, String remoteName)
    {
        if(ftp.isConnected() && connected)
        {
            try {
                FileInputStream file = new FileInputStream(localName.getAbsolutePath());
                boolean result = ftp.storeFile(remoteName, file);
                if(result) { return true; }
                else { return false; }
            } 
            catch (FileNotFoundException e) { error = e.getMessage(); return false; } 
            catch (IOException e) { error = e.getMessage(); return false; }
        }
        return false;
    }

    public boolean download(String remote, String local)
    {
        //TODO Put appropriate code here
        return false;
    }


    public boolean close()
    {
        try {
            ftp.disconnect();
            return true;
        } catch (IOException e) {
            error = e.getMessage();
            return false;
        }
    }

    public String error()
    {
        return error;
    }

}

我想知道的是如何将我的 FTP 函数放入异步任务中,以便我可以在后台活动中运行它并更新进度栏,并指出到目前为止已上传了多少字节?

干杯

编辑 代码本身目前可以工作,我只需要知道如何将其变成异步任务

First off, don't flame me for not searching, I've looked for answers to this and while there are answers out there, I can't understand any of them.

Now, with that aside, I'm trying to put my ftp command into an ASync task for Android.

Code:

package com.dronnoc.ftp;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.io.Util;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        FTPTransfer ftp = new FTPTransfer("hostname", "user", "pass");

        String data = Environment.getDataDirectory().toString();
        data += "/data/com.dronnoc.ftp/databases/test.db";

        boolean result = ftp.upload(data, "/ftp_dir/test.db");
        if(result)
        {
            Log.d("message", "Upload Successful.");
        }
        else
        {
            Log.e("message", ftp.error());
        }

    ftp.close();
    }
}

FTPTransfer.java

package com.dronnoc.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;

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


public class FTPTransfer {

    final FTPClient ftp = new FTPClient();
    private String error = null;
    private boolean connected = false;

    public FTPTransfer(String host, String username, String pass) {
        // TODO Auto-generated constructor stub
        try {
            ftp.connect(host);

            if(ftp.login(username, pass))
            {
                ftp.enterLocalPassiveMode();
                connected = true;
            }
        }
        catch (FTPConnectionClosedException e) { error = e.getMessage(); }
        catch (SocketException e) { error = e.getMessage(); }
        catch (IOException e) { error = e.getMessage(); }
    }


    public boolean upload(String localName, String remoteName)
    {
        if(ftp.isConnected() && connected)
        {
            try {
                FileInputStream file = new FileInputStream(localName);
                boolean result = ftp.storeFile(remoteName, file);
                if(result) { return true; }
                else { return false; }
            } 
            catch (FileNotFoundException e) { error = e.getMessage(); return false; } 
            catch (IOException e) { error = e.getMessage(); return false; }
        }
        return false;
    }

    public boolean upload(File localName, String remoteName)
    {
        if(ftp.isConnected() && connected)
        {
            try {
                FileInputStream file = new FileInputStream(localName.getAbsolutePath());
                boolean result = ftp.storeFile(remoteName, file);
                if(result) { return true; }
                else { return false; }
            } 
            catch (FileNotFoundException e) { error = e.getMessage(); return false; } 
            catch (IOException e) { error = e.getMessage(); return false; }
        }
        return false;
    }

    public boolean download(String remote, String local)
    {
        //TODO Put appropriate code here
        return false;
    }


    public boolean close()
    {
        try {
            ftp.disconnect();
            return true;
        } catch (IOException e) {
            error = e.getMessage();
            return false;
        }
    }

    public String error()
    {
        return error;
    }

}

What I want to know is how I can put my FTP function into an ASync task so that I can run it in a background activity and update a progress bar, and also indicate how many bytes its uploaded so far?

Cheers

EDIT
The code itself works at the moment, I just need to know how to make it into an Async task

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2025-01-01 03:03:37

不确定您是否询问如何使用 AsyncTask,但如果您想知道,这里有一个有关使用 AsyncTask 向 Web 服务发出请求的教程。这可以轻松扩展以在后台执行 FTP。 AsyncTask 也可以支持 Progress,尽管我认为教程中没有提到这一点。

基本上,您的 upload 函数需要移动到 doInBackground,连接代码也是如此。请参阅 http://geekjamboree.wordpress.com /2011/11/22/asynctask-call-web-services-in-android/

Not sure if you're asking how to use the AsyncTask but in case you are here's a tutorial about using AsyncTask to make requests to a web service. This can be extended easily to perform FTP in the background. AsyncTask can also support Progress though I don't think it's mentioned in the tutorial.

Basically your upload function needs to move to doInBackground and so does the connection code. See http://geekjamboree.wordpress.com/2011/11/22/asynctask-call-web-services-in-android/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文