0

首先 - 这是我在 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");
        } 

        }
    });

4

2 回答 2

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"));
          }
    }

希望这可以帮助

于 2012-08-23T15:21:25.930 回答
0

如果要下载文件,请尝试下一个代码:

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

将更改上传到

ftpClient.storeFile(filename, inputStream);

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

于 2012-08-23T13:48:23.483 回答