如何将图片BASE64加密保存到数据库BLOB类型中,并获取数据重新生成图片

在实际项目运行中,我们需要将图片,world ,excel等信息保存到数据库,等需要用到的时候在从中读取出来,并使用。这些信息在oracle数据库中都保存在类型BLOB中,放入前可以进行信息的加密(BASE64)。

例如:oracle中新建表TEACHER

Demo:

/**
	 * 数据库中加入图片等文件
	 */
	public static void InsertPic() {
		String sql = "insert into TEACHER(ID,NAME,PHOTO,BASE64PHOTO) values('1',?,?,?)";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
							"jdbc:oracle:thin:@//192.168.1.183:1521/orcl",
							"LWW", "lww");
			pStmt = conn.prepareStatement(sql);

			pStmt.setString(1, "Lily");// 设置字段NAME值

			// 数据库插入图片/word/excel等
			File file = new File("src/pic.jpg");
			FileInputStream fis = new FileInputStream(file);

			//创建一个和文件大小一样的缓冲区
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			pStmt.setBytes(2, buffer);//内容设置到字段PHOTO中
			System.out.println("未加密图片长度:"+fis.available());
			
			String base64 = new BASE64Encoder().encode(buffer);
			
			System.out.println("加密后图片长度:" +base64.length());
			pStmt.setBytes(3, base64.getBytes());//内容设置到字段BASE64PHOTO中
			int n = pStmt.executeUpdate();
			System.out.println(n + "条记录插入");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException| IOException e) {
			e.printStackTrace();
		} finally {
			try {
				pStmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 从数据库中取出图片等文件
	 */
	public static void getPic() {
		String sql = "select ID,NAME,PHOTO,BASE64PHOTO from TEACHER where ID=?";
		try {
			conn = DriverManager.getConnection(
							"jdbc:oracle:thin:@//192.168.1.183:1521/orcl",
							"LWW", "lww");
			pStmt = conn.prepareStatement(sql);
			pStmt.setString(1, "1");
			ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				//获取图片字段
//				FileOutputStream fos = new FileOutputStream(new File("abc.jpg"));
//				InputStream is = rs.getBinaryStream("photo");
//
//				byte[] buffer = new byte[4 * 1024];
//				int length = 0;
//				while ((length = is.read(buffer)) != -1) {
//					fos.write(buffer, 0, length);
//				}
				
				//获取图片加密字段内容,并进行base64解密
				FileOutputStream fos = new FileOutputStream(new File("abcbase64.jpg"));
				byte[] basebt = rs.getBytes("base64photo");
				
				System.out.println("加密:" +basebt.length);
				byte[] by = new BASE64Decoder().decodeBuffer(new String(basebt));
				System.out.println("解密:" +by.length);
				fos.write(by, 0, by.length);			
				fos.flush();
				fos.close();
//				is.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				pStmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

完整Demo下载:https://download.csdn.net/download/luoww1/10848498 (支持oracle)

https://download.csdn.net/download/luoww1/10859333 (支持mysql,oracle,sqlserver)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值