gpt4 book ai didi

java - 读取 Blob 文件而不将其保存为文件

转载 作者:行者123 更新时间:2023-12-01 22:07:51 41 4
gpt4 key购买 nike

我计划使用公钥加密字符串,该公钥作为 Blob 存储在数据库中。我创建了名为 readBlob() 的方法来读取 key 并将其保存到文件中。

  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.

关于java - 读取 Blob 文件而不将其保存为文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678762/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com