English 中文(简体)
查阅我圣杯应用软件的 本地存储文件吗?
原标题:Access a locally stored file from my Grails app?

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

我不确定是否最好让用户将文件名输入文本字段(如果有用户错误),然后将其存储在数据库中,这样我就可以将它链接到路径上,如:

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

但这似乎行不通,因为找不到文件,我发现404个错误

另一个我想的是,如果有的话,让用户上传文件可能比较容易,因为可能并不总是有文件。我认为这样做可能更好,但我不知道我在哪里会保存文件。我会在网络应用程序目录上建立一个文件夹,并将所有文件储存在那里吗?

谢谢你能给我的任何投入

问题回答

资源指令与 Web apps root 相对, 所以我不认为它能访问到其它信息 。

我不确定您的意思, 可能不是总有一个服务器, 程序总是会运行一个服务器。 如果您指的是数据库服务器, 我猜这在技术上是正确的, 尽管您可以使用 H2 或者其他的模拟数据库, 至少也可以使用 H2 。

< a href=> http:// grails.org/doc/latest/ guide/theWebLayer.html#uploading Files" rel=“nofollow”>docs 在文件上传上有一个相当不错的章节。 您可以跟随这个栏目, 并将文件保存到您可以访问的任何目录 - 映射或网络或本地。 最好在网络应用程序之外这样做, 这样您在重新部署时不会覆盖它。 保存文件名和任何您需要向用户显示的描述 。

然后访问您可能需要在您的网络服务器(tomcat,等等)中设置某种别名(tomcat,等等) 。 因此,让我们假设您在 / var/ myWebApp/uploads 上存储了 docs, 然后您在网络服务器设置上创建一个别名, 这样 /uploads/ 指向 / var/myWebApp/uploads , 并把它们作为 http://www.myserver.com/uploads/uploaded.pdf 指向 , 并把它们作为 http://www. myserver. com/upload/upload.pdf

我不知道你对这个网站有什么安全顾虑 但如果你担心有人通过上传目录查看文件名,你可能会想混淆文件名, 并且不允许该目录显示为索引, 并列出其中的文件 。

" 强 " / " 强 " / " 强 " / " 强 " / " 强 " / " 强 "

十分基本的例子

//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()
}




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)