• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

go语言实现简易ftp客户端

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiangxianghehe/article/details/78310249

Go语言实现的ftp库挺多的,我在这里尝试了一个简单的版本,地址https://github.com/dutchcoders/goftp
先安装依赖:

go get -u -v github.com/dutchcoders/goftp
  • 1

然后配置好ftp服务器,编译执行以下代码,代码包括列出列表和上传功能:

package main

import (
    "github.com/dutchcoders/goftp"
    "fmt"
    "os"
)

func main() {
    var err error
    var ftp *goftp.FTP

    // For debug messages: goftp.ConnectDbg("ftp.server.com:21")
    if ftp, err = goftp.Connect("server_ip:21"); err != nil {
        panic(err)
    }

    defer ftp.Close()
    fmt.Println("Successfully connected !!")


    // Username / password authentication
    if err = ftp.Login("user", "pass"); err != nil {
        panic(err)
    }

    if err = ftp.Cwd("/home/ftp"); err != nil {
        panic(err)
    }

    var curpath string
    if curpath, err = ftp.Pwd(); err != nil {
        panic(err)
    }

    fmt.Printf("Current path: %s", curpath)

    // Get directory listing
    var files []string
    if files, err = ftp.List(""); err != nil {
        panic(err)
    }
    fmt.Println("Directory listing:/n", files)


    // Upload a file
    var file *os.File
    if file, err = os.Open("E://6楼花名册.xlsx"); err != nil {
        panic(err)
    }

    if err := ftp.Stor("/home/ftp/6楼花名册.xlsx", file); err != nil {
        panic(err)
    }

}

 

上传文件代码示例如下:

// Package goftp upload helper
package goftp

import (
    "os"
    "path/filepath"
)

func (ftp *FTP) copyDir(localPath string) error {
    fullPath, err := filepath.Abs(localPath)
    if err != nil {
        return err
    }

    pwd, err := ftp.Pwd()
    if err != nil {
        return err
    }

    walkFunc := func(path string, fi os.FileInfo, err error) error {
        // Stop upon error
        if err != nil {
            return err
        }
        relPath, err := filepath.Rel(fullPath, path)
        if err != nil {
            return err
        }
        switch {
        case fi.IsDir():
            // Walk calls walkFn on root as well
            if path == fullPath {
                return nil
            }
            if err = ftp.Mkd(relPath); err != nil {
                if _, err = ftp.List(relPath + "/"); err != nil {
                    return err
                }
            }
        case fi.Mode()&os.ModeSymlink == os.ModeSymlink:
            fInfo, err := os.Stat(path)
            if err != nil {
                return err
            }
            if fInfo.IsDir() {
                err = ftp.Mkd(relPath)
                return err
            } else if fInfo.Mode()&os.ModeType != 0 {
                // ignore other special files
                return nil
            }
            fallthrough
        case fi.Mode()&os.ModeType == 0:
            if err = ftp.copyFile(path, pwd+"/"+relPath); err != nil {
                return err
            }
        default:
            // Ignore other special files
        }

        return nil
    }

    return filepath.Walk(fullPath, walkFunc)
}

func (ftp *FTP) copyFile(localPath, serverPath string) (err error) {
    var file *os.File
    if file, err = os.Open(localPath); err != nil {
        return err
    }
    defer file.Close()
    if err := ftp.Stor(serverPath, file); err != nil {
        return err
    }

    return nil
}

// Upload a file, or recursively upload a directory.
// Only normal files and directories are uploaded.
// Symlinks are not kept but treated as normal files/directories if targets are so.
func (ftp *FTP) Upload(localPath string) (err error) {
    fInfo, err := os.Stat(localPath)
    if err != nil {
        return err
    }

    switch {
    case fInfo.IsDir():
        return ftp.copyDir(localPath)
    case fInfo.Mode()&os.ModeType == 0:
        return ftp.copyFile(localPath, filepath.Base(localPath))
    default:
        // Ignore other special files
    }

    return nil
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
设计模式学习-使用go实现桥接模式发布时间:2022-07-10
下一篇:
Qmgo开源了!更好用的Go语言MongoDBdriver发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap