/
server.go
53 lines (47 loc) · 972 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"net/http"
"os"
"io"
)
func uploadHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(2000)
if err != nil {
fmt.Fprintln(w,err)
return
}
formdata := r.MultipartForm
files := formdata.File["RemoteFile"]
for i := range files {
file, err := files[i].Open()
defer file.Close()
if err != nil {
fmt.Fprintln(w, err)
return
}
out, err := os.Create(files[i].Filename)
defer out.Close()
if err != nil {
fmt.Fprintf(w, "create file err!")
return
}
_, err = io.Copy(out, file)
if err != nil {
fmt.Fprintln(w, err)
return
}
}
}
func handFile(w http.ResponseWriter, r *http.Request) {
if (r.URL.Path == "/") {
http.ServeFile(w, r, "./index.html")
} else {
http.ServeFile(w, r, "." + r.URL.Path)
}
}
func main() {
http.HandleFunc("/", handFile)
http.HandleFunc("/upload", uploadHandler)
http.ListenAndServe(":2014", nil)
}