如何在 Grails 上公开目录?

发布于 2024-09-17 01:27:38 字数 227 浏览 1 评论 0原文

我的 Grails 应用程序在文件夹中生成文件(例如“输出”)。 我怎样才能公开该文件夹,以便通过 URL 公开输出文件,例如:

http://localhost:8080/MyGrailsApp/output/myOutputFile1.xml
http://localhost:8080/MyGrailsApp/output/myOutputFile2.xml

干杯!

my Grails app generates files in a folder (e.g. "output").
How can I make that folder public, in order to expose the output files through URLs like:

http://localhost:8080/MyGrailsApp/output/myOutputFile1.xml
http://localhost:8080/MyGrailsApp/output/myOutputFile2.xml

Cheers!

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

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

发布评论

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

评论(2

青朷 2024-09-24 01:27:41

有两种方法可以做到这一点。首先是阅读有关应用程序服务器的文档,并在存储 xml 文件的任何目录上启用目录列表。如果您需要与应用程序服务器无关,第二个选项可能是创建一个简单的控制器,该控制器使用 URL 映射来自动加载并返回所请求的文件。有关 grails 中 URL 映射的文档和示例,请参阅
http://www.grails.org/URL+mapping

Two ways you could do this. First is read the documentation for whatever your application server is and enable directory listing on whatever directory your xml files are stored in. If you need to be application server agnostic the second option could be to create a simple controller that uses URL mapping to automatically load and return the requested file. For documentation and examples of URL mapping in grails see
http://www.grails.org/URL+mapping

↙温凉少女 2024-09-24 01:27:41

这段代码并不完美或经过测试,只是在此处输入它,但它应该让您朝着正确的方向前进

创建一个名为 OutputController.groovy 的控制器

def viewFile = {

    // add checks to ensure fileName parameter has no "/" or ".." to
    // prevent directory transversal

    def file = new File(OUTPUT_FILE_PATH + params?.fileName)

    if (file.exists()) {
        render(text: file.newInputStream().text, contentType: "text/plain",
                       encoding: "UTF-8")
    } else {
        render "FILE NOT FOUND: ${params?.fileName}"
    }
}

更新 url 映射文件

mappings {
  "/output/$fileName?" {
      controller = "output"
      action = "viewFile"
  }
}

This code is not perfect or tested, just typed it in here, but it should get you headed in the right direction

create a controller called OutputController.groovy

def viewFile = {

    // add checks to ensure fileName parameter has no "/" or ".." to
    // prevent directory transversal

    def file = new File(OUTPUT_FILE_PATH + params?.fileName)

    if (file.exists()) {
        render(text: file.newInputStream().text, contentType: "text/plain",
                       encoding: "UTF-8")
    } else {
        render "FILE NOT FOUND: ${params?.fileName}"
    }
}

update url mappings file

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