Example of a method to download a web image or file in the Go language
- 2020-06-23 00:35:36
- OfStack
Recently, be free and at leisure, so I learned the basic usage of Go. Since practice is the fastest way to learn, here's how to learn Go by downloading web images or files
When a file is downloaded locally, the general idea is to get the input stream of the network file and the output stream of the local file first, and then read the input stream into the output stream. Therefore, the corresponding Reader and Writer should also be obtained naturally.
The first is the ES11en.Get () method using GoLang (the do method using client works similarly)
// Take the picture of the petal mesh as an example
imgUrl := "http://hbimg.b0.upaiyun.com/32f065b3afb3fb36b75a5cbc90051b1050e1e6b6e199-Ml6q9F_fw320"
res, err := http.Get(imgUrl)
if err != nil {
fmt.Println("A error occurred!")
return
}
// defer The latter is a delayed operation, usually used to release related variables
defer res.Body.Close()
Then get the reader object for the get request response
// To obtain get Request-Responsive reader object
reader := bufio.NewReaderSize(res.Body, 32 * 1024)
Above we get the reader object for the input stream, below we get the writer object for the output stream of the local file
imgPath := "C:\\Users\\Asche\\go\\src\\GoSpiderTest\\"
// According to the pictures url Get its file name
fileName := path.Base(imgUrl)
file, err := os.Create(imgPath + fileName)
if err != nil {
panic(err)
}
// Document-Acquiring writer object
writer := bufio.NewWriter(file)
Ok, both the reader and writer objects are taken, and then read and write.
If it's too much trouble, just copy:
written, _ := io.Copy(writer, reader)
// Output file size in bytes
fmt.Printf("Total length: %d", written)
Or read and write manually
bytes := make([]byte, 32 * 1024)
for {
len, err := reader.Read(bytes)
if len < 0 || err != nil{
return
}
// Note that there byte After an array of [0:len] Otherwise, you may be writing redundant data
_, _ = writer.Write(bytes[0:len])
fmt.Printf("%d ", len)
}
ok, download complete.
Post the complete code below (method 1 for reading and writing)
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"path"
)
func main() {
imgPath := "C:\\Users\\Asche\\go\\src\\GoSpiderTest\\"
imgUrl := "http://hbimg.b0.upaiyun.com/32f065b3afb3fb36b75a5cbc90051b1050e1e6b6e199-Ml6q9F_fw320"
fileName := path.Base(imgUrl)
res, err := http.Get(imgUrl)
if err != nil {
fmt.Println("A error occurred!")
return
}
defer res.Body.Close()
// To obtain get Request-Responsive reader object
reader := bufio.NewReaderSize(res.Body, 32 * 1024)
file, err := os.Create(imgPath + fileName)
if err != nil {
panic(err)
}
// Document-Acquiring writer object
writer := bufio.NewWriter(file)
written, _ := io.Copy(writer, reader)
fmt.Printf("Total length: %d", written)
}