douhao7677 2018-02-26 10:29
浏览 352
已采纳

HTTP服务器和客户端下载多个文件

I have tried setting up a download server and a download client for individual files. How can I modify them to serve/download all the files from a directory?

Following are my server and client codes:

//server.go
func main() {
        http.HandleFunc("/dlpath", handle)
        err := http.ListenAndServe(":10001", nil)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }

func handle(writer http.ResponseWriter, r *http.Request) {
    filename := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve\\send.txt"
    http.ServeFile(writer, r, filename)
}

//client.go
func main() {
    downloadFile("res_out.txt", "http://localhost:10001/dlpath")
}

func downloadFile(dirname string, url string) error {

    // Create the file
    out, err := os.OpenFile(dirname, os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0666)
    if err != nil {
        fmt.Println(err)
    }
    defer out.Close()

    // get data
    request, err := http.NewRequest("GET", url, nil)
    if err != nil {
        fmt.Println(err)
    }

    client := http.Client{}
    resp, err := client.Do(request)
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    // Write the body to file
    _, err = io.Copy(out, resp.Body)
    if err != nil {
        return err
    }
    return nil
}

I tried serving the directory in the handle function like so:

dirname := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve"
http.FileServer(http.Dir(dirname))

and tried to print out the response on the client side, but I got nothing. How can I serve all the files from the /serve directory, and download them in the client?

EDIT: Following are the contents of the serve directory:

serve ---sample.txt ---send.txt ---dir2 ------abc.txt

How can I download all these files on the client side as separate files, with the directory structure intact?

Update: When I call the http.Handle function (as mentioned in the answer) directly in the main function, I am able to serve all the files, and the file within the inner directory too.

However, when I call the same within the handle function, it doesn't serve anything. I am guessing this has something to do with the path?

  • 写回答

2条回答 默认 最新

  • drlma06060 2018-02-26 11:00
    关注

    The problem can be in the file path you are requesting. It is prefixed with /dlpath/. You must strip this prefix and pass the rest of the text as a path. See: https://godoc.org/net/http#FileServer

    Could you try this code snippet:

    package main
    
    import (
        "log"
        "net/http"
    )
    
    func main() {
            dirName := "C:\\Users\\aarvi\\GolandProjects\\src\\Practice\\download\\serve"
    
            http.Handle("/dlpath/", http.StripPrefix("/dlpath", http.FileServer(http.Dir(dirName))))
    
            err := http.ListenAndServe(":8001", nil)
            if err != nil {
                    log.Fatal("ListenAndServe: ", err)
            }
    }
    

    Hope this helps.

    P.S. This is from the case when you are serving the directory in the handler function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题