oracle BLOB格式输出图片转换图片方法

tech2022-11-04  137

import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.*; import java.sql.*; import javax.imageio.ImageIO; import javax.imageio.stream.FileImageOutputStream; import javax.imageio.stream.ImageOutputStream; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) // @SpringBootApplication public class DemoApplication { //---------数据库连接 private static String url = "jdbc:oracle:thin:@localhost:3306:ORCL"; private static String user = ""; private static String password = ""; //-------- public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); read(); System.out.println("结束111111111111111111111111111"); } //从数据库读取一条记录并抽取第四列的BLOB对象 private static void read() { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = getConnection(); String sql = "sql语句"; stmt = conn.prepareStatement(sql); //加入sql语句的参数 stmt.setString(1,"0000000000000"); rs = stmt.executeQuery(); //获取查询结果 while(rs.next()){ //人员ID String pk_psndoc=rs.getString("pk_psndocssss"); //1、从数据库读取BLOB Blob blob_photo = rs.getBlob("photoes"); File f = new File("D:/demo/"+pk_psndoc+".JPG"); //方法一 mothod(f,blob_photo); //方法二 mothod2(f,blob_photo); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs != null){ rs.close(); } if(stmt != null){ stmt.close(); } if(conn != null) { conn.close(); } } catch (Exception e){ e.printStackTrace(); } } } public static void mothod(File f,Blob blob_photo) throws IOException { byte[] obj2 = blobToBytes(blob_photo); byte[] img_photo2=(byte[]) deserialize(obj2); FileImageOutputStream out = null; out = new FileImageOutputStream(f); out.write(img_photo2,0,img_photo2.length); out.close(); } public static void mothod2(File f,Blob blob_photo) throws IOException { byte[] obj = (byte[])blob2Object(blob_photo); FileImageOutputStream out = null; out = new FileImageOutputStream(f); out.write(obj,0,obj.length); out.close(); } public static Serializable deserialize(byte[] ba) { Serializable value = null; try { System.out.println(ba); if (ba == null) return null; System.out.println(ba); ByteArrayInputStream bi = new ByteArrayInputStream(ba); ObjectInputStream is = new ObjectInputStream(bi); value = (Serializable) is.readObject(); } catch (IOException e) { return ba; } catch (ClassNotFoundException e) { e.printStackTrace(); } return value; } public static byte[] blobToBytes(java.sql.Blob blob) { BufferedInputStream is = null; try { is = new BufferedInputStream(blob.getBinaryStream()); byte[] bytes = new byte[(int) blob.length()]; int len = bytes.length; int offset = 0; int read = 0; while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) { offset += read; } return bytes; } catch (Exception e) { return null; } finally { try { is.close(); is = null; } catch (IOException e) { return null; } } } public static Object blob2Object(java.sql.Blob decBlob) { try { ObjectInputStream in = new ObjectInputStream(decBlob.getBinaryStream()); Object obj = in.readObject(); in.close(); return obj; } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取数据库连接 */ public static Connection getConnection(){ try { Connection conn = DriverManager.getConnection(url, user, password); if(conn != null){ System.out.println("已连接!\n"); } return conn; } catch (SQLException e) { e.printStackTrace(); System.out.println("连接失败!"); throw new RuntimeException(e); // return null; } } }
最新回复(0)