0

まず第一に - これは私が StackOverflow で尋ねる最初の質問です。そして、私はドイツ出身で、私の英語はあまり上手ではありません:)

FTP クライアントを Android アプリとして作成しようとしています。私はEclipseとAndroid SDKでコーディングしています。

これは私のコードですが、機能しません。私は Apache Commons FTP Library を使用しています。手伝って頂けますか?機能的なコードは必要ありませんが、コードを機能させるためのアドバイスを得るのが大好きです。ありがとう!

だからここに私のコードがあります:


    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 に答える