我使用FileServer创建了一个简单的GoLang
package main
import (
"log"
"net/http"
)
func loggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RemoteAddr,r.URL.Path)
h.ServeHTTP(w, r)
})
}
func main() {
http.ListenAndServe(":8080", loggingHandler(http.FileServer(http.Dir("/directory/subdir/"))))
}现在,我正在寻找一种方法来检查文件是否被主机正确下载,或者监视下载了多少字节。不幸的是,我在互联网上找不到任何提示,甚至连正面的暗示都找不到。有没有人遇到过类似的问题?是否可以验证从服务器下载文件的正确性?
发布于 2022-02-01 17:38:34
您可以为FileSystem的实现提供FileServer的进度计数器。
示例:
package main
import (
"net/http"
)
type fileWithProgress struct {
http.File
size int64
downloaded int64
lastReported int64
name string
client string
}
func (f *fileWithProgress) Read(p []byte) (n int, err error) {
n, err = f.File.Read(p)
f.downloaded += int64(n)
f.lastReported += int64(n)
if f.lastReported > 1_000_000 { // report every 1e6 bytes
println("client:", f.client, "file:", f.name, "downloaded:", f.downloaded*100/f.size, "%")
f.lastReported = 0
}
return n, err
}
func (f *fileWithProgress) Seek(offset int64, whence int) (int64, error) {
f.downloaded = 0 // reset content detection read if no content-type specified
return f.File.Seek(offset, whence)
}
func (f *fileWithProgress) Close() error {
println("client:", f.client, "file:", f.name, "done", f.downloaded, "of", f.size, "bytes")
return f.File.Close()
}
type dirWithProgress struct {
http.FileSystem
client string
}
func (d *dirWithProgress) Open(name string) (http.File, error) {
println("open:", name, "for the client:", d.client)
f, err := d.FileSystem.Open(name)
if err != nil {
return nil, err
}
st, err := f.Stat()
if err != nil {
return nil, err
}
return &fileWithProgress{File: f, size: st.Size(), name: st.Name(), client: d.client}, nil
}
func fileServer(fs http.FileSystem) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
progressDir := dirWithProgress{FileSystem: fs, client: r.RemoteAddr}
http.FileServer(&progressDir).ServeHTTP(w, r)
}
return http.HandlerFunc(f)
}
func main() {
dir := http.Dir(".")
fs := fileServer(dir)
http.Handle("/", fs)
http.ListenAndServe("127.0.0.1:8080", nil)
}https://stackoverflow.com/questions/70944270
复制相似问题