【问题标题】:List FTP files with goftp使用 goftp 列出 FTP 文件
【发布时间】:2021-05-28 13:11:43
【问题描述】:

我正在尝试编写一个简单的 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", ret_file, " completed")
    }
}

由于某种原因,ReadDir 函数出现错误。 我需要获取文件名以便下载它们。

【问题讨论】:

  • 你得到什么错误?

标签: go ftp ftp-client


【解决方案1】:

ReadDir() 返回错误时,您正尝试循环访问files。这永远不会奏效,因为任何时候返回错误files 都是nil

这是非常标准的行为,可以通过阅读 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

【讨论】:

  • 非常感谢您的回复,我已经更新了我的代码,它看起来可以与您提到的 FTP 服务器一起使用。我的问题是我使用的是带有内置 FTP 服务器的 BROTHER 打印机,当我使用我的打印机值运行代码时,我收到 I/O 超时错误
  • 恐慌:错误读取响应:读取 tcp 192.168.1.1:45312->192.168.1.2:21: i/o 超时
  • FTP 是一个挑剔的协议。您可能想尝试启用the DisableEPSV option within a custom Config,然后使用DialConfig()。此外,在您的代码 sn-p 中,您声明 this section works , I am setting a file name and I can pull it。现在不是这样了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 2013-09-29
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多