ftp
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

5 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "github.com/dutchcoders/goftp"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. )
  11. func FtpDemo() {
  12. var err error
  13. var ftp *goftp.FTP
  14. // For debug messages: goftp.ConnectDbg("ftp.server.com:21")
  15. if ftp, err = goftp.Connect("123.207.246.51:21"); err != nil {
  16. panic(err)
  17. }
  18. defer ftp.Close()
  19. // Username / password authentication
  20. if err = ftp.Login("go-build", "7Jp7PJkcnTHbcNtZ"); err != nil {
  21. panic(err)
  22. }
  23. file := strings.NewReader("hello worldxxxxx")
  24. if err := ftp.Stor("test.txt", file); err != nil {
  25. panic(err)
  26. }
  27. }
  28. // 工具-->
  29. //支持命令行,
  30. //json配置
  31. //yaml配置
  32. //跨平台编译
  33. //ftp上传
  34. // 不存在0 目录1 文件2
  35. func PathStat(path string) (int, error) {
  36. info, err := os.Stat(path)
  37. if os.IsNotExist(err) { // 如果不存在
  38. return 0, nil
  39. } else if err != nil {
  40. return -1, err
  41. } else if info.IsDir() {
  42. return 1, nil
  43. } else {
  44. return 2, nil
  45. }
  46. }
  47. func PathExist(path string) (bool, error) {
  48. _, err := os.Stat(path)
  49. if err == nil {
  50. return true, nil
  51. } else if os.IsNotExist(err) {
  52. return false, nil
  53. }
  54. return false, err
  55. }
  56. type Ftp struct {
  57. ftp *goftp.FTP
  58. Addr string `json:"addr"` // 地址
  59. Port string `json:"port"` //端口
  60. Username string `json:"username"` // 用户
  61. Password string `json:"password"` // 密码
  62. }
  63. func Connect(addr string, port string, username string, password string) (*Ftp, error) {
  64. var f *goftp.FTP
  65. var err error
  66. f, err = goftp.Connect(addr + ":" + port)
  67. if err != nil {
  68. return nil, err
  69. }
  70. // 登录
  71. err = f.Login(username, password)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &Ftp{
  76. ftp: f,
  77. Addr: addr,
  78. Port: port,
  79. Username: username,
  80. Password: password,
  81. }, nil
  82. }
  83. func (f *Ftp) Size(path string) (int, error) {
  84. return f.ftp.Size(path)
  85. }
  86. func (f *Ftp) Stat(path string) ([]string, error) {
  87. return f.ftp.Stat(path)
  88. }
  89. // 路径和名字
  90. func (f *Ftp) Send(path string, reader io.Reader) error {
  91. return f.ftp.Stor(path, reader)
  92. }
  93. func (f *Ftp) SendFile(path, name string) error {
  94. var file *os.File
  95. var err error
  96. file, err = os.Open(name)
  97. if err != nil {
  98. return err
  99. }
  100. return f.Send(path, file)
  101. }
  102. // 发送字符串
  103. func (f *Ftp) SendString(path, data string) error {
  104. return f.Send(path, strings.NewReader(data))
  105. }
  106. // 发送字节
  107. func (f *Ftp) SendBytes(path string, data []byte) error {
  108. return f.Send(path, bytes.NewReader(data))
  109. }
  110. // 接收文件
  111. func (f *Ftp) Receive(path string, fn func(io.Reader) error) (string, error) {
  112. return f.ftp.Retr(path, fn)
  113. }
  114. // 发送到某个文件
  115. func (f *Ftp) ReceiveToFile(path, file string) error {
  116. var err error
  117. // 完善file
  118. if file == "" {
  119. file = path
  120. } else if !strings.HasSuffix(file, filepath.Base(file)) {
  121. file = filepath.Join(file, filepath.Base(path))
  122. }
  123. stat, err := PathStat(filepath.Dir(file))
  124. if err != nil {
  125. return err
  126. }
  127. if stat == 0 {
  128. err = os.MkdirAll(filepath.Dir(file), 0666)
  129. if err != nil {
  130. return err
  131. }
  132. } else if stat == 2 {
  133. return errors.New("目录路径为文件")
  134. }
  135. _, err = f.Receive(path, func(reader io.Reader) error {
  136. var f1 *os.File
  137. _, err = os.Stat(file)
  138. if os.IsNotExist(err) {
  139. f1, err = os.Create(file)
  140. if err != nil {
  141. return err
  142. }
  143. } else if err != nil {
  144. return err
  145. } else {
  146. f1, err = os.Open(file)
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. _, err = io.Copy(f1, reader)
  152. return nil
  153. })
  154. return err
  155. }
  156. // 打印出来
  157. func (f *Ftp) ReceiveToPrint(path string) (string, error) {
  158. return f.Receive(path, func(reader io.Reader) error {
  159. var err error
  160. _, err = io.Copy(os.Stdout, reader)
  161. return err
  162. })
  163. }
  164. // 关闭
  165. func (f *Ftp) Close() error {
  166. return f.ftp.Quit()
  167. }
  168. // 上传
  169. func (f *Ftp) Upload(localPath string) error {
  170. return f.ftp.Upload(localPath)
  171. }
  172. func main() {
  173. }