1. <tfoot id='mX2vq'></tfoot>

    <small id='mX2vq'></small><noframes id='mX2vq'>

    1. <i id='mX2vq'><tr id='mX2vq'><dt id='mX2vq'><q id='mX2vq'><span id='mX2vq'><b id='mX2vq'><form id='mX2vq'><ins id='mX2vq'></ins><ul id='mX2vq'></ul><sub id='mX2vq'></sub></form><legend id='mX2vq'></legend><bdo id='mX2vq'><pre id='mX2vq'><center id='mX2vq'></center></pre></bdo></b><th id='mX2vq'></th></span></q></dt></tr></i><div id='mX2vq'><tfoot id='mX2vq'></tfoot><dl id='mX2vq'><fieldset id='mX2vq'></fieldset></dl></div>
    2. <legend id='mX2vq'><style id='mX2vq'><dir id='mX2vq'><q id='mX2vq'></q></dir></style></legend>

        <bdo id='mX2vq'></bdo><ul id='mX2vq'></ul>

    3. 使用 Java 以二进制格式将图像存储在数据库中

      Storing Image in Data Base Using Java in Binary Format(使用 Java 以二进制格式将图像存储在数据库中)

        <small id='gPSP3'></small><noframes id='gPSP3'>

            <tbody id='gPSP3'></tbody>

              <bdo id='gPSP3'></bdo><ul id='gPSP3'></ul>
              1. <tfoot id='gPSP3'></tfoot>

                <i id='gPSP3'><tr id='gPSP3'><dt id='gPSP3'><q id='gPSP3'><span id='gPSP3'><b id='gPSP3'><form id='gPSP3'><ins id='gPSP3'></ins><ul id='gPSP3'></ul><sub id='gPSP3'></sub></form><legend id='gPSP3'></legend><bdo id='gPSP3'><pre id='gPSP3'><center id='gPSP3'></center></pre></bdo></b><th id='gPSP3'></th></span></q></dt></tr></i><div id='gPSP3'><tfoot id='gPSP3'></tfoot><dl id='gPSP3'><fieldset id='gPSP3'></fieldset></dl></div>
              2. <legend id='gPSP3'><style id='gPSP3'><dir id='gPSP3'><q id='gPSP3'></q></dir></style></legend>
                本文介绍了使用 Java 以二进制格式将图像存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在处理我的项目并遇到一些问题,我搜索了它,但找不到有意义的完整学习资源,我需要的是使用我的 Java 程序将图像存储在 SQL Server 数据库中,并且需要返回检索,图像不是更大的尺寸,它们在 30 到 50 K 之间,我可以使用 toolKit 中的 getImage() 方法从我的磁盘加载图像

                I am working around my project and got some Problem, I searched for it , but couldn't find meaning full learning resource, What I need is to Store Image in SQL Server Database using my Java program , and will need that Back to Retrieve, images are not of larger size they are ranging between 30 and 50 K, I can load images from my Disk by using getImage() method in toolKit

                Image imm = Toolkit.getDefaultToolkit().getImage("URl");
                

                ,但我不知道如何将该图像转换为二进制格式并存储在数据库中,然后从数据库中检索回来.我想通过查看几个站点将其存储在 VarBinary 中,我发现 SQL Server 中的图像类型即将停用.

                , but i don't know How to convert that image to Binary Format and to store in Database, and then retrieve Back From Database. I want to Store that in VarBinary s by looking around several sites i found that the image type in SQL Server is soon going to Retire.

                推荐答案

                虽然在数据库中存储非常大的二进制对象并不是一个好主意,但微软研究论文To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem" 表明如果对象大小小于256K.所以听起来您已经达到了使用 30K 图像进行数据库存储的最佳选择.

                While it's not a good idea to store very large binary objects in the database, the Microsoft research paper "To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem" indicates it's an efficient approach if object sizes are less than 256K. So it sounds like you've hit the sweet spot for database storage with 30K images.

                您可以使用以下方法从 URL 将图像作为缓冲图像加载:

                You can load your image as a buffered image from a URL using:

                BufferedImage imm = ImageIO.read(url);
                

                然后将其转换为字节数组:

                Then convert it to a byte array with:

                byte[] immAsBytes = 
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                //use another encoding if JPG is innappropriate for you
                ImageIO.write(imm, "jpg", baos );
                baos.flush();
                byte[] immAsBytes = baos.toByteArray();
                baos.close();
                

                然后存储字节数组本文推荐 作为数据库中的 VARBINARY.在您的 JDBC 代码中,您应该将字节数组包装在 ByteArrayInputStream 中,并将 PreparedStatement 参数设置为 BinaryStream:

                Then store the byte array as recommended by this article as a VARBINARY in the database. In your JDBC code, you should wrap the byte array in a ByteArrayInputStream and set the PreparedStatement parameter as a BinaryStream:

                PreparedStatement pstmt = commection.prepareStatement("INSERT INTO IMAGES (image) VALUES(?)");
                ByteArrayInputStream bais = new ByteArrayInputStream(immAsBytes);
                pstmt.setBinaryStream(1, bais, immAsBytes.length);
                pstmt.executeUpdate();
                pstmt.close();
                

                要从数据库中取出图像,请使用 ResultStatement.getBlob():

                To get the image out of the database, use ResultStatement.getBlob():

                Blob immAsBlob = rs.getBlob();
                byte[] immAsBytes = immAsBlob.getBytes(1, (int)immAsBlob.length()));
                

                最后,将字节数组转换为 BufferedImage 并对其进行处理:

                Finally, convert the byte array to a BufferedImage and do something with it:

                InputStream in = new ByteArrayInputStream(immAsBytes);
                BufferedImage imgFromDb = ImageIO.read(in);
                

                这篇关于使用 Java 以二进制格式将图像存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                • <tfoot id='Hth7B'></tfoot>

                    1. <legend id='Hth7B'><style id='Hth7B'><dir id='Hth7B'><q id='Hth7B'></q></dir></style></legend>

                      1. <i id='Hth7B'><tr id='Hth7B'><dt id='Hth7B'><q id='Hth7B'><span id='Hth7B'><b id='Hth7B'><form id='Hth7B'><ins id='Hth7B'></ins><ul id='Hth7B'></ul><sub id='Hth7B'></sub></form><legend id='Hth7B'></legend><bdo id='Hth7B'><pre id='Hth7B'><center id='Hth7B'></center></pre></bdo></b><th id='Hth7B'></th></span></q></dt></tr></i><div id='Hth7B'><tfoot id='Hth7B'></tfoot><dl id='Hth7B'><fieldset id='Hth7B'></fieldset></dl></div>
                          <bdo id='Hth7B'></bdo><ul id='Hth7B'></ul>

                          <small id='Hth7B'></small><noframes id='Hth7B'>

                            <tbody id='Hth7B'></tbody>