go学习 --- web文件上传

一、http文件上传

1、upload.gtpl




  
  Upload File 


 
package main

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
)
//解析from
func process(w http.ResponseWriter,r *http.Request)  {
	r.ParseForm()
	fmt.Fprintln(w,r.PostForm)
}
func upload(w http.ResponseWriter,r *http.Request)  {
	if r.Method == "GET" {
		//解析upload.gtpl文件
		files, err:= template.ParseFiles("D:/goproject/src/webDemo/http/upload.gtpl")
		if err != nil {
			fmt.Println("解析aa.gtpl文件错误:",err)
			return
		}
		files.Execute(w,nil)
	} else {
		//上传文件
		file, _, err := r.FormFile("uploaded")
		if err == nil {
			data, err := ioutil.ReadAll(file)
			if err == nil  {
				fmt.Fprintln(w,string(data))
			}
		}
	}
}
func main() {
     http.HandleFunc("/form",process)
	 http.HandleFunc("/upload",upload)
     http.ListenAndServe(":8090",nil)
}

二、form文件上传

1、server

package main

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

func upload(w http.ResponseWriter, r *http.Request)  {
	//获取请求方法
	fmt.Println("method:",r.Method)
	if r.Method == "GET" {
		unix := time.Now().Unix()
		hash := md5.New()
		io.WriteString(hash, strconv.FormatInt(unix, 10))
		token := fmt.Sprintf("%x", hash.Sum(nil))
		fmt.Println("token=",token)
		files, err := template.ParseFiles("D:/goproject/src/webDemo/http/form/upload.html")
		if err != nil {
			panic(err)
		}
		files.Execute(w,token)
	}else {
		//开辟空间
		r.ParseMultipartForm(32 << 20)
		file, header, err := r.FormFile("uploaded")
		if err != nil {
			fmt.Println(err)
			return
		}
		defer file.Close()
		fmt.Fprintf(w,"%v",header.Header)
		openFile, err := os.OpenFile("D:/goproject/src/webDemo/http/test/"+header.Filename, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			fmt.Println(err)
			return
		}
		defer openFile.Close()
		io.Copy(openFile,file)
	}
}
func main() {
	http.HandleFunc("/upload",upload)
	http.ListenAndServe(":8080",nil)
}

二、client

package main

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

func postFile(fileName string, url string) error  {
	bodybuf := &bytes.Buffer{}
	bodyWriter := multipart.NewWriter(bodybuf)
	fielWriter, err := bodyWriter.CreateFormFile("uploaded", fileName)
	if err != nil {
		fmt.Println("error writing buffer")
		return err
	}
	//打开文件句柄
	file, err := os.Open(fileName)
	if err != nil {
		fmt.Println("文件打开错误")
		return err
	}
	//文件复制
	_, err = io.Copy(fielWriter, file)
	if err != nil {
		fmt.Println(err)
		return err
	}
	contentType := bodyWriter.FormDataContentType()
	//关闭文件流
	bodyWriter.Close()
	resp, err := http.Post(url, contentType, bodybuf)
	if err != nil {
		fmt.Println(err)
		return err
	}
	defer resp.Body.Close()
	all, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	fmt.Println(resp.Status)
	fmt.Println(string(all))
	return nil

}
func main() {
	url := "http://localhost:8080/upload"
	fileName := "./2.txt"
	postFile(fileName,url)
}



    
    文件上传




 go学习 --- web文件上传_第1张图片

 go学习 --- web文件上传_第2张图片

你可能感兴趣的:(GO语言,golang,开发语言)