试图让我的Andr​​oid应用程序放在我的FTP服务器上的文件我的、放在、应用程序、器上

2023-09-06 07:22:17 作者:孤独,是一种态度

我花了很多时间试图让这个code的工作,并在其目前的状态几乎完全反映了code,我发现,在Apache FtpClient的不能下载较大的文件。我也使用Apache的公共网络。 由于某种原因,我的应用程序一直无法完成client.connect一步,甚至当我插上FTP服务器应该工作的其他IP地址。

I have spent a lot of time trying to make this code work, and in its current state it almost exactly mirrors the code I found at Apache FTPClient failing to download larger files. I am also using apache commons net. for some reason my application never gets past the client.connect step, even when i plug in other IP addresses of ftp servers that should work.

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

在此先感谢!

Thanks in advance!

推荐答案

请尝试使用这样的:

client.connect(InetAddress.getByAddress(ftpAddress,new byte[]{127,0,0,1}));