|
|
|
@ -26,6 +26,20 @@ type ZipBlock struct { |
|
|
|
// FileTransferService 定义文件传输服务
|
|
|
|
type FileService struct{} |
|
|
|
|
|
|
|
// 客户端
|
|
|
|
type UpFileClient struct { |
|
|
|
rpcClient *rpc.Client |
|
|
|
} |
|
|
|
|
|
|
|
// 创建新的客户端
|
|
|
|
func NewUpFileClient(addr string) (*UpFileClient, error) { |
|
|
|
client, err := rpc.Dial("tcp", addr) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return &UpFileClient{rpcClient: client}, nil |
|
|
|
} |
|
|
|
|
|
|
|
// 压缩文件
|
|
|
|
// 返回压缩包的名称
|
|
|
|
func (f *FileService) Compress(args *ZipBlock, reply *string) error { |
|
|
|
@ -67,7 +81,7 @@ func (f *FileService) Compress(args *ZipBlock, reply *string) error { |
|
|
|
|
|
|
|
// 客户端调用RPC传送文件的函数
|
|
|
|
// 传入zip的实际路径,远程的服务器带端口,远程文件存放路径
|
|
|
|
func clientUploadFile(remote string, filePath string, uploadPath string) error { |
|
|
|
func clientUploadFile(remote string, filePath string, uploadPath string) { |
|
|
|
fmt.Printf("remote: %s, filePath: %s, uploadPath: %s\n", remote, filePath, uploadPath) |
|
|
|
// 启用日志
|
|
|
|
logger := util.NewProductionLogger() |
|
|
|
@ -79,14 +93,24 @@ func clientUploadFile(remote string, filePath string, uploadPath string) error { |
|
|
|
|
|
|
|
// 连接到RPC服务器
|
|
|
|
service := fmt.Sprintf("%v:%v", dstip, dport) |
|
|
|
client, err := rpc.Dial("tcp", service) |
|
|
|
client, err := NewUpFileClient(service) |
|
|
|
if err != nil { |
|
|
|
//panic(err)
|
|
|
|
fmt.Printf("连接到RPC服务器失败: %v", err) |
|
|
|
return err |
|
|
|
logger.Error("NewUpFileClient failed", zap.Error(err)) |
|
|
|
} |
|
|
|
// 调用 transferFile
|
|
|
|
err = client.TransferFile(filePath, uploadPath) |
|
|
|
if err != nil { |
|
|
|
logger.Error("TransferFile failed", zap.Error(err)) |
|
|
|
fmt.Printf("TransferFile failed: %v\n", err) |
|
|
|
return |
|
|
|
} |
|
|
|
defer client.Close() |
|
|
|
fmt.Printf("TransferFile success\n") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 传输文件
|
|
|
|
func (c *UpFileClient) TransferFile(filePath string, uploadPath string) error { |
|
|
|
// 发送文件信息
|
|
|
|
// 获取文件信息
|
|
|
|
fileInfo, err := os.Stat(filePath) |
|
|
|
if err != nil { |
|
|
|
@ -101,12 +125,11 @@ func clientUploadFile(remote string, filePath string, uploadPath string) error { |
|
|
|
fmt.Printf("fileName: %s\n", fileName) |
|
|
|
// 发送文件信息
|
|
|
|
var reply bool |
|
|
|
err = client.Call("FileService.SendFileInfo", FileInfo{ |
|
|
|
FileName: fileName, |
|
|
|
err = c.rpcClient.Call("UpFileService.SendFileInfo", FileInfo{ |
|
|
|
FileName: filePath, |
|
|
|
FileSize: fileInfo.Size(), |
|
|
|
}, &reply) |
|
|
|
if err != nil { |
|
|
|
logger.Error("发送文件信息失败", zap.Error(err)) |
|
|
|
fmt.Printf("发送文件信息失败: %v\n", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
|