This commit is contained in:
ftpgxm 2021-05-18 14:00:30 +08:00
parent 82ce7ad073
commit 14f33d86a6
2 changed files with 17 additions and 8 deletions

View File

@ -124,7 +124,7 @@ func Download(filePath string, client transportpb.FileServiceClient) (err error)
} }
for i := len(downloadContext.tempList) - 1; i >= 0; i-- { for i := len(downloadContext.tempList) - 1; i >= 0; i-- {
err = appendToFile(downloadContext.file.filePath, string(readFile(downloadContext.tempList[i]))) err = appendToFile(downloadContext.file.filePath, readFile(downloadContext.tempList[i]))
if err != nil { if err != nil {
log.Println(err.Error(), "下载失败,请重试") log.Println(err.Error(), "下载失败,请重试")
return return
@ -161,11 +161,11 @@ func startDownloadTask(filePath string, tempFilePath string, b *block, client tr
log.Fatalf("下载异常 : %v\n", err) log.Fatalf("下载异常 : %v\n", err)
} }
fp, err := os.OpenFile(tempFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0755) err = createFileOnly(tempFilePath)
if err != nil { if err != nil {
log.Fatalf("文件打开异常: %s\n", err) log.Println(err.Error())
panic(err)
} }
defer fp.Close()
var recvSize int64 = 0 var recvSize int64 = 0
@ -180,7 +180,7 @@ func startDownloadTask(filePath string, tempFilePath string, b *block, client tr
recvSize += int64(blockSize) recvSize += int64(blockSize)
if blockSize != 0 { if blockSize != 0 {
_, err = fp.Write(block) err := appendToFile(tempFilePath, block)
if err != nil { if err != nil {
log.Fatalf("临时文件保存异常: %s\n", err) log.Fatalf("临时文件保存异常: %s\n", err)
} }
@ -410,7 +410,7 @@ func checkBlockStat(filePath string, b *block) bool {
return false return false
} }
func appendToFile(fileName string, content string) error { func appendToFile(fileName string, content []byte) error {
// 以只写的模式,打开文件 // 以只写的模式,打开文件
f, err := os.OpenFile(fileName, os.O_WRONLY, 0644) f, err := os.OpenFile(fileName, os.O_WRONLY, 0644)
if err != nil { if err != nil {
@ -419,7 +419,7 @@ func appendToFile(fileName string, content string) error {
// 查找文件末尾的偏移量 // 查找文件末尾的偏移量
n, _ := f.Seek(0, os.SEEK_END) n, _ := f.Seek(0, os.SEEK_END)
// 从末尾的偏移量开始写入内容 // 从末尾的偏移量开始写入内容
_, err = f.WriteAt([]byte(content), n) _, err = f.WriteAt(content, n)
} }
defer f.Close() defer f.Close()
return err return err

View File

@ -1 +1,10 @@
protoc -I=proto --go_out=plugins=grpc:. ./proto/*.proto
## 下载地址
```
https://github.com/google/protobuf
```
## 生成
```
protoc -I=proto --go_out=plugins=grpc:./dist/ ./proto/*.proto
```