本项目主要实现的功能是ftp客户端不断地将xml文件和jpg文件实时地上传到服务器,当然也可以是其他格式的文件。每当ftp客户端取到一个文件之后,将文件上传到服务器后,然后将其删除。
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 fileLen; i++{
data_front := reg_front.FindString(files[i])
date_end := reg_end.FindString(files[i])
imgName := data_front + "_" + date_end + str
fileSlice = 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 + str
fileSlice = 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 + ":" + port
conn, 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 + ":" + 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 flen; i++{
if f.Name() == fileName[i] {
logcotent := fmt.Sprintf("path=",path)
daoslog.WriteLog(logPrint, "System", logcotent)
pathFields := strings.Split(path, "\\")
var domainName string
if 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 + ":" + 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 flen; i++{
if f.Name() == fileName[i] {
logcotent := fmt.Sprintf("path=",path)
daoslog.WriteLog(logPrint, "System", logcotent)
pathFields := strings.Split(path, "\\")
var domainName string
if len(pathFields) > 3 {
domainName = pathFields[len(pathFields)-3]
}
ftpUploadFile(ftpserver, ftpuser, pw, path, domainName, serverIp+"_"+fileName[i])
RemoveFile(filePath, fileName[i])
}
}
return nil
})
}