@H_404_1@模拟HTML客户端(不使用浏览器上传文件)
@H_404_1@src/contoso.org/client/client.go :
@H_404_1@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
}
defer fh.Close()
//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
}
//获得文件名最快的代码实现方式比较
func main() {
filename := "/root/Desktop/模特唐嘉灵.jpg"
target_url := "http://localhost:8080/upload"
postFile(filename,target_url)
}
模特唐嘉灵.jpg 是个大美女
@H_404_1@
@H_404_1@第1种临时启动客户端程序的方式,指令如下:
@H_404_1@[root@contoso ~]# cd $GOPATH/src/contoso.org/client##客户端路径
[root@contoso client]# go run client.go ##上传文件,临时性非全局执行程序,注意,要先启动服务器端
@H_404_1@另外一种方式是先安装客户端后启动客户上传文件,指令如下:
@H_404_1@@H_404_1@[root@contoso ~]#cd $GOPATH && go install contoso.org/client && cd ~ ##安装程序,可执行程序的位置在$GOPATH/bin目录下
@H_404_1@@H_404_1@@H_404_1@[root@contoso ~]# client@H_404_1@##上传文件,全局执行程序,@H_404_1@注意,要先启动服务器端
@H_404_1@@H_404_1@@H_404_1@@H_404_1@@H_404_1@
@H_404_1@@H_404_1@HTML客户端(使用浏览器上传文件)
@H_404_1@@H_404_1@@H_404_1@@H_404_1@src/contoso.org/server/upload.gtpl :
@H_404_1@@H_404_1@@H_404_1@@H_404_1@<html>
<head>
<title>上传文件</title>
</head>
<body>
<form enctype="multipart/form-data" action="/upload" method="post">
<input type="file" name="uploadfile" />
<input type="hidden" name="token" value="{{.}}"/>
<input type="submit" value="upload" />
</form>
</body>
</html>@H_404_1@@H_404_1@@H_404_1@
@H_404_1@@H_404_1@@H_404_1@@H_404_1@WEB服务器要首先启动起来
@H_404_1@@H_404_1@src/contoso.org/server/server.go :
package main
import (
"crypto/md5"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
func main() {
http.HandleFunc("/upload",MyUpload)
http.ListenAndServe(":8080",nil)
}
func MyUpload(res http.ResponseWriter,req *http.Request) {
fmt.Println("method:",req.Method) //获取请求的方法
if req.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(res,token)
} else {
// 333554432/(1024*1024) = 32MB,最大允许上传32M的文件
req.ParseMultipartForm(32 << 20) //2的20次方2²º=1048576,左移位操作表示乘法运算 1048576 * 32 = 33554432
file,handler,err := req.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
fmt.Fprintf(res,"%v",handler.Header)
fmt.Printf("%v\n",handler.Filename)
//务必先手动在目录@H_404_1@src/contoso.org/server/ 下创建一个test文件夹 f,err := os.OpenFile("./test/"+filepath.Base(handler.Filename),os.O_WRONLY|os.O_CREATE,0666) if err != nil { fmt.Println(err) return } defer f.Close() io.Copy(f,file) } }