import com.ImagePanel;
import com.GUIConsole;import javax.swing.*;
import java.io.*;public class ImagePanelTest {  public static void main(String[] args) throws Exception{
    String pathname = "d:/tmp-img.dat";
    String imgpathname = "d:/131945.jpg";    ImageIcon imgIcon=new ImageIcon(imgpathname);
    //写
    ObjectOutputStream out=new ObjectOutputStream(
            new FileOutputStream(new File(pathname)));
    out.writeObject(imgIcon);
    out.close();
    //读
    ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(pathname)));
    imgIcon=(ImageIcon)in.readObject();
    in.close();
    // 显示
    ImagePanel imagePanel=new ImagePanel();
    imagePanel.setImageIcon(imgIcon);
    GUIConsole.run(imagePanel,imgIcon.getIconWidth(),imgIcon.getIconHeight());
  }
}

解决方案 »

  1.   

    Oracle中import java.io.*;
    import java.sql.*;import alan.jdbc.common.ConnectionFactory;
    import alan.jdbc.common.DatabaseUtils;
    public class BlobOperation
    {
             //插入一个blob
    public static void addLob(long id, String binFile) throws SQLException
    {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null; try
    {
    con = ConnectionFactory.getConnection(); //用你自己的方法获得连接
    con.setAutoCommit(false); String sql = "INSERT INTO Blob_Tbl(id, binfile, bincontent)";
    sql += " VALUES(?, ?, ?)";
    ps = con.prepareStatement(sql); ps.setLong(1, id);
    ps.setString(2, binFile);
    ps.setBlob(3, oracle.sql.BLOB.empty_lob()); ps.executeUpdate();
    DatabaseUtils.closeObject(ps); ps = con.prepareStatement("SELECT bincontent FROM Blob_Tbl WHERE id = " + id + " for update ");
    rs = ps.executeQuery();  if (rs.next())
    {
    oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob(1); /* write blob content */
    OutputStream binOut = binContent.getBinaryOutputStream();   //取得输出流
    BufferedOutputStream out = new BufferedOutputStream(binOut);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(binFile));
    int c;
    while ((c = in.read()) != -1)
    {
    out.write(c);        //真正的插入
    }
    in.close();
    out.close();
    }
    con.commit();
    } catch (Exception e)
    {
    e.printStackTrace();
    try
    {
    con.rollback();
    } catch (SQLException se)
    {
    }
    throw new SQLException(e.getMessage());
    } finally
    {
    DatabaseUtils.closeObject(rs, ps, con);
    }
    }
             
             //读取一个blob
    public static void fetchLob(long id, String filename) throws SQLException
    {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null; try
    {
    con = ConnectionFactory.getConnection(); String sql = "SELECT *  From Blob_Tbl Where id = " + id;
    st = con.createStatement(); rs = st.executeQuery(sql);
    while (rs.next())
    { String binFile = rs.getString("binfile");
    oracle.sql.BLOB binContent = (oracle.sql.BLOB) rs.getBlob("bincontent"); /* read blob content */
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
    BufferedInputStream in = new BufferedInputStream(binContent.getBinaryStream()); int c;
    while ((c = in.read()) != -1)
    {
    out.write(c);
    }
    in.close();
    out.close();
    } } catch (Exception e)
    {
    throw new SQLException(e.getMessage());
    } finally
    {
    DatabaseUtils.closeObject(rs, st, con);
    }
    } public static void main(String[] args) throws Exception
    {
    if (args.length == 0)
    {
    addLob(1, "a.jpg");
    } else
    {
    fetchLob(1, "b.jpg"); //读取后输出到b.jpg
    }
    }
    }
      

  2.   

    Blob_Tbl 中第一列是id 第二列是文件名  第三列是blob
      

  3.   

    谢谢大家,我是想把一个Label上显示的图片存储到本地或者MySQL数据库当中,下面的方法我使用过了,可是结果本地的图片文件不能正确打开...所以问题还是没解决;-(    ObjectOutputStream out=new ObjectOutputStream(
                new FileOutputStream(new File(pathname)));
        out.writeObject(imgIcon);
        out.close();
      

  4.   

    其实问题简化一点,跟MYSQL没关系的,就是怎么把一个ImageIcon对象存储到本地一个图片文件中,这下应该简单点把?可是我使用下面的方法,生成的图片文件就是不能正确打开。    ObjectOutputStream out=new ObjectOutputStream(
                new FileOutputStream(new File(pathname)));
        out.writeObject(imgIcon);
        out.close();
      

  5.   

    这样写进去的是一个ImageIcon对象,又不是图形文件数据,怎么可能打得 开。
    问题是:你为什么要把ImageIcon的图像写成图形文件?你的ImageIcon载入的图像从何而来?
      

  6.   

    是这样,我在GUI的一个JLabel中显示了一个图片,然后想将这个图片存储到本地文件,就这个要求,怎么做?从JLabel只能用getIcon获取到ImageIcon对象,按照您说的,那么请问如何将JLabel中显示的图片存储到本地文件呢?
      

  7.   

    这个,呵呵,好像有点难度,ImageIcon的接口都是和GUI相关的,
    不好取得图形数据啊,看看文档先。
    但是,你在load一个图形到ImageIcon时,不是可以取得图形文件数据吗?ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
    //将图形文件数据写入 byteOut
    //....
    byte[] imgData=byteOut.toByteArray();
    ImageIcon=new ImageIcon(imgData);在你的GUI中保持一个imgData变量就成。
      

  8.   

    可是ImageIcon对象是Serializable的啊,就是说它应该是保存了图象数据的。另外,该图象数据是从剪贴板获取的,我是通过ctrl+c/v将外部的一个图片拷贝到本地GUI,显示在JLabel上,然后想保存到本地,获取的过程是先从剪贴板获取到了Image对象,然后构造ImageIcon,显示在JLabel上。
      

  9.   

    ImageIcon对象当然保存了图象数据,要不然怎么显示呢,但好像没有提供访问其的接口(呵呵,我找了半天没找到,算了)。
    实现Serializable是保证Object可以被“序列化”,和图象数据没有任何关系的。
    写了个例子,应该满足你的要求了。package com.gui;import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import com.sun.image.codec.jpeg.JPEGCodec;import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.*;/**
     * User: Saro
     * Date: 2005-2-16
     * Time: 22:54:26
     */
    public class ImagePanel extends JPanel{
      private ImageIcon imageIcon;  public ImagePanel(){
        JButton pasteBtn1 = new JButton("粘贴剪贴板上的图片");
        this.add(pasteBtn1);
        pasteBtn1.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            try {
              pasteBufferImage();
            } catch (Exception e1) {
              e1.printStackTrace();
            }
          }
        });    JButton pasteBtn2 = new JButton("粘贴剪贴板上的图形文件");
        this.add(pasteBtn2);
        pasteBtn2.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            try {
              pasteImageFile();
            } catch (Exception e1) {
              e1.printStackTrace();
            }
          }
        });
      }  /**
       * 粘贴剪贴板上的图形数据
       *
       * @throws UnsupportedFlavorException
       * @throws IOException
       */
      private void pasteBufferImage() throws UnsupportedFlavorException, IOException {
        File imageDir=new File("f:");
        Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
        BufferedImage img=(BufferedImage)cb.getContents(this).getTransferData(DataFlavor.imageFlavor);
        File imagefile = new File(imageDir,"test.jpg");
        BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(imagefile));
        writeImage(img,out);
        out.close();
        imageIcon=new ImageIcon(img);
        paintAll(getGraphics());
      }  /**
       * 粘贴图形文件
       *
       * @throws UnsupportedFlavorException
       * @throws IOException
       */
      private void pasteImageFile() throws UnsupportedFlavorException, IOException {
        File imageDir=new File("f:");
        Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
        java.util.List imageList=(java.util.List)cb.getContents(this).getTransferData(DataFlavor.javaFileListFlavor);
        //假定只有一个图形文件
        File imageFile=(File)imageList.get(0);
        imageIcon=new ImageIcon(imageFile.getAbsolutePath());
        paintAll(getGraphics());
      }  /**
       * 将 BufferedImage写入数据流中
       *
       * @param image  BufferedImage对象
       * @param out 输出数据流
       * @throws IOException
       */
      public void writeImage(BufferedImage image,OutputStream out) throws IOException{
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.getDefaultJPEGEncodeParam(image).setQuality(1.0f, true);
        encoder.encode(image);
      }  public void paint(Graphics g){
        super.paint(g);
        if(imageIcon!=null){
          imageIcon.paintIcon(this,g,0,0);
        }
      }
    }
      

  10.   

    多谢了,问题基本解决了,你的代码中的writeImage方法是个关键,自己没有用过,怎么也想不出来,可能还是基础不够扎实的原因把;-(对于不是利用ctrl+v拷贝到JLabel中的图片,还不能用你的方法,因为获得的不是BufferedImage类型的对象,它们是直接从数据库用BLOB获取出来然后显示在JLabel中的,我保存了它的byte[]然后也解决了,多谢你的提示;-)现在暂时得到的结论是,对于从界面上(JButton或者JLabel中)获取的图片对象(都是ImageIcon类型),如果不是通过ctrl+v拷贝过来的,都无法直接保存到本地文件,必须提前获取其字节数组。虽然问题解决了,但是还是有两个困惑:
    1:同样是从JLabel中,利用getIcon方法获取的图片,系统如何判断哪些是BufferedImage类型,哪些 
       不是?目前的结果是,我ctrl+v拷贝上去的,都判断为是,而从数据库中获取然后显示上去的都判 
       断为不是,这个原因我还没想透。
    2:为何无法作到将一个单纯的ImageIcon对象保存到本地,而必须通过提前保存其字节数组来作到?感
       觉不应该的。或者问题可以转换为,如何获得ImageIcon对象的字节数组?哪些对象可以获得其对应
       的字节数组?一般通过什么方式?
       
       愿与大家探讨;-)