gpt4 book ai didi

java - 如何从数据库获取 BLOB 并将其像图像一样显示

转载 作者:行者123 更新时间:2023-11-29 16:41:01 24 4
gpt4 key购买 nike

我正在尝试从数据库中获取 BLOB 并将其像图像一样显示,我搜索类似的问题但从未工作

Blob blob = rs.getBlob(12);
InputStream in = blob.getBinaryStream();
BufferedImage imagen = ImageIO.read(in);
Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );

执行此操作并执行方法后,我得到一个空指针异常,因为未加载图像,然后在构造函数中采用带有图像的对象的列表为空,因此我得到空异常,但我的问题是:

从数据库中获取 BLOB 格式的图像并将其转换为在应用程序中显示的图像的正确方法是什么?

@Override
public List<MonstruoDTO> getMonstruos() {
ArrayList<MonstruoDTO> lista = new ArrayList<MonstruoDTO>();
con = ConexionBDA.getInstance().getCon();
try {
if(con!= null) {
statement = con.createStatement();
ResultSet rs=statement.executeQuery("SELECT * FROM monstruos");
while(rs.next()){
String nombre = rs.getString(2);
String habitat = rs.getString(3);
String estado = rs.getString(4);
String colaCercenable = rs.getString(5);
String dragonAnciano = rs.getString(6);
int debilidadFuego = rs.getInt(7);
int debilidadAgua = rs.getShort(8);
int debilidadRayo = rs.getInt(9);
int debilidadHielo = rs.getInt(10);
int debilidadDraco = rs.getInt(11);
Blob blob = rs.getBlob(12);
InputStream in = blob.getBinaryStream();
BufferedImage imagen = ImageIO.read(in);
Image imagenMonstruo = SwingFXUtils.toFXImage(imagen, null );
MonstruoDTO monstruo = new MonstruoDTO(nombre, habitat, estado, colaCercenable, dragonAnciano, debilidadFuego, debilidadAgua, debilidadRayo, debilidadHielo, debilidadDraco, imagenMonstruo);
lista.add(monstruo);
}
}

我得到的日志错误是这样的

java.lang.NullPointerException
at javafx.embed.swing.SwingFXUtils.toFXImage(SwingFXUtils.java:85)
at modelo.ImplementacionMonstruosDAO.getMonstruos(ImplementacionMonstruosDAO.java:57)
at controlador.ConsultarMonstruosController.obtenerListaMonstruos(ConsultarMonstruosController.java:163)
at controlador.ConsultarMonstruosController.initialize(ConsultarMonstruosController.java:83)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at controlador.VentanaMonstruosController.eventoBotonConsultar(VentanaMonstruosController.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

编辑

我能够修复崩溃,但我仍然没有看到显示的图像:

我的程序崩溃了,因为我正在使用 import java.awt.image 并尝试将其变成 javafx 图像,所以基本上解决方案是从 Blob 传递到图像,并且程序不会崩溃并显示所有数据,但仍然没有显示图像。

Image imagenMonstruo = null;
Blob blob = rs.getBlob(12);
InputStream is = blob.getBinaryStream();
imagenMonstruo = new Image(is);

你能帮我吗?

最佳答案

发布的代码很好并且可以工作,我遇到的问题是存储在数据库中的图像是错误的,因此存储的图像无法显示,所以如果有人遇到与我相同的问题,我建议你去数据库本身并在那里添加一张图片,看看图片的大小,因为从数据库添加的图片大小为 26 KB,而我想要获取的图片为 10B。

关于java - 如何从数据库获取 BLOB 并将其像图像一样显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53342022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com