如何在Common Lisp/dexador中从http地址下载并保存图像?
如何从网站下载图像并将其保存到Common Lisp中的软件包文件夹?我很难在dexador的文档中找到这样的函数如何在Common Lisp/dexador中从http地址下载并保存图像?,http,common-lisp,Http,Common Lisp,如何从网站下载图像并将其保存到Common Lisp中的软件包文件夹?我很难在dexador的文档中找到这样的函数 提前感谢您得到了一个字节向量,所以只需保存它: (let ((bytes (dex:get uri))) (with-open-file (out filename :direction :output :if-exists :supersede
提前感谢您得到了一个字节向量,所以只需保存它:
(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))))
这是斯万特回答的第一版稍作改进:
(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都是简化这些任务的小助手的集合。这将是dexador的第一个伟大贡献:]如果使用
Alexandria:write byte vector to file
,第一种情况会更容易。