Compojure:访问文件系统

发布于 2024-12-11 06:47:11 字数 1507 浏览 0 评论 0原文

这是我的project.clj 文件:

(defproject org.github.pistacchio.deviantchecker "0.9.0"
  :description "A one page application for keeping track of changes on deviantart.com gallieries"
  :dependencies [[org.clojure/clojure "1.2.1"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [enlive  "1.0.0"]
                 [compojure "0.6.4"]
                 [ring/ring-jetty-adapter "1.0.0-beta2"]]
  :dev-dependencies [[lein-ring "0.4.6"]]
  :ring {:handler org.github.pistacchio.deviantchecker.core/app}
  :main org.github.pistacchio.deviantchecker.core)

这是我的路由:

(defroutes main-routes
  (GET "/" [] (get-home))
  (GET "/add" [d] (add-gallery d))
  (GET "/delete" [d] (delete-gallery d))
  (GET "/check" [d] (check-gallery d))
  (route/resources "/")
  (route/not-found "Page not found"))

我在 /resources/public 中有一些 Web 静态文件,我可以访问它们。在代码中,我还需要访问文件系统上位于 /resources/data/resources/tpl 上的一些文件。使用leinring服务器或lein run,以下调用工作正常,

(java.io.File. "resources/tpl/home.html")

但是当使用lein uberwar打包应用程序并在Tomcat下部署时,它失败了,我得到一个 FileNotFoundException。也许这是因为在 lein 中,当前工作目录是项目根目录,而在 Tomcat 中,它是 Tomcat 的 bin 目录。

例如,我有 /resources/data/data.dat 在战争中打包为 /data/data.dat 所以要么“resources/data/data.dat " 在 Tomcat 下不起作用或 "data/data.dat" 在开发中不起作用。

顺便问一下,在 Compojure 中管理这个问题的正确方法是什么?谢谢。

this is my project.clj file:

(defproject org.github.pistacchio.deviantchecker "0.9.0"
  :description "A one page application for keeping track of changes on deviantart.com gallieries"
  :dependencies [[org.clojure/clojure "1.2.1"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [enlive  "1.0.0"]
                 [compojure "0.6.4"]
                 [ring/ring-jetty-adapter "1.0.0-beta2"]]
  :dev-dependencies [[lein-ring "0.4.6"]]
  :ring {:handler org.github.pistacchio.deviantchecker.core/app}
  :main org.github.pistacchio.deviantchecker.core)

and this my routing:

(defroutes main-routes
  (GET "/" [] (get-home))
  (GET "/add" [d] (add-gallery d))
  (GET "/delete" [d] (delete-gallery d))
  (GET "/check" [d] (check-gallery d))
  (route/resources "/")
  (route/not-found "Page not found"))

I have some web static files in /resources/public and I can access them. In the code I also need o access some files on the file system that are located on /resources/data and /resources/tpl. Using lein ring server or lein run, the following call works fine

(java.io.File. "resources/tpl/home.html")

but when packing the application with lein uberwar and deploying under Tomcat it fails and I get a FileNotFoundException. Maybe this is because with lein the current working directory is the project root while under Tomcat it is Tomcat's bin directory.

For example, I have /resources/data/data.dat that gets packed in the war as /data/data.dat so either "resources/data/data.dat" doesn't work under Tomcat or "data/data.dat" doesn't work in development.

By the way, what is the proper way of managing this in Compojure? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不顾 2024-12-18 06:47:11

您可以使用 clojure.java.io/resource 来访问资源,无论它们位于本地文件系统上,还是打包在 jar/war 中:

(require '[clojure.java.io :as io])
(io/reader (io/resource "public/some/file.txt")) ; file in resource classpath or $root/resources/public...

您可能不应该尝试从目录加载它们,因为当从 jar/war 部署文件时,您无法确定文件最终会在哪里(或者即使它可能位于文件系统上)。

You can use clojure.java.io/resource to access resources whether they're on the local file system, or packed in a jar/war:

(require '[clojure.java.io :as io])
(io/reader (io/resource "public/some/file.txt")) ; file in resource classpath or $root/resources/public...

You probably shouldn't try to load them from a directory, since you can't be sure where the file is going to end up when its deployed from a jar/war (or even if it's on the file system at all, probably).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文