I am using hibernate to insert a pdf file as bytes into database using the below code.
File file = new File("D:\\test.pdf");
byte[] imageData = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(imageData);
fileInputStream.close();
emp.setClobdata(new String(imageData));
session.save(emp);
Then I am trying to write the file from database to a pdf file. Using the below code. But the pdf is getting corrupted.
Emp emp = (Emp) session.get(Emp.class, 2);
byte[] b = emp.getClobdata().getBytes();
FileOutputStream fout = new FileOutputStream("D:\\some.pdf");
fout.write(b); fout.flush();
What I am doing wrong with this code?