我设法让它工作。我相信这只是我自己的错误,没有正确填写URI。无论如何,我将发布我的解决方案中的片段以供将来参考。


客户端 html 文件格式部分:


<form action="/test-main/file-test" enctype="multipart/form-data" method="post">

    <label for="file-upload">Upload your file :</label>

    <input type="file" id="file-upload" name="file-upload" accept="image/*">

</form>

服务器 A 代码:


import (

    "net/http"

    "errors"

    "fmt"

    "log"

    "io/ioutil"

    "bytes"


    "github.com/gorilla/mux"

)


func fileUpload(w http.ResponseWriter, r *http.Request) {

    body, err := ioutil.ReadAll(r.Body)

    if err != nil {

        return log.Fatal(err)

    }


    r.Body = ioutil.NopCloser(bytes.NewReader(body))

    // If Server A and B are separate docker images, you may need to use their docker subnet IP, like below.

    proxyReq, err := http.NewRequest(r.Method, fmt.Sprintf("http://172.18.0.2:8082%s", r.RequestURI), bytes.NewReader(body))

    if err != nil {

        return log.Fatal(err)

    }


    for header, values := range r.Header {

        for _, value := range values {

            proxyReq.Header.Add(header, value)

        }

    }


    client := &http.Client{}

    resp, err := client.Do(proxyReq)

    if err != nil {

        return log.Fatal(err)   

    }

    defer resp.Body.Close()

    respBody, err := ioutil.ReadAll(resp.Body)

    if err != nil {

        return log.Fatal(err)

    }


    // Process Server B response

    // ...

}


func createRouter() *mux.Router {

    r := mux.NewRouter()

    testPath := r.PathPrefix("/test-main").Subrouter()

    testPath.HandleFunc("/file-test", fileUpload)

    return r

}


func main() {

    // Create Server and Route Handlers

    srv := &http.Server{

        Handler:      createRouter(),

        Addr:         ":8081",

        ReadTimeout:  30 * time.Second,

        WriteTimeout: 30 * time.Second,

    }


    // Start Server

    go func() {

        log.Println("Starting Server")

        if err := srv.ListenAndServe(); err != nil {

            log.Fatal(err)

        }

    }()

}

和服务器 B 代码:


import (

    "encoding/json"

    "fmt"

    "io/ioutil"

    "net/http"


    "github.com/gorilla/mux"

)


func uploadFile(w http.ResponseWriter, r *http.Request) {

    if err := r.ParseMultipartForm(10 << 20); err != nil {

        log.Fatal(err)

    }


    file, handler, err := r.FormFile("file-upload")

    if err == http.ErrMissingFile {

        return nil

    }


    if err != nil {

        log.Fatal(err)

    }


    fmt.Printf("Uploaded File: %+v\n", handler.Filename)

    fmt.Printf("File Size: %+v\n", handler.Size)

    fmt.Printf("MIME Header: %+v\n", handler.Header)


    defer file.Close()


    // Create file

    dst, err := os.Create(fmt.Sprintf("/some-destination-folder/%s", handler.Filename))

    if err != nil {

        log.Fatal(err)

    }


    // Copy the uploaded file to the created file on the file system.

    if _, err := io.Copy(dst, file); err != nil {

        if err2 := dst.Close(); err2 != nil {

            log.Fatal(err)

        }

        log.Fatal(err)

    }

    dst.Close()


    return nil

}


func (c *Controller) createRouter() *mux.Router {

    r := mux.NewRouter()

    testPath := r.PathPrefix("/test-main").Subrouter()

    testPath.HandleFunc("/file-test", uploadFile)


    return r

}


func main() {

    // Create Server and Route Handlers

    srv := &http.Server{

        Handler:      createRouter(),

        Addr:         ":8082",

        ReadTimeout:  30 * time.Second,

        WriteTimeout: 30 * time.Second,

    }


    // Start Server

    go func() {

        log.Println("Starting Server")

        if err := srv.ListenAndServe(); err != nil {

            log.Fatal(err)

        }

    }()

}

祝未来的读者好运。