代码

    1. func main() {
    2. r := gin.Default()
    3. r.GET("/download", func(c *gin.Context) {
    4. //fileName := "11.text"
    5. fileName := "__UNI__9D9D531__20220422141746.apk"
    6. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName)) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
    7. c.Writer.Header().Add("Content-Type", "application/octet-stream")
    8. c.File("./static/" + fileName)
    9. })
    10. // Listen and Server in 0.0.0.0:8080
    11. if err := r.Run(":8080"); err != nil {
    12. log.Fatal(err)
    13. }
    14. }

    文件目录
    image.png