這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
【代碼】golang 實現的檔案服務(包括上傳,下載的server端和client端)
(2013-09-20 02:03:52)
轉載▼
標籤:golanggo檔案伺服器it |
分類: GO相關 |
//下載(支援斷電續傳)(client)
package main
import (
"http"
"os"
"io"
"strconv"
)
const (
UA = "GolangDownloader 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 reqhttp.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)
ifreq.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
}
deferresp.Body.Close()
resp_body,err := ioutil.ReadAll(resp.Body)
if err !=nil {
return err
}
fmt.Println(resp.Status)
fmt.Println(string(resp_body))
returnnil
}
// 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)
}
}