Reputation: 482
I want to insert an image from database to microsoft word file. The image format is Blob. I make MS word file using Apache POI. for insert image to MS Word File, it's need
XWPFRun run.addPicture( new FileInputStream(ImageFile) , ImageFormat, ImageName, Units.toEMU(650), Units.toEMU(80));
That function need a FileInputStream object as parameter. So how to make that "Image Blob type" can read in FileInputStream ? I have convert it to be "file" using File Class and to be OutputStream object, but it's not work. this is my last code
................
XWPFRun run1 = paragraph.createRun();
File image = get_dataImage();
String gambar = image.toString();
int imgFormat = getImageFormat(nama_gambar());
run1.addPicture( new FileInputStream(image) , imgFormat, nama_gambar(), Units.toEMU(650), Units.toEMU(80));
CTDrawing drawing = run1.getCTR().getDrawingArray(0);
CTGraphicalObject graphicalobject = drawing.getInlineArray(0).getGraphic();
CTAnchor anchor = getAnchorWithGraphic(graphicalobject, nama_gambar(),
Units.toEMU(600), Units.toEMU(70),
Units.toEMU(-5), Units.toEMU(-10));
drawing.setAnchorArray(new CTAnchor[]{anchor});
drawing.removeInline(0);
........................
function get image file
public static File get_dataImage() throws SQLException, IOException {
File file = null;
Connection conn = Koneksi.getKoneksi();
String sql = "SELECT*FROM setting";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Blob imageBlob = resultSet.getBlob("file_gambar");
byte [] array = imageBlob.getBytes( 1, ( int ) imageBlob.length() );
file = File.createTempFile("something-", ".binary", new File("."));
}
return file;
}
function get image name
public String nama_gambar() throws SQLException {
String nama_gambar = null;
Connection conn = Koneksi.getKoneksi();
String sql = "SELECT*FROM setting";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
nama_gambar = resultSet.getString("nama_gambar");
}
return nama_gambar;
}
Upvotes: 0
Views: 1678
Reputation: 482
I just found the solution. I don't know why I didn't realize it, the solution was very easy. maybe because I was too sleepy from lack of sleep these past few days
public static InputStream get_dataImage() throws SQLException, IOException {
InputStream in = null;
Connection conn = Koneksi.getKoneksi();
String sql = "SELECT*FROM setting";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Blob imageBlob = resultSet.getBlob("file_gambar");
in = imageBlob.getBinaryStream();
}
return in;
}
Upvotes: 1