首页 技术 正文
技术 2022年11月15日
0 收藏 322 点赞 5,117 浏览 3720 个字

GITHUB网址:

https://github.com/astaxie/build-web-application-with-golang

内容

login.gtpl

<html><head><title>hello, go form</title></head><body><form action="/login" method="post"><input type="checkbox" name="interest" value="football">足球<input type="checkbox" name="interest" value="basketball">篮球<input type="checkbox" name="interest" value="tennis">网球username:<input type="text" name="username">age:<input type="text" name="age">password:<input type="password" name="password"><input type="hidden" name="token" value="{{.}}"><input type="submit" value="login"></form></body></html>

  upload.gtpl

<html><head><title>hello, go upload</title></head><body><form action="/upload" method="post" enctype="multipart/form-data"><input type="file" name="uploadfile" /><input type="hidden" name="token" value="{{.}}"><input type="submit" value="upload"></form></body></html>

  go.main

package mainimport ("crypto/md5""fmt""html/template""io""log""net/http""os""strconv""strings""time")func sayHelloName(w http.ResponseWriter, r *http.Request) {r.ParseForm()fmt.Println(r.Form)fmt.Println("path", r.URL.Path)fmt.Println("scheme", r.URL.Scheme)fmt.Println(r.Form["url_long"])for k, v := range r.Form {fmt.Println("key: ", k)fmt.Println("val: ", strings.Join(v, ""))}fmt.Fprintf(w, "Hello astaxie!")}func login(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("login.gtpl")t.Execute(w, token)log.Println(t.Execute(w, nil))} else {r.ParseForm()token := r.Form.Get("token")if token != "" {log.Printf("token is ok")} else {log.Fatal("no token")}fmt.Println("username: ", template.HTMLEscapeString(r.Form.Get("username")))fmt.Println("password:", r.Form["password"])fmt.Println("token:", r.Form["token"])age, err := strconv.Atoi(r.Form.Get("age"))if err != nil {log.Print(err)}if age > 100 {log.Print("too long")} else {fmt.Println("age:", r.Form["age"])}template.HTMLEscape(w, []byte(r.Form.Get("username")))}}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("./"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)if err != nil {fmt.Println(err)return}defer f.Close()io.Copy(f, file)}}func main() {http.HandleFunc("/", sayHelloName)http.HandleFunc("/login", login)http.HandleFunc("/upload", upload)err := http.ListenAndServe(":9090", nil)if err != nil {log.Fatal(err)}}

  clientupload/main.go

package mainimport ("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 buff")return err}fh, err := os.Open(filename)if err != nil {fmt.Println("error opening to buff")return err}defer fh.Close()_, err = io.Copy(fileWriter, fh)if err != nil {fmt.Println("error Copy")return err}contentType := bodyWriter.FormDataContentType()bodyWriter.Close()resp, err := http.Post(targetUrl, contentType, bodyBuf)if err != nil {fmt.Println("error Post")return err}defer resp.Body.Close()resp_body, err := ioutil.ReadAll(resp.Body)if err != nil {fmt.Println("error ReadAll")return err}fmt.Println(resp.Status)fmt.Println(string(resp_body))return nil}func main() {target_url := "http://localhost:9090/upload"filename := "./bee-1.8.3.tar.gz"postFile(filename, target_url)}

  

学习build-web-application-with-golang第四章内容

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849