java - 将一个字节数组 (BLOB IBM DB2) 中的多个图像导出到磁盘
问题描述
我在数据库(IBM DB2)中有一个“内容”列(BLOB 数据),并且记录的数据与(https://drive.google.com/file/d/12d1g5jtomJS-ingCn_n0GKMsM4RkdYzB/view?usp=sharing)相同我已经通过编辑器打开了它,我认为它有不止一个图像(https://i.stack.imgur.com/2biLN.png,https://i.stack.imgur.com/ZwBOs.png)。
我可以将图像从字节数组(使用 C#)导出到我的磁盘,但是对于多个图像,我不知道该怎么做。
请帮我!谢谢!
编辑 1:我尝试通过以下代码将其导出为一张图片:
private void readBLOB(DB2Connection conn, DB2Transaction trans)
{
try
{
string SavePath = @"D:\\MyBLOB";
long CurrentIndex = 0;
//the number of bytes to store in the array
int BufferSize = 413454;
//The Number of bytes returned from GetBytes() method
long BytesReturned;
//A byte array to hold the buffer
byte[] Blob = new byte[BufferSize];
DB2Command cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ATTR0102500126 " +
" FROM JCR.ICMUT01278001 " +
" WHERE COMPKEY = 'N21E26B04900FC6B1F00000'";
cmd.Transaction = trans;
DB2DataReader reader;
reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
if (reader.Read())
{
FileStream fs = new FileStream(SavePath + "\\" + "quang canh.jpg", FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
//reset the index to the beginning of the file
CurrentIndex = 0;
BytesReturned = reader.GetBytes(
0, //the BlobsTable column index
CurrentIndex, // the current index of the field from which to begin the read operation
Blob, // Array name to write the buffer to
0, // the start index of the array
BufferSize // the maximum length to copy into the buffer
);
while (BytesReturned == BufferSize)
{
writer.Write(Blob);
writer.Flush();
CurrentIndex += BufferSize;
BytesReturned = reader.GetBytes(0, CurrentIndex, Blob, 0, BufferSize);
}
writer.Write(Blob, 0, (int)BytesReturned);
writer.Flush(); writer.Close();
fs.Close();
}
reader.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
但无法查看图像,显示格式错误 => https://i.stack.imgur.com/PNS9Q.png
解决方案
您目前假设该数据库中的所有 BLOBS 都是 JPEG 图像。但显然情况并非如此。
选项 1:这是一个错误的数据
保存到数据库的程序可能会失败。
数据库本身可能会失败,尤其是在关闭事务的情况下。BLOB 的事务很可能已关闭。
存储数据的物理磁盘可能已降级。再一次,您不会使用 BLOBS 获得大量冗余和纠错(加上使用纠错需要首先通过适当的 DBMS)。
选项 2:这不是 jpg
我知道关于Unicode的文章说“[...]问题归结为一个天真的程序员,他不理解一个简单的事实,即如果你不告诉我一个特定的字符串是使用 UTF-8 还是 ASCII 或 ISO 编码的8859-1(拉丁语 1)或 Windows 1252(西欧),您根本无法正确显示它,甚至无法弄清楚它的结束位置。”
这适用于图像的双重、三重和四重:
- 这可以是使用Interlacing的任意数量的格式。
- 这可能是像TIFF这样的专业图形程序图像/项目文件。它可以完全包含多个图像 - 您正在使用的每一层最多一个。
- 这甚至可以是通过 .ZIP 压缩和 word 文档运行的.SVG 文件(包含绘图命令的 XML 文本)
- 这甚至可以是 PDF,其中图像通常附加在后面(允许您阅读带有部分文件的文本,类似于交错)
推荐阅读
- flutter - 切换选项卡时 AppBar 中不需要的空间
- sql-server - 尝试在 where 语句中选择记录(最大日期)
- flutter - 在文本字段中实现语音到文本的颤动
- pandas - numpy数组中允许的DataFrame
- sql - 在触发之前导致 ORA-04091
- python - 如何使用 pytest 为类范围的固定装置创建工厂?
- asp.net-core - 如何让 NSwag 识别 POST 路由上的“application/json”返回类型?
- python-3.x - 如何使用 Python GUI 关闭具有特定文件路径的外部程序
- css - 在 CSS 中放置元素
- java - 在 selenium 中同时打开多个页面窗口时如何导航到特定选项卡?