简体   繁体   English

Android FTP拒绝连接到主机

[英]Android FTP refuses to connect to host

I have been trying to build an application which captures a few data items from the user and then proceeds to compress it all into a zip file and FTP over to a server. 我一直在尝试构建一个应用程序,该应用程序从用户那里捕获一些数据项,然后将其全部压缩到一个zip文件中,并通过FTP传输到服务器。 The compression part works as a charm and i am sure that the FTP would too, except that the program refuses to connect to the host. 压缩部分起到了吸引人的作用,我确信FTP也会这样做,但该程序拒绝连接到主机。

I have tried all permutations and combinations to solving this and have been unable to realise what the problem is. 我已经尝试了所有排列和组合来解决此问题,但无法意识到问题所在。 I have included the apache commons net libraries and have been able to pin point the error to the connection part. 我已经包括了Apache Commons Net库,并且能够将错误定位到连接部分。 Most of the code has been commented out so as to allow only the one part to be used. 大多数代码已被注释掉,以便仅使用其中一部分。

My code is as `package com.example.gis; 我的代码是`package com.example.gis;

import java.io.FileInputStream;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


public class FTP extends Activity {

public static FTPClient mFTPClient = null;
public String TAG="1234";
public static String a;
public static String b;
//public static int abc=FTP.BINARY_FILE_TYPE; 
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ftp);

Bundle extras = getIntent().getExtras();
boolean m=ftpConnect("ftp://xxxxxxxxxx.xxx", "username omitted", "password omitted", 21);
if(m==true)
{
    Toast.makeText(this,"FTP Login" , Toast.LENGTH_LONG).show();

a=extras.getString("path");
b=extras.getString("name");
ftpUpload("a", "b", "/test/");
ftpDisconnect();

}
else
    Toast.makeText(this,"Failure to connect" , Toast.LENGTH_LONG).show();
}



public boolean ftpConnect(String host, String username,
        String password, int port)
{
    mFTPClient=new FTPClient();

try {

// connecting to the host
mFTPClient.connect(host, port);

// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
// login using username & password
boolean status = mFTPClient.login(username, password);
Toast.makeText(this,"Connected as status ="+status, Toast.LENGTH_LONG).show();
/* Set File Transfer Mode
*
* To avoid corruption issue you must specified a correct
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
* for transferring text, image, and compressed files.
*/
mFTPClient.setFileType(abc);

mFTPClient.enterLocalPassiveMode();

return status;
}
} catch(Exception e) {
Log.d(TAG, "Error: could not connect to host " + host );
}

return false;
}

public boolean ftpDisconnect()
{
    try {
        mFTPClient.logout();
        mFTPClient.disconnect();
        return true;
    } catch (Exception e) {
        Log.d(TAG, "Error occurred while disconnecting from ftp server.");
    }

    return false;
}


/**
 * mFTPClient: FTP client connection object (see FTP connection example)
 * srcFilePath: source file path in sdcard
 * desFileName: file name to be stored in FTP server
 * desDirectory: directory path where the file should be upload to
 */
public boolean ftpUpload(String srcFilePath, String desFileName,
                         String desDirectory)
{
    boolean status = false;
    try {
        FileInputStream srcFileStream = new FileInputStream(srcFilePath);

        // change working directory to the destination directory
        if (ftpChangeDirectory(desDirectory)) {
            status = mFTPClient.storeFile(desFileName, srcFileStream);
        }

        srcFileStream.close();
        return status;
    } catch (Exception e) {
        Log.d(TAG, "upload failed");
    }

    return status;
}


}

Have been raking my head over this for quite sometime now. 我已经为此花了很长时间了。

boolean m=ftpConnect("ftp://xxxxxxxxxx.xxx", ...

You are specifying a protocol with ftp://. 您正在使用ftp://指定协议。 You are not supposed to do that. 您不应该这样做。 You only have to specify the server (or host) like "ftp.mycompany.com". 您只需指定服务器(或主机),例如“ ftp.mycompany.com”。

Moreover if you try to connect then you have to catch SocketException, UnknownHostException and IOException separately. 此外,如果尝试连接,则必须分别捕获SocketException,UnknownHostException和IOException。 By printing the stacktrace you would know what caused the error. 通过打印stacktrace,您将知道导致错误的原因。 I see you are not doing that. 我看到你没有这样做。

As a last remark I see that you do this internet acces on the main thread. 最后,我看到您在主线程上执行此Internet访问。 I know that that is possible for ftp ( why for ftp and not for http?) but you could better use a thread or an asynctask. 我知道这对于ftp是可行的(为什么是ftp而不是http?),但是最好使用线程或asynctask。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM