登录
首页 >  Golang >  Go教程

golang实现ftp实时传输文件的案例

来源:脚本之家

时间:2023-01-01 18:28:47 411浏览 收藏

本篇文章给大家分享《golang实现ftp实时传输文件的案例》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

一、项目简介

本项目主要实现的功能是ftp客户端不断地将xml文件和jpg文件实时地上传到服务器,当然也可以是其他格式的文件。每当ftp客户端取到一个文件之后,将文件上传到服务器后,然后将其删除。

项目实现可配置,如果开发者有类似的需求,只需要修改配置文件就可以使用本项目去完成上传文件的功能。

本项目打日志是按照当天时间来生成日志文件,每天每一种类型的日志只打一个文件。

二、项目结构图片

三、项目代码

config配置中的代码

config.ini

[path]
  xml_path     = D:\\dian\\out\\    # xml文件所在的路径
  img_path     = D:\\dian\\out\\wave\\ # 图片文件所在路径
[ftp]
  ftpfile_path   = D:\\Itudou       # 在服务器上的文件存储路径
  ftp_server_ip  = 192.168.56.1      # ftp服务器的IP
  ftp_server_port = 21           # ftp服务器的端口
  ftp_server_name = 20123762        # ftp服务器的用户名
  ftp_server_pwd  = 123456         # ftp服务器的密码
  local_ip     = 192.168.56.1      # 本地主机的IP
  local_port    = 80           #本地主机端口
  comm_way     = udp          #ftp的通信方式
[file]
  file_img     =.jpg          #文件后缀为img
  file_xml     =.xml          #文件后缀为xml
  log_print    = ture          #是否打日志,生产环境上不打日志,在调式程序的时候使用

读配置文件的代码

daos_config.go

package daosconfig
import (
  "bufio"
  "io"
  "os"
  "strings"
)
type Config struct {
  Mymap map[string]string
  strcet string
}
func (c *Config) InitConfig(path string) {
  c.Mymap = make(map[string]string)
  f, err := os.Open(path)
  if err != nil {
    panic(err)
  }
  defer f.Close()
  r := bufio.NewReader(f)
  for {
    b, _, err := r.ReadLine()
    if err != nil {
      if err == io.EOF {
        break
      }
      panic(err)
    }
    s := strings.TrimSpace(string(b))
    if strings.Index(s, "#") == 0 {
      continue
    }
    n1 := strings.Index(s, "[")
    n2 := strings.LastIndex(s, "]")
    if n1 > -1 && n2 > -1 && n2 > n1+1 {
      c.strcet = strings.TrimSpace(s[n1+1 : n2])
      continue
    }
    if len(c.strcet) == 0 {
      continue
    }
    index := strings.Index(s, "=")
    if index  -1 {
      second = second[0:pos]
    }
    pos = strings.Index(second, " #")
    if pos > -1 {
      second = second[0:pos]
    }
    pos = strings.Index(second, "\t//")
    if pos > -1 {
      second = second[0:pos]
    }
    pos = strings.Index(second, " //")
    if pos > -1 {
      second = second[0:pos]
    }
    if len(second) == 0 {
      continue
    }
    key := c.strcet + "=" + frist
    c.Mymap[key] = strings.TrimSpace(second)
  }
}
func (c Config) Read(node, key string) string {
  key = node + "=" + key
  v, found := c.Mymap[key]
  if !found {
    return ""
  }
  return v
}

ftp上传文件核心代码

daos_ftp.go

package daosftp
import (
  "fmt"
  "net"
  "os"
  "strings"
  ftp "github.com/ftp"
  "io/ioutil"
  "regexp"
  "path/filepath"
  cfg "bjdaos_tool/pkg/daosconfig"
  "bjdaos_tool/pkg/env"
  "bjdaos_tool/pkg/daoslog"
)
func getListDir(dirPth string) (files []string,files1 []string, err error) {
  dir, err := ioutil.ReadDir(dirPth)
  if err != nil {
    return nil,nil, err
  }
  PthSep := string(os.PathSeparator)
  for _, fi := range dir {
    if fi.IsDir() {
      files1 = append(files1, dirPth+PthSep+fi.Name())
      getListDir(dirPth + PthSep + fi.Name())
    }else{
      files = append(files, dirPth+PthSep+fi.Name())
    }
  }
  return files,files1, nil
}
func GetAllFileName(path string, str string) (int, []string ) {
  configPath := env.GetConfigPath()
  ftpConfig := new(cfg.Config)
  ftpConfig.InitConfig(configPath + "\\config.ini")
  logPrint := ftpConfig.Read("file", "log_print")
  files, _, err := getListDir(path)
  if err != nil {
    daoslog.WriteLog(logPrint, "System","get file path err")
  }
  fileLen := len(files)
  fileSlice := make([]string,0, fileLen)
  suffix1 := ftpConfig.Read("file", "file_img")
  suffix2 := ftpConfig.Read("file", "file_xml")
  reg_front := regexp.MustCompile("\\d{8}")
  reg_end := regexp.MustCompile("\\d{14}")
  if str == suffix1{
    for i := 0; i  3 {
          domainName = pathFields[len(pathFields)-3]
        }
        ftpUploadFile(ftpserver, ftpuser, pw, path, domainName, serverIp+"_"+fileName[i])
        RemoveFile(filePath, fileName[i])
      }
    }
    return nil
  })
}
func SendJpgFileToFtpServer(filePath string, fileType string) {
  configPath := env.GetConfigPath()
  ftpConfig := new(cfg.Config)
  ftpConfig.InitConfig(configPath + "\\config.ini")
  logPrint := ftpConfig.Read("file", "log_print")
  flen, fileName := GetAllFileName(filePath, fileType)
  serverIp := getLocalIpAddr()
  ftpserverip := ftpConfig.Read("ftp", "ftp_server_ip")
  ftpPort := ftpConfig.Read("ftp", "ftp_server_port")
  ftpuser := ftpConfig.Read("ftp", "ftp_server_name")
  pw := ftpConfig.Read("ftp", "ftp_server_pwd")
  ftpserver := ftpserverip + ":" + ftpPort
  filepath.Walk(filePath, func(path string, f os.FileInfo, err error) error {
    if f == nil {
      return err
    }
    if f.IsDir() {
      return nil
    }
    for i := 0; i  3 {
          domainName = pathFields[len(pathFields)-3]
        }
        ftpUploadFile(ftpserver, ftpuser, pw, path, domainName, serverIp+"_"+fileName[i])
        RemoveFile(filePath, fileName[i])
      }
    }
    return nil
  })
}

打日志的代码

daos_log.go

package daoslog
import (
  "fmt"
  "log"
  "os"
  "github.com/golang/glog"
  "time"
  "bjdaos_tool/pkg/env"
)
func WriteLog(islog, logtype , errcontent string) {
  if islog == "false" {
    return
  }
  if logtype != "Info" && logtype!= "Debug" && logtype!= "Error" && logtype != "System" {
    glog.Error("this is not a logtype ")
    return
  }
  data := time.Now().Format("20060102")
  logPath := env.GetConLogPath()
  logFilea := logPath + "\\" + data+"_"+ logtype+".log"
  errcontent = "[" +errcontent + "]"
  logFile, err := os.OpenFile(logFilea, os.O_RDWR | os.O_CREATE, 0777)
  if err != nil {
    fmt.Printf("open file error=%s\r\n", err.Error())
    os.Exit(-1)
  }
  logger := log.New(logFile, "{"+logtype+"} ", log.Ldate | log.Ltime | log.Lshortfile)
  logger.Println(errcontent)
}

路径处理代码

daos-evn.go

package env
import (
  "os"
  "runtime"
)
var ostype = runtime.GOOS
func GetProjectPath() string{
  var projectPath string
  projectPath, _ = os.Getwd()
  return projectPath
}
func GetConfigPath() string{
  path := GetProjectPath()
  if ostype == "windows"{
    path = path + "\\" + "config\\"
  }else if ostype == "linux"{
    path = path +"/" + "config/"
  }
  return path
}
func GetConLogPath() string{
  path := GetProjectPath()
  if ostype == "windows"{
    path = path + "\\log\\"
  }else if ostype == "linux"{
    path = path + "/log/"
  }
  return path
}

四、总结

主函数:

main.go

package main
import (
  "bjdaos_tool/pkg/daosftp"
  "bjdaos_tool/pkg/env"
  cfg "bjdaos_tool/pkg/daosconfig"
)
func main(){
  configPath := env.GetConfigPath()
  ftpConfig := new(cfg.Config)
  ftpConfig.InitConfig(configPath + "\\config.ini")
  xml_path := ftpConfig.Read("path", "xml_path")
  img_path := ftpConfig.Read("path", "img_path")
  file_img := ftpConfig.Read("file", "file_img")
  file_xml := ftpConfig.Read("file", "file_xml")
  for{
    daosftp.SendXmlFileToFtpServer(xml_path, file_xml)
    daosftp.SendJpgFileToFtpServer(img_path, file_img)
  }
}

本项目依赖包:

完整代码:https://github.com/guoshijiang/go_ftp

以上为个人经验,希望能给大家一个参考,也希望大家多多支持golang学习网。如有错误或未考虑完全的地方,望不吝赐教。

今天关于《golang实现ftp实时传输文件的案例》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表