【问题标题】:Create TIFF image stored in BFILE Oracle using JAI in java在 java 中使用 JAI 创建存储在 BFILE Oracle 中的 TIFF 图像
【发布时间】:2017-02-22 05:56:13
【问题描述】:

我已将 TIFF 图像存储在 BFILE (oracle 10G) 中。我想从数据库中读取它并将 .tiff 图像创建到本地目录。 (我正在使用 JAI)。以下是我的代码,ImageIO.read() 返回 null。

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 图像文件方面无法获得确切的帮助。请帮我。

【问题讨论】:

  • 不要使用JAI或ImageIO来复制文件,他们不太擅长。 :-) 只需将inputStream 的内容直接复制到磁盘即可。例如,请参阅 this answer 了解如何逐字节复制文件。

标签: java inputstream fileoutputstream jai


【解决方案1】:

正如我在评论中提到的,你不应该使用 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();
}

【讨论】:

  • 非常感谢您的代码。它解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多