簡體   English   中英

用Java編寫ORACLE BLOB文件

[英]Writing an ORACLE BLOB file in java

我將D驅動器中的html文件上傳到ORACLE數據庫中,現在我正嘗試使用新名稱將同一文件檢索到PC中的另一個驅動器中,但是我無法收到完全相同的文件,而我擁有的代碼下面列出了使用的。 我的問題是如何獲取與數據庫中存儲文件相同的副本。

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());
}
}
}

您在此處讀取的字節

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

因為您已經讀取了下一個字節以將其寫入,所以它永遠不會顯示在File或out中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM