GO FTP 简易服务端和客户端

服务端:

package main

import (
	"log"

	"goftp.io/server/core"
	"goftp.io/server/driver/file"
)

func main() {
	Name := "FTP Server"
	rootPath := "./static" //FTP根目录
	Port := 2121           //FTP 端口
	var perm = core.NewSimplePerm("test", "test")

	// Server options without hostname or port
	opt := &core.ServerOpts{
		Name: Name,
		Factory: &file.DriverFactory{
			RootPath: rootPath,
			Perm:     perm,
		},
		Auth: &core.SimpleAuth{
			Name:     "username",       // FTP 账号
			Password: "Password", // FTP 密码
		},
		Port: Port,
	}
	// start ftp server
	s := core.NewServer(opt)
	err := s.ListenAndServe()
	if err != nil {
		log.Fatal("Error starting server:", err)
	}
}

  

 

客户端:  三秒传一次,传成功为止

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/jlaffaye/ftp"
)

func main() {
	// Do something with the FTP conn

	for {
		newFunction()
		time.Sleep(3 * time.Second)
	}
}

func newFunction() {
	file, err := os.Open("data.dmp")
	if err != nil {
		log.Println("读取文件:")
		log.Println(err)
		return
	}

	c, err := ftp.Dial("127.0.0.1:2121", ftp.DialWithTimeout(5*time.Second))
	//c, err := ftp.Dial("172.16.33.194:2121", ftp.DialWithTimeout(5*time.Second))
	if err != nil {
		log.Println("登录0:")
		log.Println(err)
		return
	}

	err = c.Login("username", "password")
	if err != nil {
		log.Println("登录1:")
		log.Println(err)
		return
	}

	if err := c.Stor(`data.dmp`, file); err != nil {
		log.Println("上传:")
		log.Println(err)
		return
	} else {
		log.Println("上传成功")
		os.Exit(0)
	}
 
}

  

 

posted @ 2022-05-11 08:35  wsh3166Sir  阅读(1545)  评论(0编辑  收藏  举报