0

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;
    }

this is the result enter image description here

JuNas
  • 432
  • 2
  • 7
  • 16
  • 1
    After `file = File.createTempFile("something-", ".binary", new File("."));` the `file` is empty. Or how do you think the `byte [] array` magically comes into the `file`? And why not simply using [Blob.getBinaryStream](https://docs.oracle.com/javase/8/docs/api/java/sql/Blob.html#getBinaryStream--)? – Axel Richter Oct 17 '18 at 03:22
  • i'm new in java, i just follow tutorial. sorry – JuNas Oct 17 '18 at 08:06
  • it's my last code, after several changes. Well, I know it's definitely wrong. – JuNas Oct 17 '18 at 08:08
  • i have tried this tutorial https://stackoverflow.com/questions/1076972/snippet-to-create-a-file-from-the-contents-of-a-blob-in-java but, it's not work – JuNas Oct 17 '18 at 08:11

1 Answers1

1

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;
        }
JuNas
  • 432
  • 2
  • 7
  • 16