2012-12-28 47 views
0

我想使用FTP協議將圖像文件從我的android設備的存儲卡上傳到我的主機服務器。但在測試運行期間,我收到一個我無法解決的錯誤。期望人們幫助我。無法將文件上傳到主機使用FTP

源代碼

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

確保請你也加入罐子FtpClient在你的項目庫

+0

我加ftpclient.jar.My應用程序時沒有錯誤,但錯誤它在 –

+0

lib ftpclient我從http://androiddev.orkitra.com/?p=139下載 –

+0

嗨看看你的連接,它應該像con.connect(「ftp.194.90.81.149」);我的意思是ftp。那麼你的IP地址 –

0

檢查,如果你已經做了以下。

  1. 檢查,如果你已經配置構建路徑
  2. 新增ftpclient.jar
  3. 檢查android_client_config tutorial如果在運行過​​程中有任何疑問
+0

我使用FTPClient.BINARY_FILE_TYPE,而不是FTP.BINARY_FILE_TYPE。這樣做是因爲我的項目不理解FTP.BINARY_FILE_TYPE。我不知道爲什麼這樣! –