golang 后台搭建以及编码
- 用goland 新建项目vlog ,设置path
- 新建src目录并且新建main.go,src同目录下新建video 文件用于存放上传视频
项目结构截图如下:
因为只是简易搭建,并没有封装代码,代码如下
package main
import (
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
}
func uploadVlog(w http.ResponseWriter , r *http.Request) {
r.Body=http.MaxBytesReader(w,r.Body,10*1024*1024)
err :=r.ParseMultipartForm(10*1024*1024)
if err!=nil {
http.Error(w,"当前文件太大",http.StatusInternalServerError)
return
}
file, header, err := r.FormFile("uploadFile")
ret :=strings.HasSuffix(header.Filename,".mp4");
if ret ==false {
http.Error(w,"当前文件格式不正确",http.StatusInternalServerError)
return
}
dst, err := os.Create("./video/"+header.Filename)
defer dst.Close()
if err!=nil {
http.Error(w,err.Error(),http