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

  • 相关阅读:
    品鉴-宋词
    【转载】全球水质最棒的十大景点
    Python文档管理与格式化工具
    Python音频处理
    Python剪切板提取、截图、图片粘贴,操作汇总
    Python多进程
    Wifi配置
    条码生成与解析
    谎言: “太空能看到的惟一的人工痕迹,长城!”
    VNC-Server安装配置详解
  • 原文地址:https://www.cnblogs.com/zhangboyu/p/7461602.html
Copyright © 2011-2022 走看看