日期:2014-05-20  浏览次数:20547 次

从数据库读取图片的问题
Java code

            File file = new File("image/temp.jpg");
            if(file.exists()){
           file.delete();
           file.createNewFile();
        }else{
            file.createNewFile();
        }
             InputStream in = rs.getBinaryStream(5); //rs是连接数据库返回的ResultSet对象
            FileOutputStream out = new FileOutputStream("image/temp.jpg");
            byte[] bytes = new byte[1024];
            int n;//将读取的文件写入到out流中
            while ((n = in.read(bytes)) != -1) {
                out.write(bytes, 0, n);
            }
            in.close();
            out.close();
            ImageIcon icon = new ImageIcon("image/temp.jpg");
            
            JFrame frame = new JFrame(); JPanel panel = new JPanel();
            frame.getContentPane().add(panel);
            frame.getContentPane().setLayout(null); panel.setSize(400,400);
            frame.setSize(400,400); panel.add(new JLabel(icon));
            frame.setVisible(true);



代码的意图是将数据库的图片取出来,写入image/temp.jpg文件中,可以每次在窗体中显示的都是同一张图片,但是在硬盘里面的temp.jpg文件已经确实改变了,不知道为什么, 求助

------解决方案--------------------
窗体图像区域需要刷新吧
------解决方案--------------------
FileOutputStream是输出流 你是要把数据库的数据写到 "image/temp.jpg" 文件中 应该用输入流吧