2017-09-28 22 views
0

ftpでファイルをアップロードします。 私はいくつかの情報がそれについてです発見し、私はここでそれを見つけたように私のクラスを構築しました:http://www.sebastianviereck.de/ftp-upload-mit-android/Android FTPアップロード - ファイルがアップロードされていません

私はまた、マニフェストのインターネットのアクセス許可を追加し、私はここでそれを見つけたようstrictmodeを設定します。 Error StrictMode$AndroidBlockGuardPolicy.onNetwork

結果storeFile ist treuであり、ftpClientの応答コードは226です(「データ接続を閉じる。要求されたファイル操作が成功した(ファイル転送またはファイルアボートなど)」)。

だから私にとってはうまく見えますが、ファイルはアップロードされません。

誰かがアイデアを持っていますか?ここで

は私のコードです:

メイン:

FtpDataHandler ftp = new FtpDataHandler("serverName", "userName", "password", "/folder"); 
        ftp.uploadFile("/storage/emulated/0/pictures/test_sig.jpg", "test_sig.jpg"); 

FTPハンドラ:ここ

import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

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

import android.util.Log; 

public class FtpDataHandler { 
    private static final String TAG = "FtpDataHandler"; 

    String serverAdress; 
    String userName; 
    String password; 
    String serverDirectory; 
    FTPClient ftpClient; 

    public FtpDataHandler(String serverAdress, String userName, String password, String serverDiretory) { 
     this.serverAdress = serverAdress; 
     this.userName = userName; 
     this.password = password; 
     //this was my problem. I set the serverAdress to the serverDirectory insted the serverDirectory 
     //this.serverDirectory = serverAdress; 
     this.serverDirectory = serverDirectory; 

     connect(); 
    } 

    public String getServerAdress() { 
     return serverAdress; 
    } 

    public String getUserName() { 
     return userName; 
    } 

    public String getServerDirectory() { 
     return serverDirectory; 
    } 

    public boolean uploadFile(String localFilePath, String remoteFileName) { 
     boolean result = false; 

     BufferedInputStream buffIn = null; 
     try { 
      buffIn = new BufferedInputStream(new FileInputStream(localFilePath)); 
     } catch (FileNotFoundException e) { 
      Log.d(TAG, 
        "FileNotFoundException: local File to be uploaded not Found: " + localFilePath); 
     } 
     ftpClient.enterLocalPassiveMode(); 

     try { 
      result = ftpClient.storeFile(remoteFileName, buffIn); 
      Log.d(TAG, "Reply code: " + ftpClient.getReplyCode()); 
     } catch (IOException e) { 
      Log.d(TAG, "IOException: remote File could not be accessed"); 
     } 
     try { 
      buffIn.close(); 
     } catch (IOException e) { 
      Log.d(TAG, "IOException: buffIn.close()"); 
     } 

     return result; 
    } 

    public boolean connect(){ 
     boolean bool = false; 
     ftpClient = new FTPClient(); 

     try { 

      ftpClient.connect(this.serverAdress); 
      bool = ftpClient.login(this.userName, this.password); 
      if (!bool){ 
       Log.d(TAG, "Login Reply Code" + ftpClient.getReplyCode()); 
      } 
      ftpClient.changeWorkingDirectory(this.serverDirectory); 
      ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); 
     } catch (IOException e) { 
      Log.d(TAG, 
        "IOException ftp Client could not be established.controll Login, Server, Pw."); 
     } 
     Log.d(TAG, "FTP Server Response: " + ftpClient.getReplyString()); 

     return bool; 
    } 

    public void unregisterConnection(){ 
     try { 
      ftpClient.logout(); 
      ftpClient.disconnect(); 
     } catch (IOException e) { 
      Log.d(TAG, "IOException: ftpClient close/logout"); 
     } 
    } 
} 

もftpClient.getReplyString(ある)ftpClient.login、this.ftpClient後.changeWorkingDirectory(this.serverDirectory)、this.ftpClient.setFileType(2);

FTP Server Response: 200 Type set to I 

this.ftpClient.storeFile(remoteFileName、buffIn)の後の応答コード。

Reply code: 226 

FileZillaやTurbo FTP ClientのようなFtpクライアント経由でファイルをアップロードできます。 ここで私のラップトップで使用されているFileZillaのログを見ることができます。

2017-10-05 19:02:28 8160 1 Status: Resolving address of server.com 
2017-10-05 19:02:28 8160 1 Status: Connecting to serverIp... 
2017-10-05 19:02:28 8160 1 Status: Connection established, waiting for welcome message... 
2017-10-05 19:02:28 8160 1 Response: 220 FTP on server.com ready 
2017-10-05 19:02:28 8160 1 Command: AUTH TLS 
2017-10-05 19:02:28 8160 1 Response: 234 AUTH TLS successful 
2017-10-05 19:02:28 8160 1 Status: Initializing TLS... 
2017-10-05 19:02:28 8160 1 Status: Verifying certificate... 
2017-10-05 19:02:28 8160 1 Status: TLS connection established. 
2017-10-05 19:02:28 8160 1 Command: USER user 
2017-10-05 19:02:28 8160 1 Response: 331 Password required for user 
2017-10-05 19:02:28 8160 1 Command: PASS ************ 
2017-10-05 19:02:28 8160 1 Response: 230 User user logged in 
2017-10-05 19:02:28 8160 1 Command: OPTS UTF8 ON 
2017-10-05 19:02:28 8160 1 Response: 200 UTF8 set to on 
2017-10-05 19:02:28 8160 1 Command: PBSZ 0 
2017-10-05 19:02:28 8160 1 Response: 200 PBSZ 0 successful 
2017-10-05 19:02:28 8160 1 Command: PROT P 
2017-10-05 19:02:28 8160 1 Response: 200 Protection set to Private 
2017-10-05 19:02:28 8160 1 Status: Logged in 
2017-10-05 19:02:28 8160 1 Status: Retrieving directory listing... 
2017-10-05 19:02:28 8160 1 Command: PWD 
2017-10-05 19:02:28 8160 1 Response: 257 "/" is the current directory 
2017-10-05 19:02:28 8160 1 Command: TYPE I 
2017-10-05 19:02:28 8160 1 Response: 200 Type set to I 
2017-10-05 19:02:28 8160 1 Command: PASV 
2017-10-05 19:02:28 8160 1 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx). 
2017-10-05 19:02:28 8160 1 Command: MLSD 
2017-10-05 19:02:28 8160 1 Response: 150 Opening BINARY mode data connection for MLSD 
2017-10-05 19:02:29 8160 1 Response: 226 Transfer complete 
2017-10-05 19:02:29 8160 1 Status: Directory listing of "/" successful 
2017-10-05 19:02:36 8160 3 Status: Resolving address of server.com 
2017-10-05 19:02:36 8160 3 Status: Connecting to serverIp... 
2017-10-05 19:02:36 8160 3 Status: Connection established, waiting for welcome message... 
2017-10-05 19:02:36 8160 3 Response: 220 FTP on server.com ready 
2017-10-05 19:02:36 8160 3 Command: AUTH TLS 
2017-10-05 19:02:36 8160 3 Response: 234 AUTH TLS successful 
2017-10-05 19:02:36 8160 3 Status: Initializing TLS... 
2017-10-05 19:02:36 8160 3 Status: Verifying certificate... 
2017-10-05 19:02:36 8160 3 Status: TLS connection established. 
2017-10-05 19:02:36 8160 3 Command: USER user 
2017-10-05 19:02:36 8160 3 Response: 331 Password required for user 
2017-10-05 19:02:36 8160 3 Command: PASS ************ 
2017-10-05 19:02:36 8160 3 Response: 230 User user logged in 
2017-10-05 19:02:36 8160 3 Command: OPTS UTF8 ON 
2017-10-05 19:02:36 8160 3 Response: 200 UTF8 set to on 
2017-10-05 19:02:36 8160 3 Command: PBSZ 0 
2017-10-05 19:02:36 8160 3 Response: 200 PBSZ 0 successful 
2017-10-05 19:02:36 8160 3 Command: PROT P 
2017-10-05 19:02:36 8160 3 Response: 200 Protection set to Private 
2017-10-05 19:02:36 8160 3 Status: Logged in 
2017-10-05 19:02:36 8160 3 Status: Starting upload of C:\Users\User1\Pictures\test_sig.jpg 
2017-10-05 19:02:36 8160 3 Command: CWD /folder 
2017-10-05 19:02:36 8160 3 Response: 250 CWD command successful 
2017-10-05 19:02:36 8160 3 Command: PWD 
2017-10-05 19:02:36 8160 3 Response: 257 "/folder" is the current directory 
2017-10-05 19:02:36 8160 3 Command: TYPE I 
2017-10-05 19:02:36 8160 3 Response: 200 Type set to I 
2017-10-05 19:02:36 8160 3 Command: PASV 
2017-10-05 19:02:36 8160 3 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx). 
2017-10-05 19:02:36 8160 3 Command: STOR test_sig.jpg 
2017-10-05 19:02:37 8160 3 Response: 150 Opening BINARY mode data connection for test_sig.jpg 
2017-10-05 19:02:38 8160 3 Response: 226 Transfer complete 
2017-10-05 19:02:38 8160 3 Status: File transfer successful, transferred 1.728.438 bytes in 1 second 
2017-10-05 19:02:38 8160 3 Status: Retrieving directory listing of "/folder"... 
2017-10-05 19:02:38 8160 3 Command: PASV 
2017-10-05 19:02:38 8160 3 Response: 227 Entering Passive Mode (xx,xx,xx,xx,xx,xx). 
2017-10-05 19:02:38 8160 3 Command: MLSD 
2017-10-05 19:02:38 8160 3 Response: 150 Opening BINARY mode data connection for MLSD 
2017-10-05 19:02:38 8160 3 Response: 226 Transfer complete 
2017-10-05 19:02:38 8160 3 Status: Directory listing of "/folder" successful 
+0

[アンドロイドでFTPサーバー上のファイルをアップロードできません](https://stackoverflow.com/questions/11427346/unable-to-upload-file-on-ftp-server-in-android) –

+0

@ DimaKozhevinそれはこの質問と重複していません。 storeFile関数 – Fabian

+0

私たちに[mcve]を教えてください。 'remoteFileName'の実際の値を教えてください。 * "ファイルがアップロードされていない"ことを確認する方法を教えてください*。私たちにログファイルを表示してください。 –

答えて

0

私はこの問題を修正しました。それはFtpDataHandlerのコピーと過去の失敗でした。 this.serverDirectoryはserverAdressと同じです。そのため、Ftpクライアントはサーバー上のディレクトリを見つけることができませんでした。それは完全な解決策を見つけることが容易になりますように、私も、質問でそれを固定

public FtpDataHandler(String serverAdress, String userName, String password, String serverDiretory) { 
    this.serverAdress = serverAdress; 
    this.userName = userName; 
    this.password = password; 
    //this was my problem. I set the serverAdress to the serverDirectory insted the serverDirectory 
    //this.serverDirectory = serverAdress; 
    this.serverDirectory = serverDirectory; 

    connect(); 
} 

:ここ

は、コードsnippitです。

サポートのために@Martin Prikrylに感謝します。

関連する問題