Golan http靜態伺服器

來源:互聯網
上載者:User

標籤:

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靜態伺服器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.