【问题标题】:Where do I store Excel Template resources for a Grails Application我在哪里存储 Grails 应用程序的 Excel 模板资源
【发布时间】:2014-08-21 04:08:52
【问题描述】:
我正在尝试通过来自控制器的字符串 URL 访问 Excel 模板。
- 我应该将我的 Excel 模板放在我的 Grails 项目中的哪个目录? (即 WEB-INF、grails-app/assets 等)
- 将其打包为 .war 并将其放入 Tomcat 7 容器中后的 URI 是什么??
编辑
我正在使用
def wb = new WebXlsxExporter("/path/to/excel/template")
这需要文件存储位置的字符串 URL。
【问题讨论】:
标签:
tomcat
grails
resources
【解决方案1】:
所以我找到了答案... ExcelTemplates 文件夹位于 web-app 文件夹中。为了在没有收到文件未找到错误的情况下获取 URL,我使用了
request.getSession().getServletContext().getRealPath(.....)
---------
//Which looks like this when implemented
def wb = new WebXlsxExporter(request.getSession().getServletContext().getRealPath("/ExcelTemplates/testTemplate.xlsx"))
当编译成war并放在Tomcat 7 /webapp目录下时,一切正常!
【解决方案2】:
您可以将它们放在 web-app 目录下。然后您可以使用以下代码作为 InputStream 来获取它们。
private byte[] getLogoBytes() {
InputStream logo = servletContext.getResourceAsStream('/images/logo.png')
return logo.getBytes()
}