package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"
)
func main() {

	done := make(chan bool, 1)
	go f(done)
	<-done
}
func f(done chan bool)  {
	for  {
		var files []string

		//root := "D:\\picture"
		pwd,_ := os.Getwd() //获取当前目录
		err := filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
			files = append(files, path)
			return nil
		})
		if err != nil {
			panic(err)
		}
		for _, file := range files {
			//fmt.Println(file)
			fileType:=path.Ext(file)
			if fileType==".jpg" || fileType==".png" || fileType==".jpeg"  || fileType==".Bmp"  {
				postFile(file)
				remove(file)
			}

		}
	}
	done <- true
}
func remove(file string)  {
	err := os.Remove(file)               //删除文件test.txt
	if err != nil {
		//如果删除失败则输出 file remove Error!
		fmt.Println("file remove Error!")
		//输出错误详细信息
		fmt.Printf("%s", err)
	} else {
		//如果删除成功则输出 file remove OK!
		fmt.Print("file remove OK!")
	}
}
//以二进制格式上传文件
func postFile(img string){
	//img:="D:\\desk999\\ax\\12222222222.jpg"
	//这是一个Post 参数会被返回的地址
	name:=filepath.Base(img)
	fileType:=path.Ext(name)
	fileNameOnly:=strings.TrimSuffix(name, fileType)
	/*fmt.Printf("fileNameWithSuffix==%s\n fileType==%s;\n fileNameOnly==%s;",
		name,fileType[1:],fileNameOnly)*/
	uri:="http://域名/api/Test/go/name/"+fileNameOnly+"/type/"+fileType[1:]
	byte,err:=ioutil.ReadFile(img)
	res,err :=http.Post(uri,"application/json",bytes.NewReader(byte))
	if err !=nil {
		fmt.Println("err=",err)
	}
	//http返回的response的body必须close,否则就会有内存泄露
	defer func() {
		res.Body.Close()
		fmt.Println("upload OK!")
	}()
	//读取body
	body,err:=ioutil.ReadAll(res.Body)
	if err!=nil {
		fmt.Println(" post err=",err)
	}
	fmt.Println(string(body))
	time.Sleep(5 * time.Second)
}

图片上传成功后自动删除,接受端我是用thinkphp实现的代码如下

public function go(){
        $data = file_get_contents('php://input');
        if($this->request->param('name',false)){
            $file_name=$this->request->param('name',false).'.'.$this->request->param('type',false);
        }else{
            $file_name=false;
        }
        $url=$this->dataToImage($data,$file_name);
        $this->success('请求成功',['img_url'=>$url]);

    }
    /**
     * 二进制转图片
     */
    public function dataToImage($data,$name=false){
        $m=$this->request->param('m',1);
        $ym=date('Ymd');
        $filepath=ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'go'.DIRECTORY_SEPARATOR.$ym.DIRECTORY_SEPARATOR;
        if (!file_exists($filepath)) {
            mkdir($filepath,0755,true);//创建目录
        }
        if($name){
            $filename=$name;
        }else{
            $filename=time().rand(1000,9999).'-'.$m.'.jpg';
        }
        //file_put_contents($filepath.$filename,$base64_img);
        $fp=@fopen($filepath.$filename,"a+");
        @fputs($fp,$data);
        @fclose($fp);
        $img_url=$this->request->scheme().'://'.$_SERVER['HTTP_HOST'].'/uploads/go/'.$ym.'/'.$filename;
        return $img_url;
    }

本地windows 10系统后台运行的话,编译的时候执行

go build -ldflags "-H windowsgui" -o "uplodImg.exe"  file.go

 刚入门学习go语言,仅供学习使用