Android的FTP拒绝连接到主机 [英] Android FTP refuses to connect to host

查看:148
本文介绍了Android的FTP拒绝连接到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立从用户捕获了一些数据项的应用程序,然后继续COM preSS这一切到一个zip文件,FTP到一台服务器。在COM pression部分可以作为一个魅力,我敢肯定,FTP会太,除了程序拒绝连接到主机。

我已经尝试了所有的排列组合来解决这一点,并一直无法实现的问题是什么。我已经包括了Apache公地网库,并已能够到错误针点的连接部分。大多数$ C $的c已注释以便允许使用仅一个部分。

我的code是为'包com.example.gis;

 进口java.io.FileInputStream中;进口org.apache.commons.net.ftp.FTPClient;
进口org.apache.commons.net.ftp.FT preply;进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.Toast;
公共类FTP延伸活动{公共静态FTPClient mFTPClient = NULL;
公共字符串标记=1234;
公共静态字符串一个;
公共静态字符串B:
//公共静态INT ABC = FTP.BINARY_FILE_TYPE;
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.ftp);捆绑额外= getIntent()getExtras()。
布尔M = ftpConnect(ftp://xxxxxxxxxx.xxx,省略了用户名,密码省略,21);
如果(M ==真)
{
    Toast.makeText(这一点,FTP登录,Toast.LENGTH_LONG).show();A = extras.getString(路径);
B = extras.getString(名称);
ftpUpload(一,B,/测试/);
ftpDisconnect();}
其他
    Toast.makeText(这一点,无法连接,Toast.LENGTH_LONG).show();
}公共布尔ftpConnect(主机字符串,字符串的用户名,
        字符串密码,诠释端口)
{
    mFTPClient =新FTPClient();尝试{//连接到主机
mFTPClient.connect(主机,端口);//现在检查的答复code,若为阳性平均连接成功
如果(FT preply.isPositiveCompletion(mFTPClient.getReply code())){
//使用用户名和登录功放;密码
布尔状态= mFTPClient.login(用户名,密码);
Toast.makeText(这一点,连接作为状态=+状态,Toast.LENGTH_LONG).show();
/ *设置文件传输方式
*
*为了避免损坏问题,您必须指定一个正确的
*传输模式,诸如ASCII_FILE_TYPE,BINARY_FILE_TYPE,
* EBCDIC_FILE_TYPE .etc。在这里,我用BINARY_FILE_TYPE
*用于传输文本,图像和COM pressed文件。
* /
mFTPClient.setFileType(ABC);mFTPClient.enterLocalPassiveMode();返回状态;
}
}赶上(例外五){
Log.d(TAG,错误:无法连接到主机+主机);
}返回false;
}公共布尔ftpDisconnect()
{
    尝试{
        mFTPClient.logout();
        mFTPClient.disconnect();
        返回true;
    }赶上(例外五){
        Log.d(TAG,而从FTP服务器断开连接时发生错误。);
    }    返回false;
}
/ **
 * mFTPClient:FTP客户端连接对象(请参阅FTP连接例)
 * srcFilePath:SD卡中的源文件路径
 要被存储在FTP服务器文件名:* desFileName
 * desDirectory:该文件应该被上传到目录路径
 * /
公共布尔ftpUpload(字符串srcFilePath,字符串desFileName,
                         串desDirectory)
{
    布尔状态= FALSE;
    尝试{
        的FileInputStream srcFileStream =新的FileInputStream(srcFilePath);        //工作目录更改到目标目录
        如果(ftpChangeDirectory(desDirectory)){
            状态= mFTPClient.storeFile(desFileName,srcFileStream);
        }        srcFileStream.close();
        返回状态;
    }赶上(例外五){
        Log.d(TAG上传失败);
    }    返回状态;
}
}

已经被扒我的头在这个相当一段时间了。


解决方案

 布尔值M = ftpConnect(ftp://xxxxxxxxxx.xxx,...

您指定使用FTP协议://。你不应该这样做。
你只需要像指定服务器(或主机)ftp.mycompany.com。

此外,如果你试图连接,那么你必须抓住SocketException,请的UnknownHostException
和IOException异常分开。通过打印堆栈跟踪,你会知道是什么
引起的误差。我看你是不是这样做的。

作为最后一句话我看到你这样做互联网存取权限的主线上。我知道这是可能的FTP(为什么FTP而不是HTTP?),但你可以更好地使用线程或AsyncTask的。

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. 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.

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. 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;

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://. You are not supposed to do that. You only have to specify the server (or host) like "ftp.mycompany.com".

Moreover if you try to connect then you have to catch SocketException, UnknownHostException and IOException separately. By printing the stacktrace you would know what caused the error. I see you are not doing that.

As a last remark I see that you do this internet acces on the main thread. 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.

这篇关于Android的FTP拒绝连接到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆