
您可以使用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)欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)