go http 文件下载

package main



import (

	"fmt"

	"net/http"

	"os"

)



func DownFile() {



	userFile := "ttt.exe"

	fout, err := os.Create(userFile)

	defer fout.Close()



	res, err := http.Get("http://10.1.9.102/downpage/up/uu2.4.exe")

	if err != nil {

		fmt.Println("downfile error")

		return

	}

	buf := make([]byte, 1024)

	for {

		size, _ := res.Body.Read(buf)

           //读到文件结尾  

		if size == 0 {

			break

		} else {

			fout.Write(buf[:size])

		}

	}



}

    

  

你可能感兴趣的:(http)