golang实现ftp实时传输文件

news/2024/11/8 15:15:35/

一、项目简介

本项目主要实现的功能是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服务器的IPftp_server_port  = 21                     # ftp服务器的端口ftp_server_name  = 20123762               # ftp服务器的用户名ftp_server_pwd   = 123456                 # ftp服务器的密码local_ip         = 192.168.56.1           # 本地主机的IPlocal_port       = 80                     #本地主机端口comm_way         = udp                    #ftp的通信方式
[file]file_img         =.jpg                    #文件后缀为imgfile_xml         =.xml                    #文件后缀为xmllog_print        = ture                   #是否打日志,生产环境上不打日志,在调式程序的时候使用
读配置文件的代码

daos_config.go

package daosconfigimport ("bufio""io""os""strings"
)type Config struct {Mymap  map[string]stringstrcet 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 < 0 {continue}frist := strings.TrimSpace(s[:index])if len(frist) == 0 {continue}second := strings.TrimSpace(s[index+1:])pos := strings.Index(second, "\t#")if pos > -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 + "=" + fristc.Mymap[key] = strings.TrimSpace(second)}
}func (c Config) Read(node, key string) string {key = node + "=" + keyv, found := c.Mymap[key]if !found {return ""}return v
}
ftp上传文件核心代码

daos_ftp.go

package daosftpimport ("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 < fileLen; i++{data_front := reg_front.FindString(files[i])date_end := reg_end.FindString(files[i])imgName := data_front + "_" + date_end + strfileSlice = append(fileSlice, imgName)}}else if str == suffix2 {for i := 0; i < fileLen; i++{data_front := reg_front.FindString(files[i])date_end := reg_end.FindString(files[i])imgName := data_front + "_" + date_end + strfileSlice = append(fileSlice, imgName)}}return fileLen, fileSlice
}func getLocalIpAddr() string {configPath := env.GetConfigPath()ftpConfig := new(cfg.Config)ftpConfig.InitConfig(configPath + "\\config.ini")logPrint := ftpConfig.Read("file", "log_print")network := ftpConfig.Read("ftp", "comm_way")ip := ftpConfig.Read("ftp", "local_ip")port := ftpConfig.Read("ftp", "local_port")address := ip + ":" + portconn, err := net.Dial(network, address)if err != nil {daoslog.WriteLog(logPrint, "System", "get local ip address err")return "127.0.0.1"}defer conn.Close()return strings.Split(conn.LocalAddr().String(), ":")[0]
}func ftpUploadFile(ftpserver, ftpuser, pw, localFile, remoteSavePath, saveName string) {configPath := env.GetConfigPath()ftpConfig := new(cfg.Config)ftpConfig.InitConfig(configPath + "\\config.ini")logPrint := ftpConfig.Read("file", "log_print")ftpfile_path := ftpConfig.Read("ftp", "ftpfile_path")ftp, err := ftp.Connect(ftpserver)if err != nil {daoslog.WriteLog(logPrint, "System", "connect err")}err = ftp.Login(ftpuser, pw)if err != nil {daoslog.WriteLog(logPrint, "System", "Login err")}ftp.ChangeDir(ftpfile_path)dir, err := ftp.CurrentDir()ftp.MakeDir(remoteSavePath)ftp.ChangeDir(remoteSavePath)dir, _ = ftp.CurrentDir()daoslog.WriteLog(logPrint, "System", dir)file, err := os.Open(localFile)if err != nil {daoslog.WriteLog(logPrint, "System", "Open err")}defer file.Close()err = ftp.Stor(saveName, file)if err != nil {daoslog.WriteLog(logPrint, "System", "Stor err")}ftp.Logout()ftp.Quit()logcotent := fmt.Sprintf("%s:%s","success upload file",localFile)daoslog.WriteLog(logPrint, "System", logcotent)
}func RemoveFile(filePath string, fileName string){configPath := env.GetConfigPath()ftpConfig := new(cfg.Config)ftpConfig.InitConfig(configPath + "\\config.ini")logPrint := ftpConfig.Read("file", "log_print")err := os.Remove(filePath + fileName)if err != nil {daoslog.WriteLog("false", "System", "file remove err!")} else {logcotent := fmt.Sprintf("%s:%s","file remove OK!",fileName)daoslog.WriteLog(logPrint, "System", logcotent)}
}func SendXmlFileToFtpServer(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 + ":" + ftpPortfilepath.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 < flen; i++{if f.Name() == fileName[i] {logcotent := fmt.Sprintf("path=",path)daoslog.WriteLog(logPrint, "System", logcotent)pathFields := strings.Split(path, "\\")var domainName stringif len(pathFields) > 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 + ":" + ftpPortfilepath.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 < flen; i++{if f.Name() == fileName[i] {logcotent := fmt.Sprintf("path=",path)daoslog.WriteLog(logPrint, "System", logcotent)pathFields := strings.Split(path, "\\")var domainName stringif len(pathFields) > 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 daoslogimport ("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.GOOSfunc GetProjectPath() string{var projectPath stringprojectPath, _ = 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 mainimport ("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)}
}

本项目依赖包:
这里写图片描述


http://www.ppmy.cn/news/917794.html

相关文章

MFC第十七天 CFont类与LOGFONT结构体、记事本文件打开和保存及是否保存的逻辑流程分析、PreTranslateMessage虚函数与快捷键

文章目录 CFont类与LOGFONT结构体CFontDialog字体信息结构体与HFONT句柄的关系 记事本文件拖入、打开和保存及是否保存的逻辑流程分析PreTranslateMessage虚函数与快捷键附录 CFont类与LOGFONT结构体 CFontDialog 构造函数介绍 public: //用于指定字体对话框的初始字体属性&…

Opencv学习_3 (Opencv读取视频 视频进度控制 写视频)

opencv读取视频&#xff1a; 使用opencv读取视频主要用到这几个函数&#xff1a;cvCreateFileCapture cvQueryFrame (或cvGrabFrame cvRetrieveFrame) cvReleaseCapture。 1&#xff1a;简单介绍&#xff1a; CvCapture是一个结构体&#xff0c;用来保存图像捕获所需要的信息…

Spring+SpringMVC+Mybatis整合【L】

我喜欢直接上代码&#xff0c;这里做个解说&#xff0c;代码下载 http://download.csdn.net/detail/q260996583/9905047 项目框架图&#xff1a; 1、pom.xml 文件内容 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/20…

java编写创建json,将json数据转换成xml,ftp发送到服务器

一.项目简介 1.环境 1).jdk 1.6 2.jar包 commons-net-1.4.1.jar gson-2.2.4.jar jar 建议大家去官网上下载 3.项目结构 二.创建Json 1.javaBean package kingdom.json;public class TransData {private String fq_name; //交易发起人private String js_name; //交…

React Hooks 实用指南

为什么80%的码农都做不了架构师&#xff1f;>>> 前言 在React Conf 2018会议中&#xff0c;Dan Abramov 介绍了 React Hooks。官方的描述为 Hook是一项新功能提案&#xff0c;可让您在不编写类的情况下使用状态和其他React功能。 它们目前处于React v16.7.0-alpha中…

iPhone开发入门(10)— 设备上运行程序

博主&#xff1a;易飞扬原文链接 : http://www.yifeiyang.net/introduction-to-iphone-development-10-devices-running-the-program/转载请保留上面文字。 注册 iPhone Developer Program 启动程序的准备 连接Xcode 生成证明 创建和下载配置文件 注册证明书 设备注册 生成App …

GeoTrellis 尝试(springboot + swagger + spark + GeoTrellis)

原始使用springboot spark 整合在一起做了一个通过请求来驱动spark计算的demo工程&#xff0c;最近看了下GeoSpark,Geotrellis,GeoMeca关于GIS方面的大数据相关技术&#xff0c;就写了一个Demo,我这里用的是Spark 2.4.0 版本 scala 2.11 版本 Geotrellis 2.3.1版本 GeoTools 2…

批处理Bat教程-第八章:番外篇

转载自&#xff1a;http://docs.30c.org/dosbat/chapter08/ 8.1 批量十六进制二进制格式转换 想把数据(流)以十六进制或二进制的形式显示出来&#xff1f;UltraEdit之类的编辑软件一定是首选。即使是要自己亲自转出来&#xff0c;C/C等语言也会方便的多。如果您跟我一样选择使…