栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将BufferedInputStream转换为图像[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将BufferedInputStream转换为图像[重复]

首先,先确认

uploadedInputStream
是有效图像,然后再使用将其写出来
ImageIO.write
。您始终可以使用
ImageIO.read
读回的图像并将其写回到
ByteArrayInputStream
;)

我使用H2数据库进行了快速测试。

我注意到了几件事。

Blob#length
返回
long
,而
Blob#getBytes
期望则返回
int
,这可能意味着您正在截断字节流。

另外,从H2的文档来看,似乎

Blob
内容没有保存在内存中,因此我改用了
getBinaryStream

import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.IOException;import java.sql.Blob;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JScrollPane;public class TestImageDatbase {    private Connection con;    public static void main(String[] args) {        new TestImageDatbase();    }    public TestImageDatbase() {        try { clearDatabase(); saveImage(); loadImage();        } catch (ClassNotFoundException | SQLException | IOException exp) { exp.printStackTrace();        }    }    protected Connection getConnection() throws ClassNotFoundException, SQLException {        Class.forName("org.h2.Driver");        return DriverManager.getConnection("jdbc:h2:d:\Image", "sa", "");    }    protected void clearDatabase() throws IOException, ClassNotFoundException, SQLException {        Connection con = null;        PreparedStatement stmt = null;        try { con = getConnection(); System.out.println("Cleaning database"); stmt = con.prepareStatement("delete from images"); int updated = stmt.executeUpdate(); System.out.println("Updated " + updated + " rows");        } finally { try {     stmt.close(); } catch (Exception e) { } try {     con.close(); } catch (Exception e) { }        }    }    protected void saveImage() throws IOException, ClassNotFoundException, SQLException {        Connection con = null;        PreparedStatement stmt = null;        ByteArrayOutputStream baos = null;        ByteArrayInputStream bais = null;        try { baos = new ByteArrayOutputStream(); File source = new File("/path/to/file"); System.out.println("Source size = " + source.length()); BufferedImage img = ImageIO.read(source); ImageIO.write(img, "png", baos); baos.close(); bais = new ByteArrayInputStream(baos.toByteArray()); con = getConnection(); stmt = con.prepareStatement("insert into images (image) values (?)"); stmt.setBinaryStream(1, bais); int updated = stmt.executeUpdate(); System.out.println("Updated " + updated + " rows");        } finally { try {     bais.close(); } catch (Exception e) { } try {     baos.close(); } catch (Exception e) { } try {     stmt.close(); } catch (Exception e) { } try {     con.close(); } catch (Exception e) { }        }    }    protected void loadImage() throws IOException, ClassNotFoundException, SQLException {        Connection con = null;        PreparedStatement stmt = null;        ResultSet rs = null;        try { con = getConnection(); stmt = con.prepareStatement("select image from images"); rs = stmt.executeQuery(); while (rs.next()) {     System.out.println("Getting blob");     Blob blob = rs.getBlob(1);     System.out.println("Reading image");     BufferedImage img = ImageIO.read(blob.getBinaryStream());     System.out.println("img = " + img);     JOptionPane.showMessageDialog(null, new JScrollPane(new JLabel(new ImageIcon(img)))); }        } finally { try {     rs.close(); } catch (Exception e) { } try {     stmt.close(); } catch (Exception e) { } try {     con.close(); } catch (Exception e) { }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号