如何获得Grails应用程序的绝对路径 [英] How to get absolute path for a grails application

查看:172
本文介绍了如何获得Grails应用程序的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够在 web-app / images 文件夹中下载文件



我已经构建了这样的一个动作:

  def download(){
def file = new File(params。文件路径)

if(file.exists()){
response.setContentType(application / octet-stream)
response.setHeader(Content-disposition, filename = $ {file.name})
response.outputStream<< file.bytes
return
}
}

然而,当我传入一个文件路径 /images/myimage.jpeg 它没有找到该图像。

问题

如何获取grails应用程序的绝对路径部署,以便我可以将我的代码更改为:

  def file = new File(absolutePath + params.filepath)

注意

System.properties ['base.dir'] 可在本地使用,但在部署到tomcat时不可使用。

解决方案

这将在本地和部署的战争中发挥作用:

$ p> class MyController implements org.springframework.context.ResourceLoaderAware {
ResourceLoader resourceLoader
def download(){
//org.springframework.core.io.Resource
def someImage = resourceLoader.getResource(/ images / someImage.png).getFile()
}
}

邮件用户线程


I want the user to be able to download a file in my web-app/images folder

I've built an action like this:

def download () {
    def file = new File (params.filepath)

    if (file.exists()) {
        response.setContentType("application/octet-stream")
        response.setHeader("Content-disposition", "filename=${file.name}")
        response.outputStream << file.bytes
        return
    }
}

However when I pass in a file path /images/myimage.jpeg it is not finding that image.

Question

How can I get the absolute path where the grails application is deployed so that I can change my code to be like:

def file = new File (absolutePath + params.filepath)

Note

System.properties['base.dir'] works locally but not when deployed to tomcat.

解决方案

This will work locally and in the deployed war:

class MyController implements org.springframework.context.ResourceLoaderAware {
  ResourceLoader resourceLoader 
  def download() {
    //org.springframework.core.io.Resource
    def someImage = resourceLoader.getResource("/images/someImage.png").getFile()
  }
}

Found it in a mail user thread.

这篇关于如何获得Grails应用程序的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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