点击(此处)折叠或打开

  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. }

10-01 20:36
查看更多