问题描述
我希望用户能够在 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应用程序的绝对路径部署,以便我可以将我的代码更改为: 注意 这将在本地和部署的战争中发挥作用: 在。 I want the user to be able to download a file in my I've built an action like this: However when I pass in a file path Question How can I get the absolute path where the grails application is deployed so that I can change my code to be like: Note This will work locally and in the deployed war: Found it in a mail user thread. 这篇关于如何获得Grails应用程序的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
def file = new File(absolutePath + params.filepath)
System.properties ['base.dir']
可在本地使用,但在部署到tomcat时不可使用。
class MyController implements org.springframework.context.ResourceLoaderAware {
ResourceLoader resourceLoader
def download(){
//org.springframework.core.io.Resource
def someImage = resourceLoader.getResource(/ images / someImage.png).getFile()
}
}
web-app/images
folderdef 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
}
}
/images/myimage.jpeg
it is not finding that image. def file = new File (absolutePath + params.filepath)
System.properties['base.dir']
works locally but not when deployed to tomcat. class MyController implements org.springframework.context.ResourceLoaderAware {
ResourceLoader resourceLoader
def download() {
//org.springframework.core.io.Resource
def someImage = resourceLoader.getResource("/images/someImage.png").getFile()
}
}