• 欢迎访问IT乐园(o゚▽゚)o
  • 推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。

golang下载服务器

golang fhy 7年前 (2017-07-31) 6982次浏览 0个评论
文章目录[隐藏]

golang web 服务器

程序读取当前目录文件

package main

import (
    "fmt"
    "log"
    "net/http"
)


type String string

type Struct struct {
    Greeting string
    Punct    string
    Who      string
}

func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()  //解析参数,默认是不会解析的
    // fmt.Println(r.Form)  //这些信息是输出到服务器端的打印信息
    fmt.Println("path:", r.URL.Path)
    // fmt.Println("scheme:", r.URL.Scheme)
    fmt.Fprint(w, string(s))
}

func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, fmt.Sprintf("%v", s))
} 

func faviconHandler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "./favicon.ico")
}


func main() {
    fmt.Println("Golang 服务器已启动,监听端口 4001")
    http.Handle("/", String("Home Page 首页"))
    http.Handle("/string", String("I'm a frayed knot."))
    http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
    http.HandleFunc("/favicon.ico", faviconHandler)

    // 下载文件服务
    http.Handle("/down/", http.StripPrefix("/down/", http.FileServer(http.Dir("./"))))

    err := http.ListenAndServe("0.0.0.0:4001", nil)
    if err != nil {
        log.Fatal(err)
    }
}

运行界面

golang 下载服务器

文件浏览

golang 下载服务器

问题

file server 下载一个大于 2GB 的文件时,总是下载到 1G 左右就结束下载,但是没有报告错误。

最终的结果是我发现问题出在 net/sendfile_windows.go 这个文件的 sendFile 函数里。
此问题在 win 环境下发生。目前没有解决

linux 大文件下载测试通过 。

“`golang
func sendFile(fd *netFD, r io.Reader) (written int64, err error, handled bool)
“`

golang 源码中这个函数前有一行注释:

// sendFile copies the contents of r to c using the TransmitFile
// system call to minimize copies.
//
// if handled == true, sendFile returns the number of bytes copied and any
// non-EOF error.
//
// if handled == false, sendFile performed no work.
//
// Note that sendfile for windows does not support >2GB file.
func sendFile(fd *netFD, r io.Reader) (written int64, err error, handled bool) {

 


IT 乐园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:golang 下载服务器
喜欢 (0)
关于作者:
九零后挨踢男
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址