How to store a file in the database with Grails

2019-02-17 10:08发布

I need to store a file in the database using Grails. So, do you know how I can do that? Which data type should I use in the domain class (byte[] might be a solution)?

5条回答
女痞
2楼-- · 2019-02-17 10:23

I have had the same problem. All I needed to do was to go to the database (Oracle) and change the column type from RAW to Blob. (Actually I had to remove the column and then to create a new one). I was never able to find out how to use a "java.sql.Blob" as suggested above. I found this this (converting from bytes to Blob), but it seemed a bit complicated.

查看更多
淡お忘
3楼-- · 2019-02-17 10:34

alternatively, store the file on disk, and store the path to it in the database. it is generally faster to access that file on disk than in a db. but of course, that really just depends on your needs in the app. But be aware of this alternative.

查看更多
Ridiculous、
4楼-- · 2019-02-17 10:40

I recently used byte[] in a domain class for storing file, it was working fine until i moved my application from "HSQLDB" to "Oracle express". On Oracle, byte[] was mapped to a raw object instead of blob... (not sure of what was exactly wrong) and I finally solve the problem by using a java.sql.blob

查看更多
戒情不戒烟
5楼-- · 2019-02-17 10:41

See Chapter on Uploading Files in the Grails User Guide. It is also possible to use a java.sql.Blob as type for the binary content (which would be preferable when dealing with huge files).

查看更多
劳资没心,怎么记你
6楼-- · 2019-02-17 10:46

You can force the domain object to use a specific type when updating or generating the database with column mappings:

byte[] orgFile

static mapping = {
   columns { 
    orgFile type:'blob'
   }
}

This has worked for me so far ;)

查看更多
登录 后发表回答