Ant FTP无法下载子目录中的文件。

3

我正在尝试使用Ant从FTP服务器下载子目录中的文件。 已知确切的文件集。 其中一些文件位于子目录中。 Ant似乎只下载根目录中的文件。 如果不列出文件而下载所有文件,则可以正常工作。

第一个FTP操作应该与第二个操作完全相同。 但是我得到了“假定隐藏文件\\a\a.txt不是符号链接”的错误信息。

有人知道这里出了什么问题吗? 这是Ant FTP任务中的错误吗?

<?xml version="1.0" encoding="utf-8"?>
<project name="example" default="example" basedir=".">
    <taskdef name="ftp" 
    classname="org.apache.tools.ant.taskdefs.optional.net.FTP" />

    <target name="example">

        <!-- doesn't work -->
        <ftp action="get" verbose="true"
        server="localhost" userid="example" password="example" 
        remotedir="">
            <fileset dir="downloads" casesensitive="false" 
            includes="a/a.txt,a/b/ab.txt,c/c.txt" />
        </ftp>

        <!-- works (but requires multiple ftp tasks) -->
        <ftp action="get" verbose="true"
        server="localhost" userid="example" password="example"
        remotedir="a">
            <fileset dir="downloads" casesensitive="false" 
            includes="a.txt,b/ab.txt" />
        </ftp>
        <ftp action="get" verbose="true"
        server="localhost" userid="example" password="example"
        remotedir="c">
            <fileset dir="downloads" casesensitive="false" 
            includes="c.txt" />
        </ftp>

    </target>

</project>

更新:我在Commons Net jira上发布了一个关于此问题的错误报告。链接如下:https://issues.apache.org/jira/browse/NET-324 更新:我在ant bugreport系统中添加了一个错误报告。链接如下:https://issues.apache.org/bugzilla/show_bug.cgi?id=49296
6个回答

1

由于我需要它工作,所以我做了这个解决方法。 它似乎可以工作,但我并不完全满意。 它感觉不够简洁。

<scriptdef name="my-ftp-get" language="javascript">
    <attribute name="server"/>
    <attribute name="userid"/>
    <attribute name="password"/>
    <attribute name="remotedir"/>
    <attribute name="fileset_dir"/>
    <attribute name="fileset_includes"/>
    <![CDATA[
    importClass(java.io.File);
    importClass(org.apache.tools.ant.taskdefs.optional.net.FTP);
    var local_basedir = "" + attributes.get("fileset_dir") + "/";
    var original_includes = "" + attributes.get("fileset_includes");
    var remotedir = "" + attributes.get("remotedir");
    local_basedir = local_basedir.replace(/\\/g, "/");
    original_includes = original_includes.replace(/\\/g, "/");
    remotedir = remotedir.replace(/\\/g, "/");
    var includes_arr = original_includes.split(",");
    var clean_includes = {};
    for (var i = 0; i < includes_arr.length; i++) {
        var directory = "/";
        var filename = includes_arr[i];
        var split_include = includes_arr[i].split("/");
        if (split_include.length > 1) {
            directory = split_include[0] + "/";
            filename = includes_arr[i].substring(directory.length);
        }
        if (!clean_includes.hasOwnProperty(directory)) {
            clean_includes[directory] = [];
        }
        clean_includes[directory].push(filename);
    }
    var get_files = new FTP.Action();
    get_files.setValue("get");
    for (var path in clean_includes) {
        var current_clean_includes = clean_includes[path].join(",");
        var fileset = project.createDataType("fileset");
        var ftp = self.project.createTask("ftp");
        ftp.setAction(get_files);
        ftp.setServer(attributes.get("server"));
        ftp.setUserid(attributes.get("userid"));
        ftp.setPassword(attributes.get("password"));
        ftp.setRemotedir(remotedir + path);
        fileset.setDir(new File(local_basedir + path));
        fileset.setIncludes(current_clean_includes);
        ftp.addFileset(fileset);
        ftp.perform();
    }
    ]]>
</scriptdef>

<my-ftp-get
server="localhost" userid="example" password="example"
remotedir=""
    fileset_dir="downloads" casesensitive="false" 
    fileset_includes="a/a.txt,a/b/ab.txt">
</my-ftp-get>

1

我相信已经找到并解决了问题。 同时请查看我在Jira中的错误报告:https://issues.apache.org/jira/browse/NET-324

我还向Ant错误报告系统添加了一个错误报告https://issues.apache.org/bugzilla/show_bug.cgi?id=49296

diff --git a/libs/apache-ant-1.8.1/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java b/libs/apache-ant-1.8.1/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java
--- a/libs/apache-ant-1.8.1/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java
+++ b/libs/apache-ant-1.8.1/src/main/org/apache/tools/ant/taskdefs/optional/net/FTP.java
@@ -834,8 +834,7 @@
                         } else if (!result) {
                             return;
                         }
-                        this.curpwd = this.curpwd + remoteFileSep
-                            + currentPathElement;
+                        this.curpwd = getCurpwdPlusFileSep() + currentPathElement;
                     } catch (IOException ioe) {
                         throw new BuildException("could not change working dir to "
                                                  + (String) pathElements.elementAt(fcount)
@@ -895,7 +894,7 @@
              * @return absolute path as string
              */
             public String getAbsolutePath() {
-                return curpwd + remoteFileSep + ftpFile.getName();
+                return getCurpwdPlusFileSep() + ftpFile.getName();
             }
             /**
              * find out the relative path assuming that the path used to construct
@@ -1036,6 +1035,17 @@
             public String getCurpwd() {
                 return curpwd;
             }
+
+            /**
+             * @return parent directory of the AntFTPFile with a remoteFileSep appended
+             */
+            private String getCurpwdPlusFileSep() {
+                if (this.curpwd.endsWith(remoteFileSep)) {
+                    return this.curpwd;
+                }
+                return this.curpwd + remoteFileSep;
+            }
+            
             /**
              * find out if a symbolic link is encountered in the relative path of this file
              * from rootPath.

0

你可能会在Apache Commons VFS Ant Tasks中获得更多成功。这些任务提供了对各种文件系统的读写、复制、同步和其他操作。如果以后需要改用sftp、ssh、http或其他系统,这也将是一个灵活的解决方案;与ftp、ssh等原生ant任务不同,大部分VFS配置与文件系统无关,只需要改变URL即可。

你的示例代码可以按照以下方式编写:

<taskdef resource="org/apache/commons/vfs/tasks/tasks.properties"/>

<target name="example">
     <v-copy destdir="${basedir}" srcdir="ftp://example:example@localhost/downloads" includes="a/a.txt,a/b/ab.txt,c/c.txt"/>
</target>

0

你是否尝试将'/'设置为remotedir的值?

<ftp action="get" verbose="true"
   server="localhost" userid="example" password="example" 
   remotedir="/">
  <fileset dir="downloads" casesensitive="false" 
    includes="a/a.txt" />
</ftp>

0

我不知道设置remotedir=""是否合法。如果您想使用默认的根目录,则应完全省略它。


0
第一个案例将尝试获取文件//downloads/a/a.txt 第二个案例将尝试获取文件//a/downloads/a.txt
如果您用不起作用的那个替换它。
<ftp action="get" verbose="true"
    server="localhost" userid="example" password="example" 
    remotedir="">
        <fileset dir="a/downloads" casesensitive="false" 
        includes="a.txt" />
</ftp>

或者

<ftp action="get" verbose="true"
    server="localhost" userid="example" password="example" 
    remotedir="">
        <fileset dir="a" casesensitive="false" 
        includes="downloads/a.txt" />
</ftp>

它应该能更好地工作。如果这些不符合您的需求,您可以提供您想匹配的文件的完整路径,也许我们可以创建一些替代方案。


不,你混淆了remoteDir和fileset dir属性。引用手册上的话:“从FTP服务器获取文件的方式与发送文件的方式基本相同。唯一的区别在于,嵌套的文件集使用remotedir属性作为FTP服务器上文件的基目录,dir属性作为放置文件的本地目录。” - Kristof Neirynck
我想下载根目录下的几个文件以及不同子目录中的多个文件。举个例子:root.txt,a/a.txt,a/b/ab.txt,c/c.txt。 - Kristof Neirynck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接