从ftp服务器下载txt文件

时间:2015-02-27 16:43:27

标签: android ftp

我试图从FTP服务器下载一个简单的txt文件已经有一段时间了。

我正在使用此代码:

 String server = "ftp.server.com";
    int port = 21;
    String user = "user";
    String pass = "pass";

    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        String remoteFile1 = "/test.txt";
        File downloadFile1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.txt");
        OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
        boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
        outputStream1.close();

        if (success) {
            System.out.println("File #1 has been downloaded successfully.");
        }

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

但Android工作室向我抛出错误:

02-27 17:40:31.106  15373-15373/.upload_file_sample E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
        at java.net.InetAddress.getByName(InetAddress.java:289)
        at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
        at .upload_file_sample.MainActivity.DownloadDatabase(MainActivity.java:104)
        at .upload_file_sample.MainActivity$2.onClick(MainActivity.java:89)
        at android.view.View.performClick(View.java:4232)
        at android.view.View$PerformClick.run(View.java:17298)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)

我无法找出原因。我仔细检查了我的FTP登录数据,但它们是正确的。我正在使用apache-commons-net library btw。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

在asynctask中添加代码。始终确保您不在主线程中进行api调用。检查这个asyntask示例

AsyncTask Android example

答案 1 :(得分:0)

  

致命异常:主要是android.os.NetworkOnMainThreadException

网络任务不能在主线程中运行,因此您必须将任务放入新线程中。

new Thread(new Runnable() {

    @Override
    public void run() {
        // Your code
    }
}).start();