bra*_*iel 5 configuration grails
我有一个简单的grails文件上传应用程序.
我正在使用transferTo将文件保存到文件系统.
要在我的控制器中获取基本路径,我正在使用
def basePath = System.properties['base.dir'] // HERE IS HOW I GET IT
println "Getting new file"
println "copying file to "+basePath+"/files"
def f = request.getFile('file')
def okcontents = ['application/zip','application/x-zip-compressed']
if (! okcontents.contains(f.getContentType())) {
flash.message = "File must be of a valid zip archive"
render(view:'create', model:[zone:create])
return;
}
if(!f.empty) {
f.transferTo( new File(basePath+"/files/"+zoneInstance.title+".zip") )
}
else
{
flash.message = 'file cannot be empty'
redirect(action:'upload')
}
println "Done getting new file"
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当部署到我的WAS 6.1服务器时,它始终为null.
为什么在运行dev但在WAS服务器上没有生产时它可以工作?我应该以不同的方式访问此信息吗?
谢谢j,
我找到了可能的最佳动态解决方案。一般来说,我从不喜欢将绝对路径编码到任何软件中。财产文件或没有。
那么它是如何完成的:
def basePath = grailsAttributes.getApplicationContext().getResource("/files/").getFile().toString()
Run Code Online (Sandbox Code Playgroud)
grailsAttributes 在任何控制器中都可用。
getResource(somerelative dir) 将查找 web-app 文件夹内的任何内容。
因此,例如在我的开发系统中,它将字符串输出到“C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\”,并将相对目录连接到末尾
就像我上面的例子中的 C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\files
我在 WAS 6.1 中尝试过,它在容器中工作没有问题。您必须 toString 它,否则它将尝试返回该对象。
穆加富加
| 归档时间: |
|
| 查看次数: |
8932 次 |
| 最近记录: |