golang文件上傳和下載


【代碼】golang 實現的文件服務(包括上傳,下載的server端和client端)

 
//下載(支持斷電續傳)(client)

package main

import (
 

   "http"
    "os"
    "io"
    "strconv"
)

const (
    UA = "Golang Downloader from Kejibo.com"
)

func main() {
    f, err := os.OpenFile("./file.exe", os.O_RDWR, 0666)  //其實這里的 O_RDWR應該是 O_RDWR|O_CREATE,也就是文件不存在的情況下就建一個空文件,但是因為windows下還有BUG,如果使用這個O_CREATE,就會直接清空文件,所以這里就不用了這個標志,你自己事先建立好文件。
    if err != nil { panic(err) }
    stat, err := f.Stat()    //獲取文件狀態
    if err != nil { panic(err) }
    f.Seek(stat.Size, 0)    //把文件指針指到文件末,當然你說為何不直接用 O_APPEND 模式打開,沒錯是可以。我這里只是試驗。
    url := "http://dl.google.com/chrome/install/696.57/chrome_installer.exe"
    var req http.Request
    req.Method = "GET"
    req.UserAgent = UA
    req.Close = true
    req.URL, err = http.ParseURL(url)
    if err != nil { panic(err) }
    header := http.Header{}
    header.Set("Range", "bytes=" + strconv.Itoa64(stat.Size) + "-")
    req.Header = header
    resp, err := http.DefaultClient.Do(&req)
    if err != nil { panic(err) }
    written, err := io.Copy(f, resp.Body)
    if err != nil { panic(err) }
    println("written: ", written)
}


//下載(server)
package main

import (
    "flag"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
    "path"
    "strconv"
)

var dir string
var port int
var staticHandler http.Handler

// 初始化參數
func init() {
    dir = path.Dir(os.Args[0])
    flag.IntVar(&port, "port", 800, "服務器端口")
    flag.Parse()
    fmt.Println("dir:", http.Dir(dir))
    staticHandler = http.FileServer(http.Dir(dir))
}

func main() {
    http.HandleFunc("/", StaticServer)
    err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

// 靜態文件處理
func StaticServer(w http.ResponseWriter, req *http.Request) {
    fmt.Println("path:" + req.URL.Path)
    if req.URL.Path != "/down/" {
        staticHandler.ServeHTTP(w, req)
        return
    }

    io.WriteString(w, "hello, world!\n")
}

//上傳(client)

package main

import (
    "bytes"
    "fmt"
    "io"
    "io/ioutil"
    "mime/multipart"
    "net/http"
    "os"
)

func postFile(filename string, targetUrl string) error {
    bodyBuf := &bytes.Buffer{}
    bodyWriter := multipart.NewWriter(bodyBuf)
    //關鍵的一步操作
    fileWriter, err := bodyWriter.CreateFormFile("uploadfile", filename)
    if err != nil {
        fmt.Println("error writing to buffer")
        return err
    }
    //打開文件句柄操作
    fh, err := os.Open(filename)
    if err != nil {
        fmt.Println("error opening file")
        return err
    }

    //iocopy
    _, err = io.Copy(fileWriter, fh)
    if err != nil {
        return err
    }
    contentType := bodyWriter.FormDataContentType()
    bodyWriter.Close()
    resp, err := http.Post(targetUrl, contentType, bodyBuf)
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    resp_body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return err
    }
    fmt.Println(resp.Status)
    fmt.Println(string(resp_body))

    return nil
}

// sample usage
func main() {
    target_url := "http://localhost:9090/upload"
    filename := "./astaxie.pdf"
    postFile(filename, target_url)
}

//上傳(server)
package main

import (
    "crypto/md5"
    "flag"
    "fmt"
    "html/template"
    "io"
    "log"
    "net/http"
    "os"
    "path"
    "strconv"
    "time"
)

var dir string
var port int

// 初始化參數
func init() {
    dir = path.Dir(os.Args[0])
    flag.IntVar(&port, "port", 800, "服務器端口")
    flag.Parse()
    fmt.Println("dir:", http.Dir(dir))
}

func main() {
    http.HandleFunc("/upload", upload)
    err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

// 處理/upload 邏輯
func upload(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method) //獲取請求的方法
    if r.Method == "GET" {
        crutime := time.Now().Unix()
        h := md5.New()
        io.WriteString(h, strconv.FormatInt(crutime, 10))
        token := fmt.Sprintf("%x", h.Sum(nil))
        t, _ := template.ParseFiles("upload.gtpl")
        t.Execute(w, token)
    } else {
        r.ParseMultipartForm(32 << 20)
        file, handler, err := r.FormFile("uploadfile")
        if err != nil {
            fmt.Println(err)
            return
        }
        defer file.Close()
        fmt.Fprintf(w, "%v", handler.Header)
        f, err := os.OpenFile("./upload/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer f.Close()
        io.Copy(f, file)
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM