簡體   English   中英

如何從FileInputStream中的數據庫讀取Image Blob類型

[英]How to read Image Blob type from Database in FileInputStream

我想將圖像從數據庫插入到Microsoft Word文件中。 圖像格式為Blob。 我使用Apache POI制作MS Word文件。 插入圖像到MS Word文件,這是需要的

XWPFRun run.addPicture( new FileInputStream(ImageFile) , ImageFormat, ImageName, Units.toEMU(650), Units.toEMU(80));

該函數需要一個FileInputStream對象作為參數。 那么如何使“ Image Blob類型”可以在FileInputStream中讀取呢? 我已經使用文件類將其轉換為“文件”,並轉換為OutputStream對象,但是它不起作用。 這是我的最后一個代碼

................    
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);
........................

功能獲取圖像文件

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

函數獲取圖像名稱

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

這是結果 在此處輸入圖片說明

我剛剛找到了解決方案。 我不知道為什么我沒有意識到,解決方案非常簡單。 可能是因為這幾天我由於缺乏睡眠而太困了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM