package main import ( "fmt" "net/http" "io" "os" ) func Upload(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { file, header, err := r.FormFile("file") if err != nil { fmt.Printf("FromFileErr") http.Redirect(w, r, "/", http.StatusMovedPermanently) return } outPath := fmt.Sprintf("files/%s", header.Filename) fmt.Printf("upload: %s\n", outPath) outFile, err := os.Create(outPath) if err != nil { panic(err) } defer outFile.Close() io.Copy(outFile, file) outFile.Sync() } http.Redirect(w, r, "/", http.StatusMovedPermanently) } func Index(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, ` Uploader
`) } func FileExists(fileName string) bool{ _, err := os.Stat(fileName) return err == nil } func main() { if !FileExists("files") { os.Mkdir("files", 0777) } http.HandleFunc("/", Index) http.HandleFunc("/upload", Upload) http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir("files/")))) fmt.Printf("Listen:8080\n") panic(http.ListenAndServe(":8080", nil)) }