【问题标题】:FTP Upload and Download on AndroidAndroid 上的 FTP 上传和下载
【发布时间】:2011-10-08 17:35:19
【问题描述】:

我在设置了 INTERNET_ACCESS 等后收到此错误...

 private class AsyncUpload extends AsyncTask<String, Void, Void>{       
    public void ftpUpload(){
        FTPClient con = new FTPClient();
        try
        {

            con.connect("ftp.194.90.81.149"); //here i recieve exception
                //the exception is java.unknownhostexception
                //java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname
            if (con.login("username", "password"))
            {
                con.enterLocalPassiveMode(); 
                String data = "test data";
                ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
                boolean result = con.storeFile("/test.jpg", in);
                in.close();
                if (result) Log.v("upload result", "succeeded");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }


        try
        {
            con.logout();
            con.disconnect();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(String... params) {
        ftpUpload();
        return null;
    }
}

这是我测试过但仍然收到异常的另一部分代码

public class FTPFactory {
private FTPClient _ftpClient = null;

public boolean Connect(String host, String userName, String password, int port) throws IOException
{
    try {

        _ftpClient = new FTPClient();   

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

        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(_ftpClient.getReplyCode())) {             
            // login using username & password
            boolean status = _ftpClient.login(userName, password);
            return status;
        }

    } catch(IOException e) {
        throw e;
    }       
    return false;
}

public boolean Disconnect()
{
    try {
        _ftpClient.logout();
        _ftpClient.disconnect();
        return true;
    } catch (Exception e) {

    }

    return false;
}

public boolean ChangeDirectory(String directoryPath)
{
    try {
        _ftpClient.changeWorkingDirectory(directoryPath);
    } catch(Exception e) {

    }

    return false;
}


public String GetCurrentWorkingDirectory()
{
    try {
        String workingDir = _ftpClient.printWorkingDirectory();
        return workingDir;
    } catch(Exception e) {

    }

    return null;
}

public void PrintFilesList(String dirPath)
{
    try {
        FTPFile[] ftpFiles = _ftpClient.listFiles(dirPath);
        int length = ftpFiles.length;

        for (int i = 0; i < length; i++) {
            String name = ftpFiles[i].getName();
            boolean isFile = ftpFiles[i].isFile();

            if (isFile) {

            }
            else {

            }
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public boolean MakeDirectory(String newDirPath)
{
    try {
        boolean status = _ftpClient.makeDirectory(newDirPath);
        return status;
    } catch(Exception e) {

    }
    return false;
}


public boolean RemoveDirectory(String dirPath)
{
    try {
        boolean status = _ftpClient.removeDirectory(dirPath);
        return status;
    } catch(Exception e) {

    }
    return false;
}

public boolean RemoveFile(String filePath)
{
    try {
        boolean status = _ftpClient.deleteFile(filePath);
        return status;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

public boolean RenameFile(String from, String to)
{
    try {
        boolean status = _ftpClient.rename(from, to);
        return status;
    } catch (Exception e) {

    }
    return false;
}


/**
 * mFTPClient: FTP client connection object (see FTP connection example)
 * srcFilePath: path to the source file in FTP server
 * desFilePath: path to the destination file to be saved in sdcard
 */
public boolean Download(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
        status = _ftpClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {

    }

    return status;
}

/**
 * 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 Upload(String srcFilePath, String desFileName, String desDirectory)
{
    boolean status = false;
    try {
        FileInputStream srcFileStream = new FileInputStream(srcFilePath);

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

        srcFileStream.close();
        return status;
    } catch (Exception e) {

    }

    return status;
}    

}

【问题讨论】:

  • 我认为FTP URL格式不正确,检查一次
  • 您应该发布问题的堆栈跟踪。但似乎你给你的 FtpClient 一个错误的连接字符串。
  • 我已经测试了所有可能的变体,你能帮帮我吗?

标签: java android ftp


【解决方案1】:
        con.connect("ftp.194.90.81.149"); //here i recieve exception
            //the exception is java.unknownhostexception
            //java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname

您收到 UnknownHostException 的事实意味着 ftp.194.90.81.149 不是 DNS 中的真实主机名。我怀疑其中的数字部分是您真正想要的。即,尝试将该行更改为

con.connect("194.90.81.149");

【讨论】:

  • 我已经测试了这个变种,但仍然没有结果。我怎么知道我做的每一件事是否正确?
  • 哦,唐首先非常感谢您的帮助,关于我收到 java.net 的错误... .UnkownHostException .... 也许我会将应用程序发送给您,您可以看看我做错了什么?
【解决方案2】:

未知主机异常意味着您缺少 Internet 权限或未连接到 Internet 或者您的 ftp url 错误。

【讨论】:

  • 我必须从 ftp 位置获取一些视频,我的链接看起来像“ftp:///xx.xx.xx”而不是 ftp.xx.xx。
  • 我指的是IP 就是这样。是的。如上所示,我的问题已解决。
猜你喜欢
  • 2013-10-08
  • 2021-12-08
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多