Trong db của mình có bảng image chỉ có 2 trường id và data_img,
mình read inputstream sau đó copy ảnh đó ra 1 file khác nhưng chất lượng hình ảnh tệ quá, làm sao để giữ nguyên chất lượng hình ảnh nhỉ. Đây là example của mình
EDIT: ảnh trong db mình cũng lưu dưới dạng inputStream kiểu dữ liệu blob, khi copy qua thì kích thước ảnh không đổi chỉ chất lượng thì giảm đi thôi.
public class CopyImageTest {
public static void main(String[] args) throws SQLException, IOException {
CopyImageTest imageTest = new CopyImageTest();
imageTest.insertImage();
}
public Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "longpn", "xxxxx");
} catch (Exception e) {
System.out.println("Error Occured While Getting the Connection: - " + e);
}
return connection;
}
public void insertImage() throws IOException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection();
statement = connection.prepareStatement("select data_img from image");
ResultSet rs = statement.executeQuery();
while (rs.next()) {
InputStream stream = rs.getBinaryStream("data_img");
copyInputStreamToFile(stream, new File("D:\\a.png"));
}
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException: - " + e);
} catch (SQLException e) {
System.out.println("SQLException: - " + e);
} finally {
try {
connection.close();
statement.close();
} catch (SQLException e) {
System.out.println("SQLException Finally: - " + e);
}
}
}
private static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
}
}
}