***用golang写的HTTP文件服务器,怎么在服务器端判断文件已传输完毕??代码如下:***
```
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func DownloadFile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
currentDir, _ := os.Getwd()
file := currentDir + "\\" + r.URL.Path[1:len(r.URL.Path)]
stat, err := os.Stat(file)
if err != nil {
http.NotFoundHandler().ServeHTTP(w, r)
} else if stat.IsDir() {
http.NotFoundHandler().ServeHTTP(w, r)
} else {
Data, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("读取文件错误:", err.Error())
} else {
fmt.Println("开始发送:", file)
w.Write(Data)
}
}
}
func main() {
http.HandleFunc("/", DownloadFile)
err := http.ListenAndServe(":9999", nil)
if nil != err {
fmt.Println("服务启动错误", err.Error())
}
}
```
有疑问加站长微信联系(非本文作者)