【问题标题】:FTP Client in Java Android EclipseJava Android Eclipse 中的 FTP 客户端
【发布时间】:2012-08-23 23:21:25
【问题描述】:

首先 - 这是我在 StackOverflow 上提出的第一个问题。我来自德国,我的英语不太好:)

我尝试将 FTP 客户端创建为 Android 应用程序。我正在使用 Eclipse 和 Android SDK 进行编码。

这是我的代码,但它不起作用。 我使用 Apache Commons FTP 库。 你能帮助我吗?我不想要功能代码,但我喜欢获得建议以使代码正常工作。 谢谢!

这是我的代码:


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

    public class speechatStart extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);


    Button b1 = (Button) findViewById(R.id.bt_load);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


            FTPClient client = new FTPClient(); 

            TextView ausgabe = (TextView) findViewById(R.id.ausgabe);


            try {
            client.connect("ftp-web.ohost.de");
            client.login("ftp1857836", "123456789"); 
                String filename = "file1.txt"; 
                FileInputStream fis = null; 
                    fis = new FileInputStream(filename); 
                    client.storeFile(filename, fis); 
                    client.logout(); 
                    fis.close();

                    ausgabe.setText(fis);

        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ausgabe.setText("SocketException");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            ausgabe.setText("IOException");
        } 

        }
    });

【问题讨论】:

  • “它不起作用”是什么意思?您是否收到特定错误?
  • @speechat 尝试打印所有异常堆栈跟踪并显示您的 logcat:droidnova.com/debugging-in-android-using-eclipse,541.html
  • 为了您自己的安全,请隐藏您的 ftp 服务器/登录 =)
  • @Ion Aalbers - 谢谢,但这是我仅用于测试的空服务器。我有足够的其他免费网络空间...
  • @BlaineOmega - 它甚至无法连接。它什么都不做......

标签: android ftp-client


【解决方案1】:

Apache-Commons FTP 库没有为我提供可靠的解决方案。所以我使用了ftp4j,它给了我更好的解决方案,API 也很简单。

 Example:

    FTPClient client = new FTPClient();
    client.connect("ftp.host.com");
    if(client.isConnected())
    {
        client.login("username","password");
        if(client.isAuthenticated())
         {
              client.upload(new java.io.File("localFile.txt"));
          }
    }

希望对你有帮助

【讨论】:

  • link Ich habe in diesem tollen Forum eine Lösung gefunden, die funktioniert! Vielen Dank 好样的!
  • Somit ist diese Frage beantwortet und geschlossen.
  • 检查这个:link...........
【解决方案2】:

如果你想下载文件试试下一个代码:

ftpClient.retrieveFile(filename, outputStream);
outputStream.flush();
outputStream.close();
ftpClient.logout();
ftpClient.disconnect();

将更改上传到

ftpClient.storeFile(filename, inputStream);

您似乎在关闭流之前正在注销。

【讨论】:

  • 顺便问一下,您是否已将 添加到清单文件中?
猜你喜欢
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多