store and retrieve image data to any database in java


We will insert image data into database in byte[] format

Here is a piece of code to convert image data into byte[]:

 // image file
File file = new File("images/extjsfirstlook.jpg");  
// create byte with specified size 
byte[] bFile = new byte[(int) file.length()]; 
try {  
  FileInputStream fileInputStream = new FileInputStream(file); 
  // bFile is initialized
  fileInputStream.read(bFile);
  fileInputStream.close();
   } catch (Exception e) {
 e.printStackTrace(); 
 }
//now you can store bFile in database
 
 
 
Here is a piece of code to convert byte[] data into image:

try {  
    // create output stream with a file path where you want to store 
    // your image on your drive
    FileOutputStream fos = FileOutputStream("images/output.jpg");
    // it will write data byte data into the file specified 
    fos.write(book.getImage());
    fos.close()
   } catch (Exception e) {
  e.printStackTrace(); 
}
// now you can use your image  
 

Comments

Popular posts from this blog

Java 21 Features With Example

Java 10 and 10 Big Java Milestones