在數據庫中用來存儲圖片&流


感謝大佬的分享,親測有效 這裏做個記錄,防止將來丟失
轉載:http://www.cnblogs.com/warrior4236/p/5682830.html

建表

在這裏插入圖片描述

獲取數據庫連接

public class DBUtils {
	//定義數據庫連接參數
	public static final String DRIVER_CLASS_NAME="com.mysql.jdbc.Driver";
	public static final String URL="jdbc:mysql://localhost:3306/qujinlei";
	public static final String USERNAME ="root";
	public static final String PASSWORD ="123";

	//註冊數據庫驅動
	static {
		try {
			Class.forName(DRIVER_CLASS_NAME);
		}catch(ClassNotFoundException e){
			System.out.println("註冊失敗");
			e.printStackTrace();
		}
	}

	//獲取連接
	public static Connection getConn() throws SQLException{
		return DriverManager.getConnection(URL, USERNAME, PASSWORD);
	}

    //關閉連接
	public static void closeConn(Connection conn){
         if(conn != null) {
         	try{
         		conn.close();
            }catch(SQLException e){
         		System.out.println("關閉連接失敗");
         		e.printStackTrace();
            }
         }
	}

	public static void main(String[] args){
		try {
			System.out.println(DBUtils.getConn());
		}catch(SQLException e){
			System.out.println("獲取連接失敗");
			e.printStackTrace();
		}
	}
}

封裝讀取圖片的流

public class ImageUtil {

	//獲取本地圖片輸入流
	public static FileInputStream readImg1(String path) throws IOException{
		return new FileInputStream(new File(path));
	}

	//讀取表中圖片獲取輸出流
    public static  void readImg2(InputStream is,String targetPath){
		File file=new File(targetPath);
        String path=targetPath.substring(0,targetPath.lastIndexOf("/"));
        if(!file.exists()){
        	new File(path).mkdirs();
        }
        FileOutputStream fos=null;
        try {
        	fos=new FileOutputStream(file);
        	int len=0;
        	byte[] buf=new byte[1024];
        	while((len=is.read(buf))!=-1){
        		fos.write(buf,0,len);
	        }
	        fos.flush();
        } catch(Exception e){
        	e.printStackTrace();
        }finally {
            if(null != fos){
            	try{
                   fos.close();
	            }catch(IOException e){
                   e.printStackTrace();
	            }
            }
        }
    }
}

實現圖片(本地、數據庫互相傳輸)

public class ImageDemo {

	//將圖片插入數據庫
	public static void ImageToDB(){
//		String path = "D:/1.png";
		String path="D:\\1.png";
		Connection conn=null;
		PreparedStatement ps=null;
		FileInputStream fis=null;
		try{
			fis=ImageUtil.readImg1(path);
			conn=DBUtils.getConn();
			String sql="insert into photo (id,name,photo) values(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setInt(1,1);
			ps.setString(2,"leo");
			ps.setBinaryStream(3,fis,fis.available());
			int count=ps.executeUpdate();
			if(count>0){
				System.out.println("插入成功!");
			}else {
				System.out.println("插入失敗!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtils.closeConn(conn);
			if(ps!=null){
				try{
                   ps.close();
				}catch(SQLException e){
                   e.printStackTrace();
				}
			}
		}
	}

	//讀取數據庫圖片
	public static void readDBImg(){
		String targetPath="d:/image/1.jpg";
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
            conn=DBUtils.getConn();
            String sql="select * from photo where id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1,1);
            rs=ps.executeQuery();
            while(rs.next()){
            	InputStream is=rs.getBinaryStream("photo");
                ImageUtil.readImg2(is,targetPath);
            }
			System.out.println("讀取成功");
		}catch(Exception e){
		   e.printStackTrace();
		}finally{
			DBUtils.closeConn(conn);
			if(rs!=null){
				try{
				   rs.close();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
			if(ps != null) {
				try{
					ps.close();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		//將圖片插入數據庫
//		ImageToDB();
		//讀取數據庫圖片
		readDBImg();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章