GO FTP 简易服务端和客户端

服务端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)
    }
}

  

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 @   wsh3166Sir  阅读(1598)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示