简体   繁体   中英

Writing an ORACLE BLOB file in java

I uploaded a html file from my D drive to an ORACLE database, now i am trying to retrieve the same file to another drive in my pc with a new name but i am not able to receive the exact same file, The code that i have used is listed below. My question is how do i get the same copy of the file that i stored in my database.

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

The bytes you read here

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

will never show in File or out as you already read the next byte to write it out.

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

Try reading into a byte array and write out the number of bytes read like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM