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)
}
}
随机推荐
- palindrome number(回文数)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 新型USB病毒BadUSB 即使U盘被格式化也无法根除
这种病毒并不存在于USB设备中的存储文件中,而是根植于USB设备的固件里.这意味着,即使用户对U盘进行全面的格式化清理,仍不能"杀死"它.
- Android之Drawable
Android 中图片和常见的颜色都可以是一个Drawable. Drawable可以方便我们做出一些特殊的UI效果,这一点在UI相关的开发工作中尤其重要.其主要优点有: 使用简单,比自定义View成 ...
- GlitchBot -HZNU寒假集训
One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...
- ZAB协议(Zookeeper atomic Broadcast)
一.简语: ZAB协议是Paxos算法的经典实现 二.ZAB协议的两种模式: 崩溃恢复: 1.每个server都有一张选票(myid,zxid),选票投给自己 2.收集所有server的投票 3.比较 ...
- RDC去省赛玩前の日常训练 Chapter 1
4/3 技能点 A. 生成树的计数 论文:周冬<生成树的计数及其应用>(看不懂 pending) 一个栗子:Codeforces 719D 两个做法 Matrix-Tree + 高斯消元, ...
- 给你的网页添加一个随机的BGM
大晚上的,突然想到,我这么喜欢听歌的人,博客里怎么能少了BGM呢,说干就干. 首先,给博客侧边栏加一个空div:<div id="music"></div> ...
- Windows下的OpenCVSharp配置
OPenCvSharp是OpenCV的Net Warpper,应用最新的OpenCV库开发,目前放在github.. 本人认为OpenCvSharp比EmguCV使用起来更为方便,因为函数更接近于原生 ...
- threesum
算法题 问题描述:在一些给定的数中,找到三个数,他们相加的和是0,并且这三个数的组合是不能重复的 例子: input [-1, 0, -1, 2, 1] Output [[-1, 1 ,0], [-1 ...
- 微信小程序-框架详解(1)
配置 -app.json文件对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.tab等 { "pages": [ //决定页面文件的路径 "pag ...