I have the following situation into a Java application.
From the database a retrieve this Blob object that is the representation of a PDF file on the DB:
Blob blobPdf = cedolinoPdf.getAllegatoBlob();
Now I have to convert it into a PDF file. How can I do this task?
Tnx
You can use the getBinaryStream() from the ResultSet, then you can create a file from InputStream returned by the getBinaryStream().
Your code may look something like this
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "sa", "sa");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT fileName,blobFile FROM tblBlobFiles");
if (rs.next()) {
String filename = rs.getString(1);
Blob blob = rs.getBlob(2);
InputStream is = blob.getBinaryStream();
FileOutputStream fos = new FileOutputStream("C:\\DownloadedFiles"+ "\\" + filename);
int b = 0;
while ((b = is.read()) != -1)
{
fos.write(b);
}
}
} catch (IOException e)
{
e.getMessage (); e.printStackTrace();
System.out.println(e);
}
catch (SQLException e)
{
e.getMessage (); e.printStackTrace();
System.out.println(e);
}
If you need all the blobs, do a iteration on the resultset