在数据库中用来存储图片&流


感谢大佬的分享,亲测有效 这里做个记录,防止将来丢失
转载: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();
	}
}

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