Compojure:访问文件系统 [英] Compojure: access filesystem

查看:128
本文介绍了Compojure:访问文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的project.clj档案:

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)

这个我的路由:

(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 中有一些网络静态文件,我可以访问它们。在代码中我还需要访问位于 / resources / data / resources / tpl 。使用 lein ring 服务器或 lein run ,以下调用工作正常

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")

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

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.

/resources/data/data.dat 在战争中包装为 /data/data.dat resources / data / data.dat在Tomcat下不起作用,或者data / data.dat在开发中不起作用。

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.

在Compojure管理这个正确的方法?

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

推荐答案

您可以使用 clojure.java.io/resource 访问资源,无论它们是在本地文件系统还是包装在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...

您可能不应尝试从目录中加载它们,因为你不能确定文件从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).

这篇关于Compojure:访问文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆