Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java从mysql数据库检索图像以存储在本地磁盘上时出现的问题_Java_Mysql_Ubuntu 14.04 - Fatal编程技术网

使用java从mysql数据库检索图像以存储在本地磁盘上时出现的问题

使用java从mysql数据库检索图像以存储在本地磁盘上时出现的问题,java,mysql,ubuntu-14.04,Java,Mysql,Ubuntu 14.04,在将图像转换为bytes[]之后,我使用java将图像存储到mysql数据库中,然后再转换为Blob。图像格式为png 尝试从mysql数据库检索图像并将其转换为图像文件以保存在本地磁盘上 将mysql结果集迭代为: while(rs.next()) { ByteArrayInputStream bis = new ByteArrayInputStream(rs.getBytes("image")); Iterator<?> readers = ImageIO.getImageRe

在将图像转换为
bytes[]
之后,我使用java将图像存储到mysql数据库中,然后再转换为
Blob
。图像格式为
png
尝试从mysql数据库检索图像并将其转换为图像文件以保存在本地磁盘上

将mysql结果集迭代为:

while(rs.next())
{
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getBytes("image"));

Iterator<?> readers = ImageIO.getImageReadersByFormatName("png");
ImageReader reader = (ImageReader) readers.next();
Object source = bis; 

ImageInputStream iis = ImageIO.createImageInputStream(source); 
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();

Image image= reader.read(0, param);
BufferedImage bufferedImage = new BufferedImage(image1.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);

File imageFile = new File(rs.getInt("id")+".png");
ImageIO.write(bufferedImage, "png", imageFile);
}
其原因是:

javax.imageio.IIOException: Bad PNG signature!
我是java新手。我用谷歌搜索了一下,但没有找到任何令人满意的解决办法。
有谁能帮我解决这个问题,或者其他更好的解决方案,从数据库中检索图像并存储在本地磁盘中

我找到了解决办法。问题在我的代码中。下面是我用来从数据库中获取图像并存储为图像文件的正确代码

sql="select * from image_data where image_id="+image_id;        
        rs = this.runSimpleQuery(sql);
        while (rs.next()) 
        {
            Blob image_blob=rs.getBlob("image_100x100");
            int blobLength = (int) image_blob.length(); 
            byte[] blobAsBytes = image_blob.getBytes(1, blobLength); 
            InputStream in=new ByteArrayInputStream( blobAsBytes );
            BufferedImage image_bf = ImageIO.read(in); 
            ImageIO.write(image_bf, "PNG", new File(folder_path+"/"+rs.getString("name"))); 
        }

图像是什么类型的?它是blob还是其他什么?为什么要将读取的数据转换为BuffereImage?仅仅“按原样”将其保存为磁盘中的文件还不够吗?哪一行给出了异常?
sql="select * from image_data where image_id="+image_id;        
        rs = this.runSimpleQuery(sql);
        while (rs.next()) 
        {
            Blob image_blob=rs.getBlob("image_100x100");
            int blobLength = (int) image_blob.length(); 
            byte[] blobAsBytes = image_blob.getBytes(1, blobLength); 
            InputStream in=new ByteArrayInputStream( blobAsBytes );
            BufferedImage image_bf = ImageIO.read(in); 
            ImageIO.write(image_bf, "PNG", new File(folder_path+"/"+rs.getString("name"))); 
        }