main.go
package main
import (
"excetran/config"
"excetran/excelController"
"flag"
"github.com/robfig/cron"
)
func main() {
//获取到cfg.json文件里的配置信息,方便后期维护
cfgTmp := flag.String("c", "cfg.json", "configuration file")
flag.Parse()
cfg := *cfgTmp
config.ParseConfig(cfg)
//这是两个定时任务,时间可以在下面的cfg.json文件里进行配置
c := cron.New()
_ = c.AddFunc(config.Config().TimeTask.First, func() {
excelController.InputExcel()
})
_ = c.AddFunc(config.Config().TimeTask.Second, func() {
excelController.InputExcel()
})
c.Start()
//加一个阻断,可以让程序一直在后台运行
select {
}
}
excelController/inputexcel.go
package excelController
import (
"bytes"
"excetran/config"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func InputExcel() {
//这里可以获取cfg.json文件配置的路径 "D:\\报表汇总\\日报"
pwd := config.Config().AbsPath
filepath.Walk(pwd,func(path string, info os.FileInfo, err error) error{
//查询D:\\报表汇总\\日报 这个目录下的所有excel文件
if Reverse(info.Name())[:5] == "xslx." || Reverse(info.Name())[:5] == "slx."{
//path为文件的路径信息(包括文件名),info.Name()为获取的文件名
postFile(path, info.Name())
}
return err
})
}
//这里上传一个excel文件和两个参数,字段类型分别是file,excelName, excelUrl
func postFile(path, filename string ) error {
file, err := os.Open(path)
if err != nil {
fmt.Println("error opening file")
return err
}
defer file.Close()
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
//这里是上传的file文件
fileWriter, err := bodyWriter.CreateFormFile("file", filename)
if err != nil {
fmt.Println("error writing to buffer")
return err
}
_, err = io.Copy(fileWriter, file)
if err != nil {
return err
}
//这里是上传的excelName字段
if fileWriter, err = bodyWriter.CreateFormField("excelName"); err != nil {
fmt.Println(err)
}
if _, err = fileWriter.Write([]byte(filename)); err != nil {
fmt.Println(err)
}
//这里是上传的excelUrl字段
if fileWriter, err = bodyWriter.CreateFormField("excelUrl"); err != nil {
fmt.Println(err)
}
if _, err = fileWriter.Write([]byte(path)); err != nil {
fmt.Println(err)
}
//这里是通过配置文件获取的接口链接
url := "http://" + config.Config().DbSettings.Host + ":" + config.Config().DbSettings.Port + config.Config().DbSettings.Url
//这里是POST请求的类型
contentType := bodyWriter.FormDataContentType()
bodyWriter.Close()
resp, err := http.Post(url, contentType, bodyBuf)
if err != nil {
return err
}
defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
return nil
}
//反转字符串
func Reverse(str string) string {
strArr := []rune(str)
for i := 0; i < len(strArr)/2; i++ {
strArr[len(strArr)-1-i], strArr[i] = strArr[i], strArr[len(strArr)-1-i]
}
return string(strArr)
}
config/cfg.go
package config
import (
"encoding/json"
"github.com/toolkits/file"
"log"
"sync"
)
//定义的配置参数
type ConfigParam struct {
DbSettings *DbSettings `json:"dbSettings"`
TimeTask *TimeTask `json:"timeTask"`
AbsPath string `json:"absPath"`
}
type DbSettings struct {
Host string `json:"host"`
Port string `json:"port"`
Url string `json:"url"`
}
type TimeTask struct {
First string `json:"first"`
Second string `json:"second"`
}
var (
config *ConfigParam
configLock = new(sync.RWMutex)
)
func Config() *ConfigParam {
configLock.RLock()
defer configLock.RUnlock()
return config
}
func ParseConfig(cfg string) {
if cfg == "" {
log.Fatalln("use -c to specify configuration file")
}
if !file.IsExist(cfg) {
log.Fatalln("config file:", cfg, "is not existent")
}
configContent, err := file.ToTrimString(cfg)
if err != nil {
log.Fatalln("read config file:", cfg, "fail:", err)
}
var c ConfigParam
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("parse config file:", cfg, "fail:", err)
}
configLock.Lock()
defer configLock.Unlock()
config = &c
log.Println("read config file:", cfg, "successfully")
}
cfg.json
//这里是上面提到的配置信息,方便后期维护
{
"dbSettings": {
"host": "192.168.*.*",
"port": "8000",
"url": "/excelController/inputExcel"
},
"timeTask": {
"first": "0 0 */1 * * *",
"second": "0 0 23,11 * * *"
},
"absPath": "D:\\报表汇总\\日报"
}
生成exe文件
//生成的exe文件在后台运行,没有窗口,需要窗口的话运行go build main.go即可
go build -ldflags="-H windowsgui" main.go