2016-10-13 6 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를 읽고 정확한 도움을받을 수 및 이미지 파일을 티파니 만들 수 없습니다. 도와주세요.

+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

코드 주셔서 감사합니다. 그것은 내 문제를 해결했다. –