无法将文件上传到主机使用FTP

时间:2012-12-28 09:42:47

标签: android ftp

我想使用FTP协议将图像文件从我的Android设备的存储卡上传到我的主机服务器。但在测试运行期间,我得到一个我无法解决的错误。期待别人帮助我。

源代码

public class FTP extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ftp);
    Button upload=(Button)findViewById(R.id.button1);
    upload.setOnClickListener(this);
}
public void goforIt(){


    FTPClient con = null;

    try
    {
        con = new FTPClient();
        con.connect("longvan.webatu.com");

        if (con.login("a4602996", "25251325thanhtung"))
        {
            con.enterLocalPassiveMode(); // important!
            con.setFileType(FTPClient.BINARY_FILE_TYPE);
            String data = "/sdcard/paint/takeshi.jpg";

            FileInputStream in = new FileInputStream(new File(data));
            boolean result = con.storeFile("/paint/takeshi.jpg", in);
            in.close();
            if (result) Log.v("upload result", "succeeded");
            con.logout();
            con.disconnect();
            Toast.makeText(this, "Tai len thanh cong!", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, "Ket noi that bai!", Toast.LENGTH_LONG).show();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
        case R.id.button1:
            goforIt();

            break;

        default:
            break;
    }
}  
}

错误日志

12-28 16:35:04.039: E/AndroidRuntime(11942): java.lang.NoClassDefFoundError: org.apache.commons.net.ftp.FTPClient
12-28 16:35:04.039: E/AndroidRuntime(11942):    at com.example.ftp.FTP.goforIt(FTP.java:31)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at com.example.ftp.FTP.onClick(FTP.java:64)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.view.View.performClick(View.java:3558)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.view.View$PerformClick.run(View.java:14157)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.os.Handler.handleCallback(Handler.java:605)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.os.Looper.loop(Looper.java:137)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at android.app.ActivityThread.main(ActivityThread.java:4514)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at java.lang.reflect.Method.invoke(Method.java:511)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-28 16:35:04.039: E/AndroidRuntime(11942):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

请确保您在项目库中添加了jar FtpClient

答案 1 :(得分:0)

检查您是否已完成以下操作。

  1. 检查您是否配置了构建路径
  2. 添加了ftpclient.jar
  3. 如果您有任何疑问,请查看android_client_config tutorial