服务器端代码main.go
package main
import (
"io"
"net/http"
"os"
"fmt"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(32 << 20)
file, handler, err :=r.FormFile("file")
if err != nil{
fmt.Println("r.FormFile('file') err")
return
}
defer file.Close()
// fmt.Fprintf(w, "%v", handler.Header)
f, err := os.OpenFile("./html/云文件/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
w.Write([]byte("文件上传成功!"))
}
func main() {
http.Handle("/index/", http.StripPrefix("/index/", http.FileServer(http.Dir("E:/goProject/userProject/fileServer/html/"))));//主页
http.HandleFunc("/uploadfile", uploadHandler)//上传文件
http.ListenAndServe(":12345", nil)
}
html部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>云盘</title>
</head>
<body>
/**post请求提交文件**/
<form action="服务器的ip地址/uploadfile" method="post" enctype="multipart/form-data" style=" color: red;">
<input type="file" name="file" >
<input type="submit" value="提交">
</form>
</body>
</html>
效果图:
主页:
上传文件界面:
云文件列表:
如果有需要远程的,可以将服务器ip通过花生壳内网穿透映射成公网ip。这样只要穿透开着,不管在哪里,有网就可以使用网盘。