从我的Grails应用程序访问本地存储的文件? [英] Access a locally stored file from my Grails app?

查看:163
本文介绍了从我的Grails应用程序访问本地存储的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让我的grails应用程序访问本地存储的pdf文件以供参考。我有一个绝对路径到本地服务器上的一个点,并且该应用程序将运行在同一个网络上。

我不知道是否最好让用户将文件名输入到文本字段(受用户错误影响),然后将其存储在数据库中,然后我可以创建一个链接,将它追加到路径中,如下所示:

 < a href =$ {resource(dir :'/ serverName / locationName / pdfs',file:'FILENAME_with_Ext.pdf')}target =_ new> pdfFile< / a> 

但这似乎不起作用,因为它不会找到该文件,而且我得到404错误

我在想的另一件事是,让用户上传文件可能更容易,如果有的话,因为可能并不总是有文件。我认为这可能是更好的方法,但我不知道我会在哪里存储文件。我将在Web应用程序目录中创建一个文件夹并将所有文件存储在那里?



感谢您提供的任何输入内容。

解决方案

资源指令是相对于web应用程序根目录的,所以我不认为它能够访问任何外部的任何内容

我不确定你的意思可能并不总是一个服务器 - 总是有一个应用程序运行的服务器。如果你的意思是数据库服务器,我认为这在技术上是正确的,尽管你至少可以使用H2或其他内存数据库。

docs 在文件上传方面有相当不错的部分。你可以关注它,并将它保存到你有权访问的任何目录 - 映射或网络或本地。可能最好在Web应用程序之外执行此操作,以便在重新部署时不会覆盖。保存文件名和任何描述你需要显示用户。



然后访问你可能需要在你的web服务器(tomcat等)中设置某种别名)。因此,假设您将文档存储在 / var / myWebApp / uploads 中,然后使用Web服务器设置创建别名,以使 / uploads / 指向 / var / myWebApp / uploads 并将它们作为提供http://www.myserver.com/uploads/uploaded .pdf



我不知道您对这个网站有什么安全隐患,但是如果您担心,您可能想要混淆文件名关于通过上传目录查看的人,并且不允许该目录将其自身显示为索引并列出其包含的文件。



编辑

非常基本的例子

  // class 
class MyClass {
String fileDescription
byte [] fileDocument
}

// form
说明< g:textField name =fileDescription/>
< input type =filename =fileDocument/>
< input type =submit
< / g:uploadForm>

//控制器
def save = {
def myClass = new MyClass(params)
myClass.save()
}


I need to have my grails application access a locally stored pdf file for reference purposes. I have an absolute path to a spot on a local server, and the app will be running on the same network.

I'm not sure if it is better to have the user input the filename into a text field (subject to user error) and then store it in the database, which I could then make a link to appending it to the path as in:

<a href="${resource(dir:'/serverName/locationName/pdfs',file:'FILENAME_with_Ext.pdf')}" target="_new">pdfFile</a>

but this doesn't seem to work, as it won't find the file and I get a 404 error

The other thing I was thinking is that it might be easier to have the user upload the file , if there is one, because there might not always be a file. I think this might be the better way to go, but I don't know where I would store the files. Would I make a folder in the Web Application directory and store all the files there?

Thanks for any input you can give me.

解决方案

The resource directive is relative to the web apps root, so i don't think it will be able to access anything outside that.

I'm not sure what you mean by there might not always be a server - there is always a server that the app runs on. If you mean database server I guess that is technically correct, although you could always use H2 or some other in-memory database at the very least.

The docs have a pretty good section on file uploads. You could follow that and have it save the files to any directory you have access to - mapped or network or local. Probably best to do it outside the web app so you don't overwrite when you redeploy. Save the filename and whatever description you need to show the users.

Then to access you will probably need to set up some sort of aliasing in your web server (tomcat, etc). So lets say you stored the docs at /var/myWebApp/uploads then you create an alias with the web server setup so that /uploads/ points to /var/myWebApp/uploads and serve them up as http://www.myserver.com/uploads/uploaded.pdf

I don't know what security concerns you have for this site but you might want to obfuscate the file names if you are worried about people looking through the uploads directory, and don't allow that directory to show itself as an index and list the files it contains.

EDIT

Very basic example

//class
class MyClass {
    String fileDescription
    byte[] fileDocument
}

//form
<g:uploadForm action="save">
    Description <g:textField name="fileDescription" />
    <input type="file" name="fileDocument" />
    <input type="submit" 
</g:uploadForm>

//controller
def save = {
    def myClass = new MyClass(params)
    myClass.save()
}

这篇关于从我的Grails应用程序访问本地存储的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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