问题描述
我有一个简单的grails文件上传应用程序。
我使用transferTo将文件保存到文件系统。
要使用控制器中的基本路径,我使用
def basePath = System.properties ['base.dir'] //这是我如何获取
println获取新文件
println将文件复制到+ basePath +/ files
def f = request.getFile('file')
def okcontents = ['application / zip','application / x-zip-compressed']
if(!okcontents.contains (f.getContentType())){
flash.message =文件必须是有效的zip存档
render(view:'create',model:[zone:create])
return;
}
if(!f.empty){
f.transferTo(new File(basePath +/ files /+ zoneInstance.title +。zip))
}
else
{
flash.message ='file不能为空'
redirect(action:'upload')
}
println
由于某种原因,当部署到我的WAS 6.1服务器时,它总是为null。
为什么在WAS服务器上运行dev而不在prod中运行?我应该以不同的方式访问此信息吗?
感谢j,
我发现了最好的动态解决方案。作为一个规则,我从来不喜欢将绝对路径编码到任何软件。
下面是如何完成的:
def basePath = grailsAttributes.getApplicationContext()。getResource(/ files /)。getFile()。toString()
b $ b
grailsAttributes可在任何控制器中使用。
getResource(一些相对dir)会寻找web应用程序文件夹中的任何内容。
因此,例如在我的开发系统中,它将toString出来到C:\ WORKSPACEFOLDER\PROJECTFOLDER\web-app\,相对dir结尾
像上面的例子中那样
C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\files
我在WAS 6.1中试过,它在容器中没有问题。
你必须toString它或它会尝试返回对象。
mugafuga
I have a simple grails file upload app.
I am using transferTo to save the file to the file system.
To get the base path in my controller I am using
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"
For some reason this is always null when deployed to my WAS 6.1 server.
Why does it work when running dev but not in prod on the WAS server? Should I be accessing this information in a different way?
Thanks j,
I found the best dynamic solution possible. As a rule I never like to code absolute paths into any piece of software. Property file or no.
So here is how it is done:
def basePath = grailsAttributes.getApplicationContext().getResource("/files/").getFile().toString()
grailsAttributes is available in any controller.
getResource(some relative dir) will look for anything inside of the web-app folder.
So for example in my dev system it will toString out to "C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\ with the relative dir concated to the end
like so in my example aboveC:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\files
I tried it in WAS 6.1 and it worked in the container no problems.You have to toString it or it will try to return the object.
mugafuga
这篇关于grails base.dir系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!