2011-08-04 6 views
0

私はこのコードを動作させるために多くの時間を費やしましたが、現在の状態ではほぼhttps://stackoverflow.com/questions/3482583/apache-ftpclient-failing-to-download-larger-filesというコードを反映しています。私はまた、apacheコモンズネットを使用しています。 何らかの理由で私のアプリケーションがクライアントの接続ステップを過ぎることはありません。たとえ動作するはずのftpサーバの他のIPアドレスを接続しても。自分のアンドロイドアプリケーションで自分のftpサーバにファイルを保存しようとしました

import java.io.BufferedOutputStream; 
import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPFile; 
import org.apache.commons.net.ftp.*; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageView; 



public class FtpClientService extends Activity { 
    //static int fail; 


public FtpClientService(){ 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.ftp); 
    FtpConnect(); 
} 

public static void FtpConnect(){ 
    String userName="usrname"; 
    String passWord = "password"; 
    String ftpAddress = "127.0.0.1"; //this isnt the ip address I was using... 
    String retrieveFromFTPFolder = "/Pictures/"; 
    String strLine; 
    DataInputStream inputStream = null; 
    BufferedReader bufferedReader = null; 
    FTPClient client = null; 
    FTPFile[] ftpFiles = null; 
    int reply; 

    try{ 

     client = new FTPClient(); 
    client.setListHiddenFiles(true); 
    client.connect(ftpAddress); //this right here is where it fails 
    client.login(userName, passWord); 
    client.setFileType(FTP.BINARY_FILE_TYPE); 
     if(!client.completePendingCommand()) { 
      client.logout(); 
      client.disconnect(); 
      System.err.println("File transfer failed."); 

     } 

    } catch (Exception e) { 
    if (client.isConnected()) { 
    try { 
    client.logout(); 
    client.disconnect(); 
    } 
    catch (IOException f) {} 
    } 
    } 
    finally{ 
    if (client.isConnected()) { 
    try { 
    client.logout(); 
    client.disconnect(); 
    } 
    catch (IOException f){} 
    } 
    } 
} 

public static void main(String[] args) { 
    FtpConnect(); 
} 

ありがとうございます!

+0

あなたはスタックトレースを提供することはできますか?または何かエラーメッセージが表示されますか? – CrackerJack9

+0

スタックトレースの出力は何ですか?例外からのメッセージはあなたに手がかりを与えるかもしれません。 – brindy

答えて

0

connectメソッドでは、InetAddressを適切なアドレスとポートで渡します。

また、メインスレッドでFTP接続を実行しないでください。AsyncTaskを使用することを検討してください。

0

は、これを使用してみてください:

client.connect(InetAddress.getByAddress(ftpAddress,new byte[]{127,0,0,1})); 
関連する問題