2013-10-30 50 views
0

我想從一個oracle數據庫提取一個TIF文件。大部分從blob轉換而來的文件都能正常工作,但有些文件似乎缺少幾千字節。提取斑點到文件:一些文件缺少數據

例如,當我使用SQL GUI提取文件時,我得到一個大小爲912,390字節的TIF文件。當我使用我的方法時,我得到一個大小爲909,312字節的文件。缺少的3078字節意味着我無法打開文件。我在做什麼導致文件不完整?

//... 
if ((FileOutDir != null) && (blobSQL != null) && (conn != null)) { 
     PreparedStatement selBlobs = null; 
     OutputStream fos = null; 

     if (conn != null) { 
      if (blobSQL != null) { 
       try { 

        selBlobs = conn.prepareStatement(blobSQL); 
        ResultSet rs = selBlobs.executeQuery(); 

        Blob blob = null; 
        InputStream is = null; 

        int b = 0; 
        int cols = rs.getMetaData().getColumnCount(); 

        String filepath = FileOutDir; 

        while (rs.next()) { 

         blob = rs.getBlob(1); 
         is = blob.getBinaryStream(); 
         filepath += "/"; 

         for (int c = 2; c <= cols; c++) { 
          filepath += rs.getObject(c).toString() + "_"; 
         } 
         filepath = filepath.substring(0, 
           filepath.length() - 1); 
         filepath += fileSuffix; 

         fos = new BufferedOutputStream(new FileOutputStream(filepath)); 

         while ((b = is.read()) != -1) { 
          fos.write(b); 
         } 

         filepath = FileOutDir; 
         b = 0; 
        } 

       } catch (Exception e) { 
        JOptionPane.showMessageDialog(gui, e.toString()); 
       } finally { 
        try { 
         selBlobs.close(); 
         fos.close(); 
        } catch (Exception e2) { 
         System.out 
           .println("Problem closing BlobToFile connections: " 
             + e2.toString()); 
        } 
       } 
//... 
+1

你可以嘗試在fos.close()之前加上'fos.flush();'看看它是否有幫助? –

+0

這似乎已經解決了這個問題,但我將不得不嘗試使用更大的設置來確保。如果它與10,000行一起工作,我會留下一個答案。再次感謝! – Native

+1

我會將此添加爲答案,以便在問題有幫助時關閉該問題。 –

回答

1

嘗試在fos.close();之前加fos.flush();,看看是否有幫助。

+0

仍然可以使用10,000行。再次感謝Przemyslaw Kruglej,我感謝幫助! – Native