You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
3.7 KiB
201 lines
3.7 KiB
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"github.com/dutchcoders/goftp"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func FtpDemo() {
|
|
var err error
|
|
var ftp *goftp.FTP
|
|
|
|
// For debug messages: goftp.ConnectDbg("ftp.server.com:21")
|
|
if ftp, err = goftp.Connect("123.207.246.51:21"); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer ftp.Close()
|
|
|
|
// Username / password authentication
|
|
if err = ftp.Login("go-build", "7Jp7PJkcnTHbcNtZ"); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
file := strings.NewReader("hello worldxxxxx")
|
|
if err := ftp.Stor("test.txt", file); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// 工具-->
|
|
//支持命令行,
|
|
//json配置
|
|
//yaml配置
|
|
//跨平台编译
|
|
//ftp上传
|
|
|
|
// 不存在0 目录1 文件2
|
|
func PathStat(path string) (int, error) {
|
|
info, err := os.Stat(path)
|
|
if os.IsNotExist(err) { // 如果不存在
|
|
return 0, nil
|
|
} else if err != nil {
|
|
return -1, err
|
|
} else if info.IsDir() {
|
|
return 1, nil
|
|
} else {
|
|
return 2, nil
|
|
}
|
|
}
|
|
|
|
func PathExist(path string) (bool, error) {
|
|
_, err := os.Stat(path)
|
|
if err == nil {
|
|
return true, nil
|
|
} else if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
type Ftp struct {
|
|
ftp *goftp.FTP
|
|
Addr string `json:"addr"` // 地址
|
|
Port string `json:"port"` //端口
|
|
Username string `json:"username"` // 用户
|
|
Password string `json:"password"` // 密码
|
|
}
|
|
|
|
func Connect(addr string, port string, username string, password string) (*Ftp, error) {
|
|
var f *goftp.FTP
|
|
var err error
|
|
f, err = goftp.Connect(addr + ":" + port)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 登录
|
|
err = f.Login(username, password)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Ftp{
|
|
ftp: f,
|
|
Addr: addr,
|
|
Port: port,
|
|
Username: username,
|
|
Password: password,
|
|
}, nil
|
|
}
|
|
|
|
func (f *Ftp) Size(path string) (int, error) {
|
|
return f.ftp.Size(path)
|
|
}
|
|
|
|
func (f *Ftp) Stat(path string) ([]string, error) {
|
|
return f.ftp.Stat(path)
|
|
}
|
|
|
|
// 路径和名字
|
|
func (f *Ftp) Send(path string, reader io.Reader) error {
|
|
return f.ftp.Stor(path, reader)
|
|
}
|
|
|
|
func (f *Ftp) SendFile(path, name string) error {
|
|
var file *os.File
|
|
var err error
|
|
file, err = os.Open(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return f.Send(path, file)
|
|
}
|
|
|
|
// 发送字符串
|
|
func (f *Ftp) SendString(path, data string) error {
|
|
return f.Send(path, strings.NewReader(data))
|
|
}
|
|
|
|
// 发送字节
|
|
func (f *Ftp) SendBytes(path string, data []byte) error {
|
|
return f.Send(path, bytes.NewReader(data))
|
|
}
|
|
|
|
// 接收文件
|
|
func (f *Ftp) Receive(path string, fn func(io.Reader) error) (string, error) {
|
|
return f.ftp.Retr(path, fn)
|
|
}
|
|
|
|
// 发送到某个文件
|
|
func (f *Ftp) ReceiveToFile(path, file string) error {
|
|
var err error
|
|
// 完善file
|
|
if file == "" {
|
|
file = path
|
|
} else if !strings.HasSuffix(file, filepath.Base(file)) {
|
|
file = filepath.Join(file, filepath.Base(path))
|
|
}
|
|
stat, err := PathStat(filepath.Dir(file))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stat == 0 {
|
|
err = os.MkdirAll(filepath.Dir(file), 0666)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if stat == 2 {
|
|
return errors.New("目录路径为文件")
|
|
}
|
|
|
|
_, err = f.Receive(path, func(reader io.Reader) error {
|
|
var f1 *os.File
|
|
_, err = os.Stat(file)
|
|
if os.IsNotExist(err) {
|
|
f1, err = os.Create(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
} else {
|
|
f1, err = os.Open(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
_, err = io.Copy(f1, reader)
|
|
return nil
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
// 打印出来
|
|
func (f *Ftp) ReceiveToPrint(path string) (string, error) {
|
|
return f.Receive(path, func(reader io.Reader) error {
|
|
var err error
|
|
_, err = io.Copy(os.Stdout, reader)
|
|
return err
|
|
})
|
|
}
|
|
|
|
// 关闭
|
|
func (f *Ftp) Close() error {
|
|
return f.ftp.Quit()
|
|
}
|
|
|
|
// 上传
|
|
func (f *Ftp) Upload(localPath string) error {
|
|
return f.ftp.Upload(localPath)
|
|
}
|
|
|
|
func main() {
|
|
|
|
}
|
|
|