简体   繁体   English

在MySQL数据库中存储BLOB

[英]Storing a BLOB in MySQL Database

I have an image uploaded to a server with location like : opt/glassfish/domains/domain1/applications/j2ee-modules/SmartbadgePortal/images/2badge.jpg 我有一个图像上传到服务器,其位置如下: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". 但是,当我使用System.out打印“图像”时。 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|#] 图像内容为* :BufferedImage @ 10d7a81:type = 5 ColorModel:#pixelBits = 24 numComponents = 3色彩空间= java.awt.color.ICC_ColorSpace@722270透明度= 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. 我需要图像的内容,并将其存储在MySQL的BLOB列中。 Please help as I am been trying various methods like ByteArrayInputStream ,but every time I see only this info rather than image itself. 请帮忙,因为我一直在尝试ByteArrayInputStream之类的各种方法,但是每次我看到的都是此信息,而不是图像本身。

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. 除非有特定原因,否则在BLOB单元中存储图像通常不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM