標籤:
main.go
package mainimport ("fmt""io""log""net/http""os""strconv""strings""time")func main() {config := readConfig()log.Println("start")http.Handle("/", http.FileServer(http.Dir(config.Wwwroot)))http.Handle("/download/", http.StripPrefix("/download/", http.FileServer(http.Dir(config.Downloadpath))))http.HandleFunc("/upload", upload)http.HandleFunc("/upload2", upload2)http.ListenAndServe(":"+config.Port, nil)}type Sizer interface {Size() int64}func upload(writer http.ResponseWriter, r *http.Request) {file, fileheader, _ := r.FormFile("file_1")defer file.Close()config := readConfig()newfile, _ := os.Create(config.Uploadpath + fileheader.Filename)defer newfile.Close()io.Copy(newfile, file)fmt.Fprintf(writer, "上傳檔案的大小為: %d", file.(Sizer).Size())}func upload2(writer http.ResponseWriter, r *http.Request) {log.Println("upload2 started.")file, fileheader, _ := r.FormFile("file")defer file.Close()config := readConfig()str_time := strings.Replace(fileheader.Filename, "IMG_", "", -1)str_time = strings.Replace(str_time, "VID_", "", -1)var folder stringt, error := time.Parse("20060102", str_time[0:8])if error == nil {folder = config.PhotoPath + strconv.Itoa(t.Year()) + "/" +strconv.Itoa(int(t.Month())) + "月/" + strconv.Itoa(t.Day()) + "日/"//folder := config.PhotoPath + str_time[0:4] + "/" + str_time[4:6] + "月/" + str_time[6:8] + "日/"} else {folder = config.Uploadpath}os.MkdirAll(folder, os.ModeDir)log.Println(folder + fileheader.Filename)newfile, _ := os.Create(folder + fileheader.Filename)defer newfile.Close()_, error2 := io.Copy(newfile, file)if error2 == nil {fmt.Fprintf(writer, "%s", fileheader.Filename)return}log.Println("error: " + error2.Error())fmt.Fprintf(writer, "error: %s", error2.Error())}
myConfig.go
package mainimport ("encoding/json""io/ioutil""log")//定義結構體//首字母大寫 , json:"msg_id" 是 tagtype MyConfig struct {Wwwroot string `json:"wwwroot"`Uploadpath string `json:"uploadpath"`Downloadpath string `json:"downloadpath"`PhotoPath string `json:"photopath"`Port string `json:"port"`}func readConfig() MyConfig {var config MyConfigfilename := `./aa.json`bytes, _ := ioutil.ReadFile(filename)err := json.Unmarshal(bytes, &config)if err != nil {log.Fatal(err)}//log.Println(config.Wwwroot)return config}
serialize.go
package mainimport ("encoding/json""fmt")//定義結構體//首字母大寫 , json:"msg_id" 是 tagtype Message struct {MsgId string `json:"msg_id"`Content string `json:"content"`}//json 序號還原序列化func T3_1() {msg := Message{"msgid_001", "contente2222222222"}str, err := json.Marshal(msg)//輸出 {"msg_id":"msgid_001","content":"contente2222222222"}fmt.Println(string(str), err)var msg1 Message// str := `{"changes": [{"armid":3,"Index":5}, {"armid":3,"Index":6}]}`//還原序列化為 stucterr = json.Unmarshal(str, &msg1)//輸出 {msgid_001 contente2222222222}fmt.Println(msg1)//還原序列化為mapvar msg2 map[string]stringerr = json.Unmarshal(str, &msg2)//輸出 map[msg_id:msgid_001 content:contente2222222222]fmt.Println(msg2)}
aa.json
{"wwwroot":"./wwwroot/","uploadpath":"./up/","downloadpath":"./down/","photopath":"./Record/","port":"80"}
Golan http靜態伺服器