【发布时间】:2020-02-28 21:41:34
【问题描述】:
我计划使用一个公钥加密一个字符串,该公钥作为 Blob 存储在数据库中。我创建了一个名为 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 的方法。
【问题讨论】:
-
它根本不起作用。您没有写入每次读取的数量,因此您正在将垃圾写入文件。没有什么说你必须在任何地方写它。不清楚你在问什么。