如何从blob写入磁盘上的映像?

时间:2011-11-30 07:22:11

标签: java

我正在尝试从磁盘上的数据库创建映像文件。我写了以下代码:

{
    oracle.sql.BLOB blob1 = (BLOB) rs.getBlob(1);

    //fillFilePath is file path 
    File blobFile   = new File(fillFilePath);
    String checkExe[]=fillFilePath.split("\\.");

    FileOutputStream  outStream  = new FileOutputStream(blobFile); 
    InputStream inStream   = blob1.getBinaryStream(); 

    int length  = -1; 
    int size    = blob1.getBufferSize(); 
    byte[]  buffer  = new byte[size]; 

    BufferedImage image = ImageIO.read( inStream );
    System.out.println("Inside image upload");

    System.out.println("Inside image jpg");
    ImageIO.write(image, "JPG", outStream);

但它不起作用。

请给我任何建议?

2 个答案:

答案 0 :(得分:0)

尝试:

        BLOB image = ((OracleResultSet) rs).getBLOB("image");            
        blobLength = image.length();
        chunkSize = image.getChunkSize();
        binaryBuffer = new byte[chunkSize];

        for (position = 1; position <= blobLength; position += chunkSize) 
        {                               
            bytesRead = image.getBytes(position, chunkSize, binaryBuffer);               
            outputFileOutputStream.write(binaryBuffer, 0, bytesRead);                
            totbytesRead += bytesRead;
            totbytesWritten += bytesRead;
        }

答案 1 :(得分:0)

BufferedImage bi= ImageIO.read(obj.getPhoto().getBinaryStream());//photo  is Blob.
             File outputfile = new File("folderInYourProject\\"+nameVar+".jpg");
             ImageIO.write(bi, "jpg", outputfile);
相关问题