Java 从oracle 11g xe获取Blob文件时创建的空文件
下面给出了两个程序 1> 将blob文件存储到数据库中 及 2> 从数据库检索blob文件 1。将blob文件存储到数据库中Java 从oracle 11g xe获取Blob文件时创建的空文件,java,sql,oracle,jdbc,oracle11g,Java,Sql,Oracle,Jdbc,Oracle11g,下面给出了两个程序 1> 将blob文件存储到数据库中 及 2> 从数据库检索blob文件 1。将blob文件存储到数据库中 package jdbc; import java.net.URL; import java.sql.*; import java.io.*; import java.util.Properties; public class Storing_Image { public static void main(String[] args) throws Except
package jdbc;
import java.net.URL;
import java.sql.*;
import java.io.*;
import java.util.Properties;
public class Storing_Image {
public static void main(String[] args) throws Exception{
Properties p = new Properties();
p.put("user", "system");
p.put("password", "password");
URL url = Storing_Clob.class.getResource("/images/bunny.jpg");
File file = new File(url.toURI());
FileInputStream fis = new FileInputStream(file);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);
PreparedStatement pstmt = con.prepareStatement("insert into myblob(BLOB_FILE) values(?)");
pstmt.setBinaryStream(1, fis, (byte)file.length());
System.out.println("Length of string: "+file.length());
System.out.println("No of rows affected: "+pstmt.executeUpdate());
con.close();
}
}
import java.sql.*;
import java.util.Properties;
import java.io.*;
public class Retrieving_Image {
public static void main(String[] args) throws Exception{
Properties p = new Properties();
p.put("user", "system");
p.put("password", "password");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);
PreparedStatement pstmt = con.prepareStatement("select BLOB_FILE from myblob",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pstmt.executeQuery();
rs.first();
InputStream is = rs.getBinaryStream(1);
FileOutputStream fos = new FileOutputStream("H:/result1.jpg");
while(((byte) is.read())!=-1)
{
fos.write((byte) is.read());
}
fos.close();
con.close();
}
}
myblob表只有一个Blob数据类型的列
插入表后,我执行以下命令:-
select * from myblob;
它给出了以下输出:-
我猜这是表中图片的字节值
2.从数据库检索Blob文件(图像)
package jdbc;
import java.net.URL;
import java.sql.*;
import java.io.*;
import java.util.Properties;
public class Storing_Image {
public static void main(String[] args) throws Exception{
Properties p = new Properties();
p.put("user", "system");
p.put("password", "password");
URL url = Storing_Clob.class.getResource("/images/bunny.jpg");
File file = new File(url.toURI());
FileInputStream fis = new FileInputStream(file);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);
PreparedStatement pstmt = con.prepareStatement("insert into myblob(BLOB_FILE) values(?)");
pstmt.setBinaryStream(1, fis, (byte)file.length());
System.out.println("Length of string: "+file.length());
System.out.println("No of rows affected: "+pstmt.executeUpdate());
con.close();
}
}
import java.sql.*;
import java.util.Properties;
import java.io.*;
public class Retrieving_Image {
public static void main(String[] args) throws Exception{
Properties p = new Properties();
p.put("user", "system");
p.put("password", "password");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);
PreparedStatement pstmt = con.prepareStatement("select BLOB_FILE from myblob",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pstmt.executeQuery();
rs.first();
InputStream is = rs.getBinaryStream(1);
FileOutputStream fos = new FileOutputStream("H:/result1.jpg");
while(((byte) is.read())!=-1)
{
fos.write((byte) is.read());
}
fos.close();
con.close();
}
}
这将提供以下输出:-
正如您所看到的,这是一个空白图像,即0kb。
我正在使用Oracle11gXe和ojdbc6_g.jar驱动程序 你试过fos.flush()吗;关闭前?@ThrashBean是的。它不会改变任何东西。您是否进行了一些调试并检查while循环是否已执行或
is.read()
是否在第一次读取时停止?再看看@bellabax我做了如下工作:int counter=0;while((is.read())!=-1){System.out.println(“计数器是:+(++计数器));fos.write((字节)是.read());}
它给我以下输出:-计数器是:1计数器是:2计数器是:3计数器是:4计数器是:5计数器是:6计数器是:7计数器是:8计数器是:9计数器是:10
请用我提供的sql cmd输出图像告诉它。thnks。