zoukankan      html  css  js  c++  java
  • get.go

    package api

    import (
        "net/http"
        "fmt"
        "io/ioutil"
    )

    const bufferSize = 512 * 1024
    //获取空间文件
    func Get(host string, port int, vid uint64, fid uint64, filename string) ([]byte, error) {
        url := fmt.Sprintf("http://%s:%d/%d/%d/%s", host, port, vid, fid, filename)
        resp, err := http.Get(url)
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()

        if resp.StatusCode == http.StatusOK {
            return ioutil.ReadAll(resp.Body)
        }else {
            return nil, fmt.Errorf("%d != 200", resp.StatusCode)
        }
    }
    //获取空间文件   指定字节区间
    func GetRange(host string, port int, vid uint64, fid uint64, filename string, start int, length int) ([]byte, error) {
        url := fmt.Sprintf("http://%s:%d/%d/%d/%s", host, port, vid, fid, filename)
        req, _ := http.NewRequest(http.MethodGet, url, nil)
        req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, start + length - 1))
        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            return nil, err
        }
        defer resp.Body.Close()

        if resp.StatusCode == http.StatusPartialContent {
            return ioutil.ReadAll(resp.Body)
        }else {
            return nil, fmt.Errorf("%d != 200", resp.StatusCode)
        }
    }

  • 相关阅读:
    有道
    excel 数据入库
    iso-8859-1 Unicode 编码
    爬虫编码问题
    WIKi 百科爬虫
    降低耦合性获取微博数据
    Python基础总结3-字符串
    Python基础总结2
    Linux常用命令04(其他命令)
    Linux常用命令03(系统信息)
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7461602.html
Copyright © 2011-2022 走看看