go 断点续传
时间:2023-01-30 18:00:00
package main import ( "fmt" "os" "strconv" "io" ) func main() { //文件目录 srcFile:="E:\\企业级四层负荷均衡lVS\\L100 - 企业级开源四层负荷平衡解决方案-LVS(完整版) - 199元\\第1章 课程简介\\1-1 LVS导学视频.mp4" //复制目录 destFile:="E:\\cpoy\\LVS第一章.mp4" //临时目录 tempFile:=destFile "temp.txt" file1,_:=os.Open(srcFile) file2,_:=os.OpenFile(destFile,os.O_CREATE|os.O_WRONLY,os.ModePerm) file3,_:=os.OpenFile(tempFile,os.O_CREATE|os.O_RDWR,os.ModePerm) defer file1.Close() defer file2.Close() //1.阅读临时文件中的数据seek // Seek 设置文件指针偏移量 file3.Seek(0,io.SeekStart) bs:=make([]byte,100,100) // read 读取 len(bs) 字节 n1,err:=file3.Read(bs) fmt.Printf("读取字节大小%v\n",n1) countStr:=string(bs[:n1]) fmt.Printf("转换为字符串%v\n",countStr) //count,_:=strconv.Atoi(countStr) count,_:=strconv.ParseInt(countStr,10,64) fmt.Printf("转换为INT64%v\n",count) //2. 设置读,写的偏移量 file1.Seek(count,0) file2.Seek(count,0) data:=make([]byte,1024,1024) n2:=-1// 读取的数据量 n3:=-1//写数据量 total :=int(count)///阅读总量 for{ //3.读取数据 n2,err=file1.Read(data) if err ==io.EOF{ fmt.Println("复制文件。。") file3.Close() os.Remove(tempFile) break } ///将数据写入目标文件 n3,_=file2.Write(data[:n2]) total = n3 ///将复制总量存储在临时文件中 file3.Seek(0,io.SeekStart) file3.WriteString(strconv.Itoa(total)) //假装断电 if total>8000000{ panic("假装断电了。。。。,假装的。。。") } } }