Compojure:访问文件系统
这是我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 clojure.java.io/resource 来访问资源,无论它们位于本地文件系统上,还是打包在 jar/war 中:
您可能不应该尝试从目录加载它们,因为当从 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: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).