HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Clojure的Ring Web应用程序中生成并流式传输zip文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个需要的Ring处理程序:

>压缩几个文件
>将Zip流式传输到客户端.

现在我有点工作,但只有第一个压缩条目流式传输,然后停止/停止.我觉得它与刷新/流媒体有关,这是错误的.

这是我的(compojurE)处理程序:

(GET "/zip" {:as request}
            :query-params [order-id   :- s/Any]
            (stream-lessons-zip (read-String order-id) (:db request) (:auth-user request)))

这是stream-lessons-zip函数:

(defn stream-lessons-zip
  []
  (let [lessons ...];... not shown

  {:status 200
   :headers {"Content-Type" "application/zip,application/octet-stream"
             "Content-Disposition" (str "attachment; filename=\"files.zip\"")
   :body (futil/zip-lessons lessons)}))

我使用管道输入流来进行流式处理:

(defn zip-lessons
 "Returns an inputstream (piped-input-stream) to be used directly in Ring http responses"
[lessons]
(let [paths (map #(SELEct-keys % [:file_path :file_name]) lessons)]
(ring-io/piped-input-stream
  (fn [output-stream]
    ; build a zip-output-stream from a normal output-stream
    (with-open [zip-output-stream (ZipOutputStream. output-stream)]
      (doseq [{:keys [file_path file_name] :as p} paths]
        (let [f (cio/file file_path)]
          (.putNextEntry zip-output-stream (ZipEntry. file_name)) 
          (cio/copy f zip-output-stream)
          (.closeEntry zip-output-stream))))))))

所以我确认’课程’向量包含4个条目,但zip文件只@R_495_11262@个条目.此外,Chrome似乎并未“完成”下载,即.它认为它仍在下载.

我怎样才能解决这个问题?

解决方法

听起来像http-kit不支持使用阻塞IO生成有状态流.非有状态流可以通过这种方式完成:

http://www.http-kit.org/server.html#async

不接受使用阻塞IO引入有状态流的PR:

https://github.com/http-kit/http-kit/pull/181

听起来,探索的选择是使用ByteArrayOutputStream将zip文件完全呈现到内存,然后返回生成的缓冲区.如果此端点没有高度流量,并且它生成的zip文件不大(<1 gb),那么这可能会起作用.

大佬总结

以上是大佬教程为你收集整理的在Clojure的Ring Web应用程序中生成并流式传输zip文件全部内容,希望文章能够帮你解决在Clojure的Ring Web应用程序中生成并流式传输zip文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。