Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1083063
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 746
  • 用 户 组: 普通用户
  • 注册时间: 2018-06-12 20:01
个人简介

寫写code、调調bug、填填坑,僅此而已。

文章分类

全部博文(80)

文章存档

2019年(30)

2018年(50)

分类: C/C++

2018-11-21 19:09:32


点击(此处)折叠或打开

  1. func readSmallFile(path string) string{
  2.     fi,err:=os.Open(path)
  3.     if nil{
  4.        panic(err)
  5.     }
  6.     defer fi.Close()
  7.     fd,err:=ioutil.ReadAll(fi)
  8.     return string(fd)
  9. }
  10. func readBigFile(path string) {
  11.     file,err := os.Open(path)
  12.     if err!=nil{
  13.      panic(err)
  14.     }
  15.     defer file.Close()
  16.     bufReader := bufio.NewReader(file)
  17.     buf := make([]byte,1024*1024*10)
  18.     for{
  19.      readNum,err := buffReader.Read(buf)
  20.      if err !=nil && err != io.EOF{
  21.      panic(err)
  22.      }
  23.      if readNum == 0{
  24.      fmt.Println("read file end...")
  25.      break
  26.      }
  27.     }
  28. }
  29. func putHttp(url,file,username,userpasswd string) int{
  30.     
  31.     client := &http.Clien{}
  32.     fileinfo:=readSmallFile(file)
  33.     req,err := http.NewRequest("PUT",url,strings.NewReader(fileinfo))
  34.     if nil{
  35.      panic(err)
  36.      return 0
  37.     }

  38.     //req.SetBasicAuth(username,userpasswd)
  39.     req.Header.Set("Authoriaztion","Bearer "+userpasswd)
  40.     req.Header.Set("Content-Type","text/html;charset=UTF-8")
  41.     resp,err := client.Do(req)
  42.     if err!=nil{
  43.      painc(err)
  44.      return 0
  45.     }
  46.     body,err := ioutil.ReadAll(resp.Body)
  47.     if err!=nil{
  48.      panic(err)
  49.      return 0
  50.     }
  51.     defer resp.Body.Close()
  52.     fmt.Println(string(body))
  53.     fmt.Println("statuscode==",resp.StatusCode)

  54.     return 1

  55. }

阅读(3943) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~