如何使用drakma下载和保存文件:http-request和flexistreams

时间:2012-09-26 17:30:09

标签: common-lisp binary-data

我正在尝试下载并保存PDF,但在使用EOF-Error写入时失败。 这样做的正确方法是什么?

(with-open-file (file "/home/*/test.pdf"
                      :direction :io
                      :if-does-not-exist :create
                      :if-exists :supersede
                      :element-type '(unsigned-byte 8))
  (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                    :want-stream t)))
    (awhile (read-byte input)
      (write-byte it file))
    (close input)))

3 个答案:

答案 0 :(得分:7)

解决方案是我忘了使用read-byte的两个可选参数。

正确的方法是将eof-error-peof-value设置为nil

(with-open-file (file "/home/*/test.pdf"
                      :direction :output
                      :if-does-not-exist :create
                      :if-exists :supersede
                      :element-type '(unsigned-byte 8))
  (let ((input (drakma:http-request "http://www.fractalconcept.com/ex.pdf"
                                    :want-stream t)))
    (awhile (read-byte input nil nil)
      (write-byte it file))
    (close input)))

答案 1 :(得分:1)

AWHILE包含在ARNESI包中。

答案 2 :(得分:1)

(ql:quickload "trivial-download")
(trivial-download:download URL FILE)

几乎完全是你的代码所做的,但是在更大的块中,并且可以显示进度条。

可以在http://QuickLisp.org找到QuickLisp。