如何使用 Oracle 目录别名和 BFILE 对象将上传的文件写入磁盘?

发布于 2024-07-27 02:43:47 字数 532 浏览 2 评论 0原文

我有一个java webapp,它需要通过http上传文件,然后将它们存储在服务器上。 这些文件需要与 Oracle 数据库中的特定记录相关联,因此数据库中将有一个表存储对关联记录的引用、对文件的引用以及其他描述性数据(例如标题等)。该表似乎是:

  • 将文件存储为 BLOB
  • 存储对文件的 BFILE 引用 存储
  • 包含文件路径的字符串

我们更愿意将文件存储在数据库外部,因此我们不会将其存储为 BLOB 。 DBA 表示他们的首选是将引用存储为 BFILE。 oracle.sql.BFILE 对象提供对 InputStream 的访问以读取文件,但没有明显的写入方法。

当对存储目录的唯一引用是 Oracle 目录别名时,将文件数据写回磁盘的最佳方法是什么?


我们认为简单的 java.io 是写入文件的最佳方式,这意味着存储目录必须可供 Web 应用程序服务器作为挂载使用。 由于该目录无论如何都可供 Web 应用程序使用,因此我们决定不需要 BFILE,只需将文件名存储在数据库中即可。

I have a java webapp which needs to upload files via http and then store them on the server. The files need to be associated with specific records in an Oracle database, so there will be a table in the database storing the reference to the associated record, and a reference to the file, along with other descriptive data such as title etc. The options for this table appear to be:

  • store the file as a BLOB
  • store a BFILE reference to the file
  • store a String containing the path to the file

We would prefer to store the file outside of the database, so we will not store it as a BLOB. The DBAs have indicated that their preferred option is to store the reference as a BFILE. The oracle.sql.BFILE object provides access to an InputStream for reading the file, but no obvious way of writing to it.

What is the best way of writing the file data back to disk when the only reference to the storage directory is the Oracle directory alias?


We decided that simple java.io was the best way to write to the file, which means that the storage directory has to be available to the web application servers as a mount. Since the directory was available to the webapp anyway, we then decided that the BFILE was not required and just to store the filename in the database instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-08-03 02:43:47

根据 Oracle JDBC 开发人员指南 - 14.6 使用 BFILE

  • BFILE 是只读的。 您无法插入数据或以其他方式写入 BFILE。
  • 您不能使用 JDBC 创建新的 BFILE。 它们仅在外部创建。

因此,您似乎需要一个单独的工具/方法来将数据上传到 Oracle 存储目录,这可能使 BFILE 对于您的场景来说是一个糟糕的选择。

According to the Oracle JDBC Developers Guide - 14.6 Working with BFILEs:

  • BFILEs are read-only. You cannot insert data or otherwise write to a BFILE.
  • You cannot use JDBC to create a new BFILE. They are created only externally.

So it seems that you need a separate tool/method to upload the data to the Oracle storage directory, which probably makes BFILE a bad choice for your scenario.

我爱人 2024-08-03 02:43:47

“我们更愿意将文件存储在数据库之外”

为什么? 您对此的备份/恢复方案是什么? 例如,如果发生磁盘故障,您将如何恢复文件? 这些文件是否纯粹是暂时的,因此您实际上不需要保留它们?

基本上,BFILE 是 BLOB 存储和简单存储数据库中的路径之间的折衷方案。 您可以按照与传统文件相同的方式写入它,然后将其“可供”数据库读取。 如果您的网络应用程序与数据库运行在不同的物理服务器上,则您需要有一个两个盒子都可以访问的存储位置(以及适当的读/写权限)。

"We would prefer to store the file outside of the database"

Why ? What is your backup/recovery scenarios for this. For example, in the event of a disk failure, how would you recover the file ? Are the files purely transitory, so you don't actually need to preserve them ?

Basically, the BFILE is a compromise between BLOB storage and simply storing a path in the database. You write to it in the same manner as you would a conventional file, then make it 'available' to the database for reading. If your web-app is running on a different physical server from the database, you'd need to have a storage location that is accessible to both boxes (and appropriate permissions for read/write).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文