2011-11-30 83 views
-1

我正在從磁盤上的數據庫創建一個映像文件。我寫了下面的代碼:如何從blob在磁盤上寫入映像?

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

但它不工作。

請給我任何建議?

+0

你得到任何異常? –

+1

blob中有什麼?如果它是一個jpeg,那麼根本不需要使用ImageIO,只需將這些字節寫入磁盤即可。 –

回答

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

我試過上面的代碼數據寫入圖像文件,但它沒有獲得該圖像。 – Arun

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