存储在磁盘上的图像 - 按名称搜索

Images stored on disk - search by name

我正在编写一个 java 应用程序(实际上是一个 Struts2 网络应用程序),我需要存储和检索图像。我发现将图像存储在数据库中不是一个好主意 (Can I store images in MySQL),最好将它们写入磁盘的目录中。

有时我需要访问名称中包含特定子字符串的所有图像。如果我使用数据库,我会写这样的查询:select .. where name like %my_substring%。如何使用 java 文件系统实现相同的功能?当然,我不想加载所有图像,然后迭代它们以找到具有适当名称的图像。

只需用索引文件处理即可。每次在光盘上存储图像时,其名称也会写入索引文件中。当您搜索子字符串时,只需搜索索引并使用索引中的名称加载文件

正如您链接的问题中的标记答案所述:

Do not store images in the database. Store images in directories and store references to the images in the database.

因此,您的查询将变为 select path where name like %my_substring%(其中 path 是存储图像引用的列的名称)。

执行查询后,您的应用程序将根据查询结果访问图像。

我最终选择了没有数据库的方案(虽然@npinti提供的方案真的很聪明)。所以我做了类似的事情

File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    if(listOfFiles[i].getName().contains(my_substring)){
     //add the file to a list of files to be displayed 

}}}