Java Mysql存取图片

数据库:

在这里插入图片描述

数据库工具类:

public class DbUtil {

	
	public Connection getCon() throws Exception{
		Class.forName(PropertiesUtil.getValue("jdbcName"));
		Connection con=DriverManager.getConnection(PropertiesUtil.getValue("dbUrl"), PropertiesUtil.getValue("dbUserName"), PropertiesUtil.getValue("dbPassword"));
		return con;
	}
	
	public void closeCon(Connection con)throws Exception{
		if(con!=null){
			con.close();
		}
	}
	
	public static void main(String[] args) {
		DbUtil dbUtil=new DbUtil();
		try {
			dbUtil.getCon();
			System.out.println("数据库连接成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接失败");
		}
	}
}

配置文件工具类

public class PropertiesUtil {

	public static String getValue(String key){
		Properties prop=new Properties();
		InputStream in=new PropertiesUtil().getClass().getResourceAsStream("/diary.properties");
		try {
			prop.load(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (String)prop.get(key);
	}
}

配置文件

dbUrl=jdbc:mysql://localhost:3306/image_rsstore?useUnicode=true&characterEncoding=utf8
dbUserName=root
dbPassword=
jdbcName=com.mysql.jdbc.Driver

图片读取的工具类:

public class iamge_save {

	// 获取输入流
	public static FileInputStream readImages(String path) throws Exception {
		return new FileInputStream(new File(path));
	}

	// 读取表中图片获取输出流
	public static void readimage(InputStream in, String targetpath) {
		File file = new File(targetpath);
		String path = targetpath.substring(0, targetpath.lastIndexOf("/"));
		if (!file.exists()) {
			new File(path).mkdir();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			int len = 0;
			byte[] buf = new byte[1024];
			while ((len = in.read(buf))!= -1) {
				fos.write(buf, 0, len);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if(fos!=null) {
				try {
					fos.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
		}
	}

}

测试类:

public class restore_image {

	public static void save_image() throws Exception {
		Connection con = null;
		PreparedStatement ps = null;
		String path = "F:/cyt2.jpg";
		FileInputStream in = null;
		DbUtil dbUtil = new DbUtil();
		try {
			in = iamge_save.readImages(path);
			con = (Connection) dbUtil.getCon();
			String sqlString = "insert into image(name, pic) value(?, ?)";
			ps = (PreparedStatement) con.prepareStatement(sqlString);
			ps.setString(1, "first");
			ps.setBinaryStream(2, in);
			int count = ps.executeUpdate();
			if (count > 0) {
				System.err.println("insert is ok!");
			} else {
				System.err.println("insert is error");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			dbUtil.closeCon(con);
			if (ps != null) {
				try {
					ps.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
		}

	}

	public static void read_image() throws Exception {
		String targetpath = "F:/123.jpg";
		DbUtil dbUtil = new DbUtil();
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection = (Connection) dbUtil.getCon();
			String sqlString = "select * from image where name = ?";
			ps = (PreparedStatement) connection.prepareStatement(sqlString);
			ps.setString(1, "first");
			rs = ps.executeQuery();
			while (rs.next()) {
				InputStream inputStream = rs.getBinaryStream("pic");
				iamge_save.readimage(inputStream, targetpath);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			dbUtil.closeCon(connection);

			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
				if (ps != null) {
					try {
						ps.close();
					} catch (Exception e3) {
						// TODO: handle exception
						e3.printStackTrace();
					}

				}

			}
		}
	}

	public static void main(String[] args) throws Exception {
		// DbUtil.main(args);
		save_image();
		read_image();
	}

}

存取图片只能按照二进制的形式装入mysql中,其中牵扯到File操作和IO流操作

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值