diff --git a/docs/netutil.md b/docs/netutil.md index 90c3578..16892e3 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -44,6 +44,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [DownloadFile](#DownloadFile) +- [UploadFile](#UploadFile) - [IsPingConnected](#IsPingConnected) - [IsTelnetConnected](#IsTelnetConnected) @@ -899,6 +901,61 @@ func main() { } ``` +### DownloadFile + +

Download the file exist in url to a local file.

+ +Signature: + +```go +func DownloadFile(filepath string, url string) error +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") + + fmt.Println(err) +} +``` + +### UploadFile + +

Upload the file to a server.

+ +Signature: + +```go +func UploadFile(filepath string, server string) (bool, error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ok, err := netutil.UploadFile("./a.jpg", "http://www.xxx.com/bucket/test") + + fmt.Println(ok) + fmt.Println(err) +} +``` + ### IsPingConnected

checks if can ping the specified host or not.

@@ -915,10 +972,7 @@ func IsPingConnected(host string) bool package main import ( - "encoding/json" "fmt" - "io/ioutil" - "log" "github.com/duke-git/lancet/v2/netutil" ) @@ -951,10 +1005,7 @@ func IsTelnetConnected(host string, port string) bool package main import ( - "encoding/json" "fmt" - "io/ioutil" - "log" "github.com/duke-git/lancet/v2/netutil" ) diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index 62e8c23..cc16537 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -44,6 +44,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [DownloadFile](#DownloadFile) +- [UploadFile](#UploadFile) - [IsPingConnected](#IsPingConnected) - [IsTelnetConnected](#IsTelnetConnected) @@ -901,6 +903,61 @@ func main() { } ``` +### DownloadFile + +

从指定的server地址下载文件。

+ +函数签名: + +```go +func DownloadFile(filepath string, url string) error +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") + + fmt.Println(err) +} +``` + +### UploadFile + +

将文件上传指定的server地址。

+ +函数签名: + +```go +func UploadFile(filepath string, server string) (bool, error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ok, err := netutil.UploadFile("./a.jpg", "http://www.xxx.com/bucket/test") + + fmt.Println(ok) + fmt.Println(err) +} +``` + ### IsPingConnected

检查能否ping通主机。

@@ -917,10 +974,7 @@ func IsPingConnected(host string) bool package main import ( - "encoding/json" "fmt" - "io/ioutil" - "log" "github.com/duke-git/lancet/v2/netutil" ) @@ -953,10 +1007,7 @@ func IsTelnetConnected(host string, port string) bool package main import ( - "encoding/json" "fmt" - "io/ioutil" - "log" "github.com/duke-git/lancet/v2/netutil" ) diff --git a/netutil/net_test.go b/netutil/net_test.go index 309238f..7e374e4 100644 --- a/netutil/net_test.go +++ b/netutil/net_test.go @@ -110,6 +110,13 @@ func TestEncodeUrl(t *testing.T) { assert.Equal(expected, encodedUrl) } +// func TestDownloadFile(t *testing.T) { +// assert := internal.NewAssert(t, "TestDownloadFile") + +// err := DownloadFile("./lancet_logo.jpg", "https://picx.zhimg.com/v2-fc82a4199749de9cfb71e32e54f489d3_720w.jpg?source=172ae18b") +// assert.IsNil(err) +// } + func TestIsPingConnected(t *testing.T) { assert := internal.NewAssert(t, "TestIsPingConnected")