golang 文件传输小demo(转载)

转载地址:https://www.cnblogs.com/qq702368956/p/10195497.html

获取文件信息需要用到os. Stat接口,发送文件前开启接收者(服务端),启动客户端先发送文件名给接收者,接收者收到文件名返回确认信息"ok",才读取本地文件 发送给接收者。

发送者

package main

import (
    "fmt"
    "io"
    "net"
    "os"
)

func main() {
    var addr string = "127.0.0.1:1234"
    fmt.Println("请输入需要传输的文件:")
    var path string = "./test.txt"
    info, errf := os.Stat(path)
    if errf != nil{
        fmt.Println("os.Stat errf =", errf)
        return
    }

    conn, err := net.Dial("tcp", addr)
    if err != nil{
        fmt.Println("net.Dial err =",err)
        return
    }
    defer conn.Close()

    _, err = conn.Write([]byte(info.Name()))
    if err != nil{
        fmt.Println("conn.Write info.Name err =",err)
        return
    }
    var n int
    buf := make([]byte, 1024)
    n, err = conn.Read(buf)
    if err != nil{
        fmt.Println("conn.Read ok err =", err)
        return
    }
    if "ok" == string(buf[:n]){
        fmt.Println("ok")
        SendFile(path, conn)
    }
}

func SendFile(path string, conn net.Conn){
     file , err := os.Open(path)

     if err != nil{
         fmt.Println("os.Open err =", err)
         return
     }
     defer file.Close()
     buf := make([]byte, 1024 * 4)

     for {
         n, err := file.Read(buf)
         if err != nil{
             if err == io.EOF{
                 fmt.Println("文件发送完毕")
            } else{
                fmt.Println("file.Read err =",err)
            }

             return
         }
         if n == 0{
             fmt.Println("文件发送完毕")
             break
         }
         conn.Write(buf[:n])
     }
}

 

接收者

package main

import (
    "fmt"
    "io"
    "net"
    "os"
)

func main() {
    var addr string = "127.0.0.1:1234"
    listenner, err := net.Listen("tcp", addr)
    if err != nil{
        fmt.Println("net.Listen err =", err)
        return
    }
    defer listenner.Close()

    conn, errl := listenner.Accept()
    if errl != nil{
        fmt.Println("listenner.Accept err =", errl)
        return
    }
    var n int
    buf := make([]byte, 1024)
    n, err = conn.Read(buf)
    if err != nil{
        fmt.Println("conn.Read fileName err =", err)
        return
    }
    fileName := string(buf[:n])
    n, err = conn.Write([]byte("ok"))
    if err != nil{
        fmt.Println("conn.Write ok err =", err)
        return
    }

    RecvFile(fileName, conn)
}

func RecvFile(fileName string, conn net.Conn){
     file , err := os.Create(fileName)
     if err != nil{
         fmt.Println("os.Create err =", err)
         return
     }

     defer file.Close()

     buf := make([]byte, 1024 * 4)
     for{
         n, err := conn.Read(buf)
         if err != nil{
             if err == io.EOF{
                 fmt.Println("文件接收完成")
            } else {
                fmt.Println("conn.Read err =", err)
            }
            return
        }

         n, err = file.Write(buf[:n])
         if err != nil{
             fmt.Println("file.Write err =", err)
             break
        }
     }
}

  

转载于:https://www.cnblogs.com/K-artorias/p/11585943.html

你可能感兴趣的:(golang 文件传输小demo(转载))