2015-11-20 39 views
0

我從我的D盤上傳了一個html文件到一個ORACLE數據庫,現在我試圖用一個新名稱在我的電腦中檢索同一個文件到另一個驅動器,但我無法接收完全相同的文件,我已經使用的代碼列在下面。 我的問題是我如何得到我存儲在我的數據庫中的文件的相同副本。在java中編寫ORACLE BLOB文件

import java.io.FileWriter; 

import java.io.FileWriter; 
import java.io.InputStream; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

public class RBLOB { 
public static void main(String args[])throws Exception 
{ 
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");) 
{ 
PreparedStatement ps=con.prepareStatement("select * from players"); 
ResultSet rs=ps.executeQuery(); 
rs.next(); 
InputStream is= rs.getBinaryStream(2); 

FileWriter fw=new FileWriter("C:\\Users\\y_san\\Desktop\\test.html"); 
while(is.read()!=-1) 
{ 
    fw.write(is.read()); 
System.out.println(is.read()); 
fw.flush(); 
} 
} 
catch(Exception ex) 
{ 
System.out.println(ex.getMessage()); 
} 
} 
} 

回答

0

的字節你讀到這裏

while(is.read()!=-1) 

絕不會在文件顯示或出,你已經讀過的下一個字節寫出來。

while(is.read()!=-1) { // byte 1 read 
fw.write(is.read()); // byte 2 read  
System.out.println(is.read()); // byte 3 read 

嘗試讀入一個字節數組寫出來念想

byte [] buf = new byte [1024]; 
    int len = is.read(buf); 
    while (len > 0){ 
     fw.write (buf,0,len); 
     // more stuff 

     // read next chunk 
     len = is.read(buf); 
    } 
+0

yeah..i得到它的字節數,感謝ü – SKY

+0

- 也我取代我用FileWriter的文件和FileOutputStream中,因爲文件編寫器不支持寫入字節。 – SKY