forked from navy1125/gostudy
/
fileserver.go
63 lines (60 loc) · 1.7 KB
/
fileserver.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
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func main() {
//log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("D:\\work\\gostudy"))))
//http.Handle("/gostudy/", http.StripPrefix("/gostudy/", http.FileServer(http.Dir("D:\\work\\gostudy"))))
http.Handle("/bw/", http.StripPrefix("/bw/", http.FileServer(http.Dir("/home/whj/gogos/gameauth/game/bw"))))
http.HandleFunc("/post", postfunc)
http.HandleFunc("/upload", uploadfunc)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func uploadfunc(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Write([]byte("hello world from host1" + r.Method + "\n"))
return
}
r.ParseMultipartForm(1 << 20)
//w.Write([]byte("hello world from host1"))
//w.Write([]byte(r.Header.Get("Content-Type")))
f, fh, err := r.FormFile("upload")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
filename := fh.Filename
if strings.Contains(fh.Filename, ":") {
strtmp := strings.Split(fh.Filename, ":")
filename = strtmp[len(strtmp)-1]
fmt.Fprintf(w, "\nchange file name:%s\n", filename)
} else if strings.Contains(fh.Filename, "/") {
strtmp := strings.Split(fh.Filename, "/")
filename = strtmp[len(strtmp)-1]
fmt.Fprintf(w, "\nchange file name:%s\n", filename)
}
fmt.Fprintf(w, "\n%+v\n%s\n", fh.Header, filename)
file, err := os.OpenFile("/tmp/"+filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
io.Copy(file, f)
}
func postfunc(w http.ResponseWriter, r *http.Request) {
file, err := os.Open("/tmp/upload.html")
if err != nil {
fmt.Println("open upload.html err:", err)
return
}
text, _ := ioutil.ReadAll(file)
w.Write([]byte(text))
}