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

4
  • 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? Commented Oct 17, 2018 at 3:22
  • i'm new in java, i just follow tutorial. sorry Commented Oct 17, 2018 at 8:06
  • it's my last code, after several changes. Well, I know it's definitely wrong. Commented Oct 17, 2018 at 8:08
  • i have tried this tutorial stackoverflow.com/questions/1076972/… but, it's not work Commented Oct 17, 2018 at 8:11

1 Answer 1

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.