java操作保存图片到oracle数据库及获取图片到页面显示

2019-04-15 13:20发布


《--------保存图片到数据库--------------》
public void savePic(InputStream is){
        String insertsql = "insert into pic(id,pic) values(1,empty_blob())";
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            int rt = stmt.executeUpdate(insertsql);
            if(rt > 0){
                System.out.println("initialization succeed!!");
                String preCursor = "select id,pic from pic where id='1' for update";
                rs = stmt.executeQuery(preCursor);
                rs.next();
                Blob b = (Blob)rs.getBlob("pic");
                BufferedOutputStream bos = new BufferedOutputStream(b.setBinaryStream(0L));
                BufferedInputStream bis = new BufferedInputStream(is);
                int bytes;
                try {
                    while((bytes = bis.read()) != -1){
                        bos.write(bytes);
                    }

                    bis.close();
                    bos.close();
                    conn.commit();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


《--------获取图片到到页面--------------》
public JPEGImageEncoder getPic(String id,HttpServletResponse res) throws IOException{
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        BufferedInputStream imageStream = null;
        BufferedImage image = null;
        JPEGImageEncoder encoder = null;
        ServletOutputStream sos =  res.getOutputStream();
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            System.out.println("initialization succeed!!");
            String preCursor = "select id,pic from pic where id='1'";
            rs = stmt.executeQuery(preCursor);
            rs.next();
            imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
            try {
                image = ImageIO.read(imageStream);
                encoder =  JPEGCodec.createJPEGEncoder(sos);
                encoder.encode(image);
                imageStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return encoder;
    }   从数据库获取图片,导出到本地的完整代码: package dbcon;

import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class DBManager {
    private String url = "jdbc:oracle:thin:@10.23.65.21:1521:orcl";
    private String userName = "test";
    private String password = "test";
    private String driver   = "oracle.jdbc.driver.OracleDriver";
    private Connection conn;
   
   
    public DBManager(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   
    public void getPic(String picPath) throws IOException{
        Statement stmt = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        BufferedInputStream imageStream = null;
        BufferedImage image = null;
        JPEGImageEncoder encoder = null;
        try {
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            System.out.println("initialization succeed!!");
            String preCursor = "select id,zp from zpb where id='2220087'";
            rs = stmt.executeQuery(preCursor);
            while(rs.next()){
                imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
                try {
                    String id = rs.getString(1);
                    File picturePath = new File(picPath +"/"+ id +".jpg");
                    boolean created  = false;
                    if(!picturePath.exists()){
                        created = picturePath.createNewFile();
                    } else {
                        created  = true;
                    }
                    if(created){
//                        ServletOutputStream sos =  res.getOutputStream();
                        OutputStream ostream = new FileOutputStream(picturePath);
                        image = ImageIO.read(imageStream);
                        encoder =  JPEGCodec.createJPEGEncoder(ostream);
                        encoder.encode(image);
                        imageStream.close();
                        ostream.flush();
                        ostream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try {
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

   
    public static void main(String[] s){
        DBManager manager = new DBManager();
        String localPath = "E:/testpics";
        try {
            manager.getPic(localPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}