tln(img_name)
img_url := "http://localhost:8001/images/Test" + name //C(文件服务器地址)
img_data := ReadImgData(img_url) //向服务器气球图片数据
if len(img_data) == 0 {
fmt.Println("file access forbidden:", name)
return nil, os.ErrNotExist
}
fmt.Println("get img file:", img_url)
var f http.File = &ReadImg{buf: bytes.NewReader(img_data), fileUrl: img_name, fileData: img_data} //标红的可以查看标准库bytes的Reader类型,NewReader(p []byte)可返回*Reader,然后调用和http.File相同的Seek()和Read()方法
return f, nil
}
func InitHttpImgFileServ() {
http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(HttpDealImg{})))
}
func main() {
InitHttpImgFileServ()
http.ListenAndServe(":8000", nil)
}
六、测试截图
请求地址http://localhost:8000/img/qq.png
后台打印的信息是获取的是http://localhost:8001/images/Test/qq.png,也就是C(文件服务器里图片的真实地址)
七、结束
当文件保存在其他的服务器上,需要在客户端显示图片,但是不想被知道真实的路径的时候就可以通过http.File和os.FileInfo去封装一下就可以实现,可能上面介绍的不清楚,有问题的可以留言一起沟通学习一下,谢谢!
如果有更好的实现方法,希望大家可以分享出来一起学习,谢谢、
|