将图片从网址保存到文件 [英] Save an image from url to file
问题描述
问题:如何从URL获取图像并将其保存到我的计算机
以下是我到目前为止:
package main
进口(
fmt
净/ http
图片
io / ioutil
)
func main(){
url:=http://i.imgur.com/m1UIjW1.jpg
//不要担心错误
响应,_ := http.Get(url);
推迟response.Body.Close()
m,_,err:= image.Decode(response.Body)
error:= ioutil.WriteFile( /images/asdf.jpg,m,0644)
}
然而,当我运行这段代码,我得到的不能在函数参数中使用m(type image.Image)作为类型[] byte
我假设我必须将image.Image(变量 m
)转换为未定义的字节数?这是否正确的方式来解决这个问题?
没有必要解码文件。只需将响应主体复制到您打开的文件。这是修改后的例子中的交易:
-
response.Body
数据,并实现Reader
接口 - 这意味着您可以依次调用Read
,就好像它是一个打开的文件。 - 我在这里打开的文件实现了
Writer
界面。这恰恰相反 - 这是一个可以调用Write
的流。 -
io.Copy
修补读写器,消耗读写器流并将其内容写入写入器。
这是我最喜欢的go - implicit接口之一。你不必声明你正在实现一个接口,你只需要实现它来在某些情况下使用。这允许混合和匹配不需要知道其与其相互作用的其他代码的代码。
软件包主要
import(
fmt
io
log
net / http
os
)
func main(){
url:=http://i.imgur.com/m1UIjW1.jpg
// do not担心错误
响应,e:= http.Get(url)
if e!= nil {
log.Fatal(e)
}
推迟response.Body.Close()
//打开文件写入
文件,err:= os.Create(/ tmp / asdf.jpg)
如果err!= nil {
log.Fatal(err)
}
//使用io.Copy将响应主体转储到文件中。这支持大文件
_,err = io.Copy(file,response.Body)
if err!= nil {
log.Fatal(err)
}
file.Close()
fmt.Println(Success!)
}
Very new to Go (first simple project I'm working on).
Question: How do I get an image from URL and then save it to my computer?
Here's what I have so far:
package main
import (
"fmt"
"net/http"
"image"
"io/ioutil"
)
func main() {
url := "http://i.imgur.com/m1UIjW1.jpg"
// don't worry about errors
response, _ := http.Get(url);
defer response.Body.Close()
m, _, err := image.Decode(response.Body)
error := ioutil.WriteFile("/images/asdf.jpg", m, 0644)
}
However, when I run this code, I get cannot use m (type image.Image) as type []byte in function argument
I'm assuming I have to convert image.Image (variable m
) into an undefined amount of bytes? Is that the correct way to go about this?
There is no need to decode the file. Simply copy the response body to a file you've opened. Here's the deal in the modified example:
response.Body
is a stream of data, and implements theReader
interface - meaning you can sequentially callRead
on it, as if it was an open file.- The file I'm opening here implements the
Writer
interface. This is the opposite - it's a stream you can callWrite
on. io.Copy
"patches" a reader and a writer, consumes the reader stream and writes its contents to a Writer.
This is one of my favorite things about go - implicit interfaces. You don't have to declare you're implementing an interface, you just have to implement it to be used in some context. This allows mixing and matching of code that doesn't need to know about other code it's interacting with.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
url := "http://i.imgur.com/m1UIjW1.jpg"
// don't worry about errors
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
//open a file for writing
file, err := os.Create("/tmp/asdf.jpg")
if err != nil {
log.Fatal(err)
}
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
file.Close()
fmt.Println("Success!")
}
这篇关于将图片从网址保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!