Golang使用sftp golang库将远程文件复制到本地文件夹

Golang使用sftp golang库将远程文件复制到本地文件夹,第1张

Golang使用sftp golang库将远程文件复制到本地文件夹

您可以使用sftp包中的

Open(path string)
WriteTo(wio.Writer)
方法来完成此 *** 作(当然,您需要os.File或类似的东西来写入)。

client, err := ssh.Dial("tcp", "192.x.x.x:22", sshConfig)if err != nil {    panic("Failed to dial: " + err.Error())}fmt.Println("Successfully connected to ssh server.")// open an SFTP session over an existing ssh connection.sftp, err := sftp.NewClient(client)if err != nil {    log.Fatal(err)}defer sftp.Close()srcPath := "/tmp/"dstPath := "C:/temp/"filename := "test.txt"// Open the source filesrcFile, err := sftp.Open(srcPath + filename)if err != nil {    log.Fatal(err)}defer srcFile.Close()// Create the destination filedstFile, err := os.Create(dstPath + filename)if err != nil {    log.Fatal(err)}defer dstFile.Close()// Copy the filesrcFile.WriteTo(dstFile)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4916234.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存