问题
I've been working on renovating a very old Java Servlet based webapp. The webapp lets users upload images. The webapp stores a path reference to the images in the database, but uploads the images to a directory on the server where they stay.
The database for the webapp was upgraded to Oracle 11g which I am told can handle binary information.
So, my question is, which is the smarter thing to do?
Stay with storing the images as files on the server or stuff the images into the Oracle 11g database?
Thanks much in advance
Steve
回答1:
Yes, you can use BLOBs for binary data in Oracle. (And many other databases.) We use them here to store PDF files.
I've seen it done either / both ways. Store the images in the database, or store the images in the file system and then save the file name in the database.
Advantages to Storing In Database
It's all right there in the database. You pull out a record, the associated image is right there with it.
You get data integrity between the file and its associated data (assumes a good schema design, of course.)
One logical operation to get data and image.
I find it harder and more expensive to scale a database to hold a lot of binary data than the file system alternative.
Disadvantages to Storing in Database
Not all tools will make it easy to browse the images.
Can't easily access the images from the web.
Depending on file size and quantity, you can end up with a lot of raw data in some of your tables.
Loading and storing blob data isn't always straight forward. (I recently wrote a custom data type for Hibernate, for example, to handle our blobbed PDFs.)
Advantages to File System
Probably more efficient for reading and writing the files.
If you want to serve the image up on the web, you can do that right from the file system.
Easier to browse images during debugging, and lots of free tools to do this.
I believe it to be easier to scale an application when putting blobs in the file system.
Disadvantages to File System
Data integrity / consistency issues. Change something about the file structure, and you have another step to do in the database.
It's another thing to back up.
Multiple logical operations (read from database, read from file system) to obtain image and data.
回答2:
An idea is to store images in the server and keep a reference to the actual location of the image. That way you can assign more meta to the picture, such as name, size, format, etc for easy query.
回答3:
Store on the server, then have data for the images in the database.
I.e. you can extract EXIF data from the image at upload time and then store them together with a filesystem reference to the image.
来源:https://stackoverflow.com/questions/11268675/storing-uploaded-images-should-it-be-stored-on-the-server-or-in-the-database