如何使用Grails读取本地文件? [英] How to read local file with Grails?

查看:157
本文介绍了如何使用Grails读取本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的Grails应用程序启动时,我通过从远程URL下载的CSV文件构建数据结构.如果无法访问该文件,我想回退到本地副本.当前正在服务层中处理文件,是使用Quartz作业启动的.

When my Grails application starts, I build up a data structure from a CSV file downloaded from a remote URL. If the file is not accessible, I'd like to fall back to a local copy. Currently processing the file in the service layer, initiated using a Quartz job.

使用Groovy读取Grails中的本地资源的最佳实践是什么?

  • 我应该将文件存储在哪里?
  • 如何安全正确地读取文件?

一般情况下的答案将是可以接受的.

推荐答案

我认为处理此问题的最佳方法是将文件的位置存储在

I think the best way to deal with this is to store the file's location in an externalized configuration file.

因此,您将确定一个标准化位置(例如/etc/myappname/CSVFileConfig.groovy),或使用环境变量或类似名称传递配置文件路径.有关示例,请参见外部配置.

So, you'd determine a standardized location (such as /etc/myappname/CSVFileConfig.groovy), or pass the config file path in using an environment variable or something similar. See Externalized Configuration for examples.

然后,您可以简单地将本地文件的实际路径添加到该外部配置,如下所示:

Then you can simply add the actual path to the local file to that extenal config, like so:

// CSVFileConfig.groovy
my.custom.csv.path = ...

最后,使用常规配置操作访问它:

Finally, access it using normal config operations:

// in your Quartz job
def path = grailsApplication.config.my?.custom?.csv?.path
if(!path) {
    // no file to load
} else {
    // load file
}

就读取文件而言,您主要关注的是什么?如果您使用的是CSV库,例如OpenCSV(用于大多数用于Grails的Grails库中,),它将处理文件的打开和解析.

As far as reading the file, what are your primary concerns? If you are using a CSV library, such as OpenCSV (used in most of the Grails libraries for CSV parsing), it will handle the opening and parsing of the file.

对于超出此范围的安全性问题,我不确定如何以通用方式处理它们.这将取决于您的特定情况.我认为来自网址的风险因素更高.

For security issues beyond that, I'm not sure how to handle them in a generic way. It will depend on your specific scenario. I think the one coming from a URL has a higher risk factor.

这篇关于如何使用Grails读取本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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