在 java 中写入 ORACLE BLOB 文件
Writing an ORACLE BLOB file in java
我从我的 D 驱动器上传了一个 html 文件到 ORACLE 数据库,现在我试图用新名称将同一个文件检索到我电脑中的另一个驱动器,但我无法收到完全相同的文件,下面列出了我使用的代码。
我的问题是如何获得与我存储在数据库中的文件相同的副本。
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)
永远不会显示在文件中或输出中,因为您已经读取了下一个字节以将其写出。
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);
}
我从我的 D 驱动器上传了一个 html 文件到 ORACLE 数据库,现在我试图用新名称将同一个文件检索到我电脑中的另一个驱动器,但我无法收到完全相同的文件,下面列出了我使用的代码。 我的问题是如何获得与我存储在数据库中的文件相同的副本。
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)
永远不会显示在文件中或输出中,因为您已经读取了下一个字节以将其写出。
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);
}