2016-10-13 15 views
0

BFILE(oracle 10G)にTIFFイメージを格納しました。私はデータベースからそれを読んで、.tiffイメージをローカルディレクトリに作成したい。 (私はJAIを使用しています)。以下は私のコードですImageIO.read()がnullを返すところです。JavaでJAIを使用してBFILE Oracleに格納されたTIFFイメージを作成します。

OraclePreparedStatement pst = 
(OraclePreparedStatement)con.prepareStatement("select chq_tif_img from 
mstr where id = 52"); 

ResultSet rs = pst.executeQuery(); 

if(rs.next()) 
{ 
    bfile = ((OracleResultSet)rs).getBFILE ("chq_tif_img ");  
    bfile.openFile(); 

    System.out.println("BFILE: getDirAlias() = " + bfile.getDirAlias()); 
    System.out.println("BFILE: getName() = " + bfile.getName()); 
    System.out.println("BFILE: fileExists() = " + bfile.fileExists()); 
    System.out.println("BFILE: isFileOpen() = " + bfile.isFileOpen()); 
    System.out.println("BFILE: length = " + bfile.length()); 
    InputStream inputStream = bfile.getBinaryStream(); 
    System.out.println("-->"+inputStream); 
    BufferedImage bi = ImageIO.read(inputStream); 
    TIFFEncodeParam params_omg= new TIFFEncodeParam(); 
    FileOutputStream os_omg = new FileOutputStream("anand.tiff"); 
    javax.media.jai.JAI.create("encode", bi, os_omg, "TIFF", params_omg); 

    inputStream.close(); 
    bfile.closeFile(); 
} 

私はここで検索していたが、私は、データベースからTIFFを読んで正確なヘルプを取得し、.TIFFイメージファイルを作成できませんでした。私を助けてください。

+0

、ファイルをコピーするJAIまたは 'ImageIO'を使用しないでください彼らはそれほど良くはありません。 :-) 'inputStream'の内容を直接ディスクにコピーするだけです。バイトごとにファイルをコピーする方法については、たとえば[この回答](http://stackoverflow.com/a/29005856/1428606)を参照してください。 – haraldK

答えて

0

私のコメントで述べたように、ファイルをコピーするためにJAIまたはImageIOを使用しないでください。非常に良くありません。 :-)

代わりに、InputStreamの内容を直接ディスク(つまりFileOutputStream)にコピーするほうがはるかに高速で互換性があります。私のコメントの線に沿ってあなたのコードを変更する

、あなたが取得します:

OraclePreparedStatement pst = 
(OraclePreparedStatement)con.prepareStatement("select chq_tif_img from 
mstr where id = 52"); 

ResultSet rs = pst.executeQuery(); 

if(rs.next()) 
{ 
    bfile = ((OracleResultSet)rs).getBFILE ("chq_tif_img ");  
    bfile.openFile(); 

    // Skipping debug output for brevity 

    try (InputStream inputStream = bfile.getBinaryStream(); 
     OutputStream os_omg = new FileOutputStream("anand.tiff")) { 
     FileUtils.copy(inputStream, os_omg); 
    } 
    finally { 
     bfile.closeFile(); // Make sure you always close the file when done 
    } 
} 

FileUtils.copyのように実装できます

public void copy(final InputStream in, final OutputStream out) { 
    byte[] buffer = new byte[1024]; 
    int count; 

    while ((count = in.read(buffer)) != -1) { 
     out.write(buffer, 0, count); 
    } 

    // Flush out stream, to write any remaining buffered data 
    out.flush(); 
} 
+0

コードをありがとうございます。それは私の問題を解決しました。 –

関連する問題