把图片存到mysql数据库中,然后再把图片读出来输出到文件中.
为什么打开这个图片文件后显示的是乱码if (rs.next()) {
Blob blb = rs.getBlob("图片");
new FileOutputStream(
"C:\\Documents and Settings\\Administrator\\桌面\\123.gif")
.write(blb.getBytes(1l, (int)blb.length()));
}

解决方案 »

  1.   


      FileOutputStream out=new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\123.gif");    
            // File file = new File("newFile.jpg");   
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
            encoder.encode(tag); //JPEG编码   
            out.close();   
      

  2.   

    推荐个赚钱的好地方:http://www.jooplay.com/?374827编码方式不对
      

  3.   

    UTF-8的编码方式可以很好的支持中文。
      

  4.   

    InputStream in = rs.getBinaryStream("图片");
    File f = new File("a.jpg");
    OutPutStream out = new BufferedOutPutStream(new FileOutPutStream(f));
    byte[] buff = new byte[1024];
    for(int i =0;(i=in.read(buff))>0;)
    {
      out.write(buff,0,i);
    }
    out.close();
    in.close();
      

  5.   

    这是我存图片的方式
    con = JDBC.getConnection();
    String sql = "insert into blob_test(big_byte)values(?)";
    ps = con.prepareStatement(sql);
    File f = new File("c:\\af.jpg");
    InputStream in = new BufferedInputStream(new FileInputStream(f)); ps.setBinaryStream(1,in,f.length()); int i = ps.executeUpdate();
    in.close();
      

  6.   


    错了,应该是con = JDBC.getConnection();
    String sql = "select big_int from blob_test";
    st = con.createStatement();
    rs = st.executeQuery(sql);
    while(rs.next())
    {
    InputStream in = rs.getBinaryStream(1);
    File f = new File("c:\\af.jpg");
    OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
    byte[] b = new byte[1024];
    for(int i = 0; (i = in.read(b)) > 0 ;)
    {
    out.write(b,0,i);
    }
    out.close();
    in.close();
    }
      

  7.   

    我存储的图片是名称为 "暴风截屏.bmp" 的文件, 当截屏的图片格式是 .bmp 时可以存到表中longblob类型的字段中,如果截屏的格式是 "暴风截屏.jpg" 的文件,当执行 "pstmt.executeUpdate();" 方法时就抛Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException不知道是不是格式的问题 
      

  8.   


    刚才试了,好像还是不行.我把我的代码贴出来吧
    public class Test { public static void main(String[] args) throws Exception { // Class.forName("org.gjt.mm.mysql.Driver"); String jdbc = "jdbc:mysql://localhost:3306/gxjsql";
    String user = "root";
    String password = "xiao";
    Connection conn = null;
    String sql1 = "insert into prosen1 (name,age) values (?,?);"; // 增加记录
    String sql2 = "select name from prosen1 where id = 1;"; // 查询记录
    conn = java.sql.DriverManager.getConnection(jdbc, user, password);
    //存储部份
    /*
    PreparedStatement ps = conn.prepareStatement(sql1); File f = new File(
    "C:\\Documents and Settings\\Administrator\\桌面\\暴风截屏.bmp");
    InputStream in1 = new BufferedInputStream(new FileInputStream(f)); ps.setBinaryStream(1, in1, f.length());  // 设置longblob类型字段name的值
    ps.setInt(2, 13); // 设置int 类型字段age的值  int y = ps.executeUpdate();
    in1.close();  
    */
    // 上面部份是将桌面图片存入到数据库中,下面部份是将数据库中图片读取出来输出到桌面上

    //读取部份
    Statement st = conn.createStatement();  //创建对象
    ResultSet rs = st.executeQuery(sql2);  //查询记录
    while (rs.next()) {
    InputStream in2 = rs.getBinaryStream(1);
    File f2 = new File(
    "C:\\Documents and Settings\\Administrator\\桌面\\213.BMP");
    OutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    byte[] b = new byte[1024];
    for (int i = 0; (i = in2.read(b)) > 0;) {
    out.write(b, 0, i);
    }
    out.close();
    in2.close();
    }
    conn.close(); 
    }
    }