简体   繁体   English

用Java编写ORACLE BLOB文件

[英]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. 我将D驱动器中的html文件上传到ORACLE数据库中,现在我正尝试使用新名称将同一文件检索到PC中的另一个驱动器中,但是我无法收到完全相同的文件,而我拥有的代码下面列出了使用的。 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. 因为您已经读取了下一个字节以将其写入,所以它永远不会显示在File或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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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