草庐IT

java - Android 上的 FTP 上传和下载

coder 2023-12-11 原文

我在设置 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;
}    

最佳答案

        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 - Android 上的 FTP 上传和下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6751159/

有关java - Android 上的 FTP 上传和下载的更多相关文章

  1. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  2. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  3. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  8. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  9. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  10. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

随机推荐