日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go语言实现简易ftp客户端

發(fā)布時間:2025/7/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go语言实现简易ftp客户端 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 https://blog.csdn.net/xiangxianghehe/article/details/78310249

Go語言實現(xiàn)的ftp庫挺多的,我在這里嘗試了一個簡單的版本,地址https://github.com/dutchcoders/goftp。
先安裝依賴:

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

然后配置好ftp服務(wù)器,編譯執(zhí)行以下代碼,代碼包括列出列表和上傳功能:

package mainimport ("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 goftpimport ("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 }

轉(zhuǎn)載于:https://www.cnblogs.com/lvdongjie/p/9554849.html

總結(jié)

以上是生活随笔為你收集整理的go语言实现简易ftp客户端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。