我试图通过创建 POST 请求并将其发送到不同的服务器。在我的过程中,我尝试发送 3 个 POST,一个发送到第一台服务器,其余发送到另一台服务器。 p>
但是它只适用于第一个 POST 请求,其余请求得到 200 代码,但文件仍然是空的。我不熟悉 http 连接,我该如何纠正我的程序? 如果您能帮助我,我将不胜感激!
这是接收文件的函数。
func ReceivePublicKey() *gin.Engine { router := gin.Default() router.MaxMultipartMemory = 8 << 20 //router.Static("/", "./static") router.POST("/receive", func(context *gin.Context) { file, _ := context.FormFile("file") log.Println(file.Filename) dst := "./" + file.Filename context.SaveUploadedFile(file, dst) context.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) context.String(200, "server: receive publicKey successfully!") }) return router }
这是创建和发送请求的函数
func CreateSendFileReq(file *os.File, fileName string, url string) *http.Request { bodyBuf := &bytes.Buffer{} bodyWrite := multipart.NewWriter(bodyBuf) fileWrite, err := bodyWrite.CreateFormFile("file", fileName) _, err = io.Copy(fileWrite, file) if err != nil { log.Println("err") } bodyWrite.Close() if err != nil { log.Println("err") } req, _ := http.NewRequest("POST", url, bodyBuf) req.Header.Set("Content-Type", bodyWrite.FormDataContentType()) return req } func SendRequest(r *http.Request) *http.Response { client := &http.Client{} resp, err := client.Do(r) if err == nil { return resp } else { log.Fatal(err) return nil } }
还有不起作用的功能
func IotInit() { privateFile, _ := os.Open("private.pem") publicFile, _ := os.Open("public.pem") userId := GenerateIotId(publicFile) fmt.Println(userId) sendPrivateToServer := Controller.CreateSendFileReq(privateFile, userId+".pem", "http://192.168.42.129:8090/receive") sendPublicToUser := Controller.CreateSendFileReq(publicFile, "public.pem", "http://localhost:8090/receive") resp := Controller.SendRequest(sendPublicToUser) if resp.StatusCode != 200 { log.Fatal(resp.StatusCode) } resp.Body.Close() sendPrivateToUser := Controller.CreateSendFileReq(privateFile, "private.pem", "http://localhost:8090/receive") resp = Controller.SendRequest(sendPrivateToServer) if resp.StatusCode != 200 { log.Fatal(resp.StatusCode) } resp.Body.Close() resp = Controller.SendRequest(sendPrivateToUser) if resp.StatusCode != 200 { log.Fatal(resp.StatusCode) } resp.Body.Close() publicFile.Close() privateFile.Close() }
正确答案
好吧,现在我发现了我的错误。 首先,我没有检查文件的大小,这解释了为什么我在保存空文件时得到 200 代码。 第二个在 IotInit() 中获取 userId 我使用了 io.Copy() 并使文件指针指向文件结尾,这解释了为什么“public.pem”为空,我永远不要将任何内容复制到请求体。此外,我在创建 POST 请求时使用 io.Copy(),“private.pem”也可能为空。
以上是使用 gin 时如何将文件发送到一个进程中的不同服务器?的详细内容。更多信息请关注PHP中文网其他相关文章!

goroutinesarefunctionsormethodsthatruncurranceingo,启用效率和灯威量。1)shememanagedbodo'sruntimemultimusingmultiplexing,允许千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函数可以从panic中恢复。具体方法是:1)在defer函数中使用recover()捕获panic,避免程序崩溃;2)记录详细的错误信息以便调试;3)根据具体情况决定是否恢复程序执行;4)谨慎使用,以免影响性能。

本文讨论了使用GO的“字符串”软件包进行字符串操作,详细介绍了共同的功能和最佳实践,以提高效率并有效地处理Unicode。

本文详细介绍了GO的“时间”包用于处理日期,时间和时区,包括获得当前时间,创建特定时间,解析字符串以及测量经过的时间。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。