golang http 用户密码验证以及读取大文件和小文件
func readSmallFile(path string) string{
fi,err:=os.Open(path)
if nil{
panic(err)
}
defer fi.Close()
fd,err:=ioutil.ReadAll(fi)
return string(fd)
}
func readBigFile(path string) {
file,err := os.Open(path)
if err!=nil{
panic(err)
}
defer file.Close()
bufReader := bufio.NewReader(file)
buf := make([]byte,1024*1024*10)
for{
readNum,err := buffReader.Read(buf)
if err !=nil && err != io.EOF{
panic(err)
}
if readNum == 0{
fmt.Println("read file end...")
break
}
}
}
func putHttp(url,file,username,userpasswd string) int{
client := &http.Clien{}
fileinfo:=readSmallFile(file)
req,err := http.NewRequest("PUT",url,strings.NewReader(fileinfo))
if nil{
panic(err)
return 0
}
//req.SetBasicAuth(username,userpasswd)
req.Header.Set("Authoriaztion","Bearer "+userpasswd)
req.Header.Set("Content-Type","text/html;charset=UTF-8")
resp,err := client.Do(req)
if err!=nil{
painc(err)
return 0
}
body,err := ioutil.ReadAll(resp.Body)
if err!=nil{
panic(err)
return 0
}
defer resp.Body.Close()
fmt.Println(string(body))
fmt.Println("statuscode==",resp.StatusCode)
return 1
}