读取 Blob 文件而不将其保存为文件
Read Blob file without saving it a file
我计划使用存储在数据库中的 public 密钥来加密字符串,因为 Blob.I 已经创建了名为 readBlob()
的方法来读取密钥并将其保存到文件。
public static void readBlob(int userid, String filename) throws SQLException, IOException {
String url = "jdbc:mysql://localhost:3306/bank";
String user = "root";
String password = "root";
String email=null;
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from users where user_id=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setLong(1, userid);
ResultSet result = statement.executeQuery();
File file = new File(filename);
@SuppressWarnings("resource")
FileOutputStream output = new FileOutputStream(file);
String keys=null;
while(result.next()) {
byte[] buffer = new byte[1];
InputStream input = result.getBinaryStream("key");
while (input.read(buffer) > 0) {
output.write(buffer);
}
}
}
效果很好,但我不需要将它保存到本地文件,我需要一种无需保存即可读取 Blob 的方法。
您可以使用getBlob
Blob key = result.getBlob("key")
Retrieves the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.
我计划使用存储在数据库中的 public 密钥来加密字符串,因为 Blob.I 已经创建了名为 readBlob()
的方法来读取密钥并将其保存到文件。
public static void readBlob(int userid, String filename) throws SQLException, IOException {
String url = "jdbc:mysql://localhost:3306/bank";
String user = "root";
String password = "root";
String email=null;
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "select * from users where user_id=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setLong(1, userid);
ResultSet result = statement.executeQuery();
File file = new File(filename);
@SuppressWarnings("resource")
FileOutputStream output = new FileOutputStream(file);
String keys=null;
while(result.next()) {
byte[] buffer = new byte[1];
InputStream input = result.getBinaryStream("key");
while (input.read(buffer) > 0) {
output.write(buffer);
}
}
}
效果很好,但我不需要将它保存到本地文件,我需要一种无需保存即可读取 Blob 的方法。
您可以使用getBlob
Blob key = result.getBlob("key")
Retrieves the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.