Compojure:访问文件系统

编程入门 行业动态 更新时间:2024-10-24 04:40:23
本文介绍了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 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:访问文件系统

本文发布于:2023-10-31 18:44:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546951.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件系统   Compojure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!