将一个字节数组(BLOB IBM DB2)中的多个图像导出到磁盘

o2rvlv0m  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(134)

我在数据库(IBM DB2)中有一列“内容”(BLOB数据)和一个记录的数据相同,(https://drive.google.com/file/d/12d1g5jtomJS-ingCn_n0GKMsM4RkdYzB/view?usp=sharing)我已经通过编辑器打开了它,我认为它在这个(https://i.stack.imgur.com/2biLN.pnghttps://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

nqwrtyyt

nqwrtyyt1#

你目前正在假设该数据库中的所有BLOB都是JPEG图像。但显然情况并非如此。

选项1:这是错误的数据

保存到数据库的程序可能会失败。
数据库本身可能会失败,尤其是在关闭事务的情况下。BLOB的事务最有可能被关闭。
存储数据的物理磁盘可能已经降级。同样,您不会通过BLOB获得大量冗余和错误纠正(加上使用错误纠正首先需要通过适当的DBMS)。

选项2:这不是jpg

我知道一篇关于Unicode的文章说:“[...]问题归结于一个天真的程序员,他不理解一个简单的事实,即如果你不告诉我一个特定的字符串是使用UTF-8、ASCII、ISO 8859-1(Latin 1)还是Windows 1252(西欧)编码的,你就不能正确地显示它,甚至不能弄清楚它的结尾。”
这可以应用于图像的两倍、三倍和四倍:

  • 这可以是使用X1 E1 F1 X的任何数量的格式。
  • 这可能是一个专业的图形程序员的图像/项目文件,如TIFF。2它可以完全包含多个图像-最多每层一个你正在使用的图像。
  • 这甚至可以是通过.ZIP压缩和Word文档运行的.SVG file(包含绘图顺序的XML文本
  • 这甚至可以是一个PDF,其中的图像通常附加在后面(允许您阅读部分文件的文本,类似于交错)

相关问题