使用 goftp 列出 FTP 文件

如何解决使用 goftp 列出 FTP 文件

我正在尝试编写一个简单的 Go 程序,该程序连接到 FTP 服务器,列出指定目录中的文件并拉取它们。

代码是这样的:

package main

import (
    "bytes"
    "fmt"
    "github.com/secsy/goftp"
    "io/IoUtil"
    "log"
    "os"
    "path"
    "time"
)

func main() {
    config := goftp.Config{
        User:               "anonymous",Password:           "root@local.me",ConnectionsPerHost: 21,Timeout:            10 * time.Second,Logger:             os.Stderr,}

    // Connecting to the server
    client,dailErr := goftp.DialConfig(config,"ftp.example.com")

    if dailErr != nil {
        log.Fatal(dailErr)
        panic(dailErr)
    }
    // setting the search directory
    dir := "/downloads/"
    files,err := client.ReadDir(dir)

    if err != nil {
        for _,file := range files {
            if file.IsDir() {
                path.Join(dir,file.Name())
            } else {
                fmt.Println("the file is %s",file.Name())
            }
        }
    }
    // this section works,I am setting a file name and I can pull it
    // if I mark the search part
    ret_file := "example.PDF"

    fmt.Println("Retrieving file: ",ret_file)
    buf := new(bytes.Buffer)
    fullPathFile := dir + ret_file
    rferr := client.Retrieve(fullPathFile,buf)

    if rferr != nil {
        panic(rferr)
    }

    fmt.Println("writing data to file",ret_file)

    fmt.Println("opening file",ret_file,"for writing")
    w,_ := IoUtil.ReadAll(buf)
    ferr := IoUtil.WriteFile(ret_file,w,0644)

    if ferr != nil {
        log.Fatal(ferr)
        panic(ferr)
    } else {
        fmt.Println("Writing"," completed")
    }
}

出于某种原因,我在 ReadDir 函数上遇到错误。 我需要获取文件名以便下载。

解决方法

files 返回错误时,您正在尝试遍历 ReadDir()。这永远不会奏效,因为任何时候都会返回错误 filesnil

这是非常标准的行为,可以通过阅读 the implementation of ReadDir() 来确认。

我猜您可能已经使用 the example from the project used to demonstrate ReadDir() 作为起点。在示例中,涉及错误处理,因为它决定是否继续遍历目录树。但是,请注意 when ReadDir() returns an error that doesn't result in stopping the program,the subsequent for loop is a no-op,因为 filesnil

下面是一个小程序,它演示了如何以简单的方式成功使用 Readdir() 的结果:

package main

import (
    "fmt"
    "github.com/secsy/goftp"
)

const (
    ftpServerURL = "ftp.us.debian.org"
    ftpServerPath = "/debian/"
)

func main() {
    client,err := goftp.Dial(ftpServerURL)
    if err != nil {
        panic(err)
    }
    files,err := client.ReadDir(ftpServerPath)
    if err != nil {
        panic(err)
    }
    for _,file := range files {
        fmt.Println(file.Name())
    }
}

它输出(与 http://ftp.us.debian.org/debian/ 处的当前列表匹配):

$ go run goftp-test.go
README
README.CD-manufacture
README.html
README.mirrors.html
README.mirrors.txt
dists
doc
extrafiles
indices
ls-lR.gz
pool
project
tools
zzz-dists

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?