0

我正在尝试将文件上传到服务器。通过 FTP 将文件上传到服务器的方式是什么?

我写了这门课:

服务器连接.java:

import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.SocketClient;
import org.apache.commons.net.ftp.FTPClient;


public class serverconnection
{

    public FTPClient connectftp() 
    {
        FTPClient ftp = null;
        try {
            ftp.connect("ftp://ftp.drivehq.com/");
            ftp.login("zule", "*****");
        //  ftp.changeWorkingDirectory("/public");
        //  ftp.makeDirectory("200");
        } catch (SocketException en) {
            // TODO Auto-generated catch block
            en.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return ftp;
    }
}

这是 mainActivity (只有相关代码):

import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;



public class MainActivity extends Activity implements OnClickListener {
    Button scan;
    String contents;
    String format;
    TextView contentstext;
    TextView formattext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------FTP-------------
        serverconnection ftpconnect =new serverconnection();
        FTPClient ftp=ftpconnect.connectftp();
        scan=(Button)findViewById(R.id.scanbutton);
        .....

当我在手机上安装应用程序时出现错误:“不幸的是,您的应用程序必须停止...”

新代码:

public class MainActivity extends Activity implements OnClickListener {
    Button scan;
    String contents;
    String format;
    TextView contentstext;
    TextView formattext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //----------------------FTP-------------
        //serverconnection ftpconnect =new serverconnection();
        //SimpleFTP ftp=ftpconnect.connectftp();
        SimpleFTP ftp = new SimpleFTP();
        try {
            ftp.connect("market.bugs3.com", 21, "u884282808", "lionetwork1");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

新的日志猫:

09-16 13:43:31.131: E/AndroidRuntime(1203): FATAL EXCEPTION: main
09-16 13:43:31.131: E/AndroidRuntime(1203): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.market/com.example.market.MainActivity}: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.Looper.loop(Looper.java:137)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.lang.reflect.Method.invoke(Method.java:511)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at dalvik.system.NativeStart.main(Native Method)
09-16 13:43:31.131: E/AndroidRuntime(1203): Caused by: android.os.NetworkOnMainThreadException
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.tryAllAddresses(Socket.java:108)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.<init>(Socket.java:177)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at java.net.Socket.<init>(Socket.java:149)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at com.example.market.MainActivity.onCreate(MainActivity.java:31)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.Activity.performCreate(Activity.java:4465)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-16 13:43:31.131: E/AndroidRuntime(1203):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-16 13:43:31.131: E/AndroidRuntime(1203):     ... 11 more
4

1 回答 1

3

注意:AsyncTask类在 API 级别 30 中已弃用。请java.util.concurrent改用。

问题是您试图在主线程上进行网络调用。这在 Android 3.0 或更高版本上是不允许的。

您应该通过在不同线程上调用 FTP 服务器来解决此问题。一个很好的方法是使用 AsyncTask:

private class FtpTask extends AsyncTask<Void, Void, FTPClient> {
     protected FTPClient doInBackground(Void... args) {
        serverconnection ftpconnect =new serverconnection();
        FTPClient ftp=ftpconnect.connectftp();
        return ftp;
     }

     protected void onPostExecute(FTPClient result) {
         Log.v("FTPTask","FTP connection complete");
         ftpClient = result;
         //Where ftpClient is a instance variable in the main activity
     }
 }

然后您可以在主线程中使用以下代码运行此后台线程:

new FtpTask().execute();

编辑:

如果要在不同方法之间传递参数,可以更改 AsyncTask<Void, Void, Void>超类初始化。

例如AsyncTask<String, Double, Integer>,可以将 String 变量传递给 doInBackground 方法,使用 double 跟踪进度并使用整数作为结果类型(结果类型是 doInBackground 的返回类型,它将作为一个发送到 onPostExecute范围)。

于 2012-09-16T14:02:49.713 回答