我是Java的新手。我正在尝试检索和显示存储在 MySQL 数据库中的一些图像。当我尝试将 blob 对象转换为输入流或字节数组时,出现此错误:java.sql.SQLException: Not a valid escape sequence
我尝试使用 getBinaryStream() 将 blob 对象转换为 InputStream:
public List<InputStream> fetchImages() {
List<InputStream> inputStreams = new ArrayList<>();
String query = "SELECT * from product WHERE image IS NOT NULL ";
try (Statement statement = connection.createStatement()) {
ResultSet result = statement.executeQuery(query);
while (result.next()) {
Blob blob = result.getBlob("image");
InputStream stream = blob.getBinaryStream();
inputStreams.add(stream);
}
} catch (SQLException | NullPointerException error) {
error.printStackTrace();
}
return inputStreams;
}
然后我尝试了类似的东西:
Blob blob = result.getBlob("image");
byte[] bytes = blob.getBytes(1, (int) blob.length());
它给出了同样的错误。