在 MySQL 数据库中存储 BLOB

发布于 2024-12-29 19:57:21 字数 955 浏览 0 评论 0原文

我将图像上传到位置如下的服务器: opt/glassfish/domains/domain1/applications/j2ee-modules/SmartbadgePortal/images/2badge.jpg

我正在尝试读取图像的内容而不是图像信息。我搜索了很多并且可以获得以下解决方案:

                   File uploadedFile = new File(path);
                    System.out.println("Uploaded File is ***  : " + uploadedFile);
                    item.write(uploadedFile);
                    Image image = null;
                    image = ImageIO.read(uploadedFile);
                    System.out.println("Image Contents is ***  : " + image);

但是,当我使用 System.out 打印“图像”时。我得到:

图像内容是*:BufferedImage@10d7a81:类型= 5 ColorModel:#pixelBits = 24 numComponents = 3颜色空间= java.awt.color.ICC_ColorSpace@722270透明度= 1有alpha = false isAlphaPre = false ByteInterleavedRaster:宽度 = 418 高度 = 387 #numDataElements 3 dataOff[0] = 2|#]

但是,这不是我需要的。我需要图像的内容,并且需要将其存储在 MySQL 的 BLOB 列中。请帮助,因为我一直在尝试各种方法,例如 ByteArrayInputStream ,但每次我只看到此信息而不是图像本身。

I have an image uploaded to a server with location like : opt/glassfish/domains/domain1/applications/j2ee-modules/SmartbadgePortal/images/2badge.jpg

I am trying to read the contents of the image rather than the image information. I searched a lot and could get the following solution for it :

                   File uploadedFile = new File(path);
                    System.out.println("Uploaded File is ***  : " + uploadedFile);
                    item.write(uploadedFile);
                    Image image = null;
                    image = ImageIO.read(uploadedFile);
                    System.out.println("Image Contents is ***  : " + image);

However, the when I used System.out to print "image". I get :

Image Contents is * : BufferedImage@10d7a81: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@722270 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 418 height = 387 #numDataElements 3 dataOff[0] = 2|#]

But , this is not what I need. I need the contents of the image and need to store it in a BLOB column in MySQL. Please help as I am been trying various methods like ByteArrayInputStream ,but every time I see only this info rather than image itself.

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

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

发布评论

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

评论(1

累赘 2025-01-05 19:57:21

尽管这不是您正在寻找的答案,但我的建议是将图像存储在服务器的文件系统中,并将文件名(可能还有目录)保存在数据库中。除非有特定原因,否则将图像存储在 BLOB 单元中通常不是一个好主意。

Although it's not the answer you're looking for, my recommendation is to store the image in your server's file system and saving the file name (and maybe directory) in your DB. Storing an image in a BLOB cell is usually not a good idea unless there's a specific reason.

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