From 46a546c631897d0fc32695a1ad88b47a4f6c25d4 Mon Sep 17 00:00:00 2001 From: Kakune55 Date: Mon, 18 Dec 2023 16:46:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/upload.gtpl | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 2 + main.go | 54 +++++++++++++ 4 files changed, 262 insertions(+) create mode 100644 Web/upload.gtpl create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/Web/upload.gtpl b/Web/upload.gtpl new file mode 100644 index 0000000..18d2f1d --- /dev/null +++ b/Web/upload.gtpl @@ -0,0 +1,201 @@ + + + + + + 文件上传 + + + + + +
+

Pixel

+ +
+ +
+
+ + + +
+ +
+
+
+ +
+ 缩略图 +
+
+ + + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7d27c1c --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module Pixel + +go 1.21.3 + +require github.com/mattn/go-sqlite3 v1.14.19 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3042612 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI= +github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..f6cd9b3 --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "crypto/md5" + "fmt" + "html/template" + "io" + "log" + "net/http" + "os" + "strconv" + "time" +) + + +func main() { + + http.HandleFunc("/upload", upload) //设置访问的路由 + err := http.ListenAndServe(":9090", nil) //设置监听的端口 + if err != nil { + log.Fatal("ListenAndServe: ", err) + } +} + + +// 处理/upload 逻辑 +func upload(w http.ResponseWriter, r *http.Request) { + fmt.Println("method:", r.Method) //获取请求的方法 + if r.Method == "GET" { //前端页面渲染 + crutime := time.Now().Unix() + h := md5.New() + io.WriteString(h, strconv.FormatInt(crutime, 10)) + token := fmt.Sprintf("%x", h.Sum(nil)) + + t, _ := template.ParseFiles("Web/upload.gtpl") + t.Execute(w, token) + } else { //后端POST接收逻辑 + r.ParseMultipartForm(32 << 20) + file, handler, err := r.FormFile("file") + if err != nil { + fmt.Println(err) + return + } + defer file.Close() + fmt.Fprintf(w, "%v", handler.Header) + f, err := os.OpenFile("./test/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) // 此处假设当前目录下已存在test目录 + if err != nil { + fmt.Println(err) + return + } + defer f.Close() + io.Copy(f, file) + } +} \ No newline at end of file