这不起作用(图像返回404)..
myRouter := mux.NewRouter() myRouter.Handle("/images/",http.StripPrefix("/images/",http.FileServer(http.Dir(HomeFolder + "images/"))))
这个工程(图像显示确定)..
http.Handle("/images/",http.FileServer(http.Dir(HomeFolder + "images/"))))
简单的下载Web服务器程序,显示问题…
package main import ( "fmt" "net/http" "io" "log" "github.com/gorilla/mux" ) const ( HomeFolder = "/root/test/" ) func HomeHandler(w http.ResponseWriter,req *http.Request) { io.WriteString(w,htmlContents) } func main() { myRouter := mux.NewRouter() myRouter.HandleFunc("/",HomeHandler) // // The next line,the image route handler results in // the test.png image returning a 404. // myRouter.Handle("/images/",http.FileServer(http.Dir(HomeFolder + "images/")))) // myRouter.Host("mydomain.com") http.Handle("/",myRouter) // This method of setting the image route handler works fine. // test.png is shown ok. http.Handle("/images/",http.FileServer(http.Dir(HomeFolder + "images/")))) // HTTP - port 80 err := http.ListenAndServe(":80",nil) if err != nil { log.Fatal("ListenAndServe: ",err) fmt.Printf("ListenAndServe:%s\n",err.Error()) } } const htmlContents = `<!DOCTYPE HTML> <html lang="en"> <head> <title>Test page</title> <Meta charset = "UTF-8" /> </head> <body> <p align="center"> <img src="/images/test.png" height="640" width="480"> </p> </body> </html> `