如何从网站下载图像并将其保存到Common Lisp中的包文件夹中?我很难在dexador的文档中找到这样的函数。
提前感谢
您得到一个字节向量,所以只需保存它:
(let ((bytes (dex:get uri)))
(with-open-file (out filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create
:element-type 'unsigned-byte)
(write-sequence bytes out)))
如果你有太多的数据,你可能想使用缓冲流拷贝:
(let ((byte-stream (dex:get uri :want-stream t))
(buffer (make-array buffer-size :element-type 'unsigned-byte)))
(with-open-file (out filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create
:element-type 'unsigned-byte)
(loop :for p := (read-sequence buffer byte-stream)
:while (plusp p)
:do (write-sequence buffer out :end p))))
这是对Svante的答案的第一个版本的略微改进
(alexandria:write-byte-vector-into-file (dex:get "https://httpbin.org/image/png")
#P"/tmp/myfile"
:if-exists :supersede)
这是他的第二个版本的稍微简化的版本:
(serapeum:write-stream-into-file (dex:get "https://httpbin.org/image/png"
:want-stream t)
#P"/tmp/myfile"
:if-exists :supersede)
Alexandria和Serapeum都是简化此类任务的小助手。