golang 视频服务器

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"time"
)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
	video, err := os.Open("./ts.mp4")
	if err != nil {
		log.Fatal(err)
	}
	defer video.Close()

	http.ServeContent(w, r, "ts.mp4", time.Now(), video)
}


func main() {
	// 视频播放服务器
	http.HandleFunc("/ts", ServeHTTP)
	http.ListenAndServe(":8080", nil)
}

编译执行:

go build main.go
./main

浏览器输入:http://192.168.1.243:8080/ts
在这里插入图片描述