go get -u github.com/labstack/echo/v4
基础Echo使用
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
// 创建一个新的 Echo 实例
e := echo.New()
// 定义一个简单的 GET 路由
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, Echo!")
})
// 启动服务器
e.Start(":8080")
}
从0实现视频文件上传
视频属性查看
package main
import (
"net/http"
"github.com/labstack/echo"
)
func uploadVideo(c echo.Context) error {
// 从表单数据中获取上传的文件
file, err := c.FormFile("video")
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Failed to retrieve file"})
}
return c.JSON(http.StatusOK, echo.Map{"sucess": "Sucess get file", "Filemultipart": file})
}
curl -X POST http://localhost:8080/upload/video -F "video=@/home/ubuntu/C++数据结构面试必看/salad_sys/f83f42fab029ed852a61fc6b094ca5f6.mp4"
//结果
{"Filemultipart":
{"Filename":"f83f42fab029ed852a61fc6b094ca5f6.mp4",
"Header":{"Content-Disposition":
["form-data; name=\"video\";filename=\"f83f42fab029ed852a61fc6b094ca5f6.mp4\""],
"Content-Type":["application/octet-stream"]},
"Size":280516},
"sucess":"Sucess get file"
}
问?视频传输的内容放在哪里 通过file.Open()查看
func uploadVideo(c echo.Context) error {
// 从表单数据中获取上传的文件
file, err := c.FormFile("video")
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Failed to retrieve file"})
}
// 打开上传的文件以读取其内容
src, err := file.Open()
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to open file"})
}
defer src.Close()
// 读取文件内容(示例读取为字节切片,但请注意这样做会将文件内容加载到内存中)
buf := make([]byte, file.Size)
_, err = src.Read(buf)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to read file content"})
}
// 返回成功响应,包含文件的基本信息和内容大小
return c.JSON(http.StatusOK, echo.Map{
"success": "Success get file",
"filename": file.Filename,
"filesize": file.Size,
"filecontent_size": len(buf), // 返回内容大小示例
})
}
上传视频到/video目录
在本地路径创建video目录
运行
package main
import (
"io"
"net/http"
"os"
"path/filepath"
"github.com/labstack/echo"
)
// 上传并保存视频文件
func uploadVideo(c echo.Context) error {
// 从表单数据中获取上传的文件
file, err := c.FormFile("video")
if err != nil {
return c.JSON(http.StatusBadRequest, echo.Map{"error": "Failed to retrieve file"})
}
// 打开上传的文件以读取其内容
src, err := file.Open()
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to open file"})
}
defer src.Close()
// 指定保存路径和文件名
dstPath := filepath.Join("video", file.Filename)
// 创建目标文件
dst, err := os.Create(dstPath)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to create file"})
}
defer dst.Close()
// 将上传的文件内容复制到本地文件
_, err = io.Copy(dst, src)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"error": "Failed to save file"})
}
return c.JSON(http.StatusOK, echo.Map{
"message": "File uploaded successfully",
"path": dstPath,
})
}
成功实现上传视频
下载视频
package main
import (
"net/http"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello,Echo!")
})
// 路由:处理视频上传
e.POST("/upload/video", uploadVideo)
// 路由:播放视频
e.GET("/video/:filename", playVideo)
e.Start(":8080")
}
package main
import (
"path/filepath"
"github.com/labstack/echo"
)
func playVideo(c echo.Context) error {
// 获取视频文件名
filename := c.Param("filename")
videoPath := filepath.Join("video", filename)
// 返回视频文件
return c.File(videoPath)
}
保存到/save文件
保存成功
学习引申
1.文件是否需要解码编码,如何实现?长视频需要切片?上传需要编码?
2.文件是否兼容多种浏览器播放
3.补全前端实现交互
4.补全视频信息表,属性表