当前位置: 技术问答>java相关
oracle的blob类型读写文件,help me
来源: 互联网 发布时间:2015-07-21
本文导语: 我的环境,tomcat 3.32 + oracle + win2000 在blob中读写都没有返回错误,可是写出来得excel文件,提示不能识别的文件格式 打开后,里面什么也没有,为什么?? 写入blob代码() try{ String fileurl = "d:/1.xls"; File f = n...
我的环境,tomcat 3.32 + oracle + win2000
在blob中读写都没有返回错误,可是写出来得excel文件,提示不能识别的文件格式
打开后,里面什么也没有,为什么??
写入blob代码()
try{
String fileurl = "d:/1.xls";
File f = new File(fileurl);
FileInputStream is=new FileInputStream(f);
String sql="insert into excel(id,excel,dx0) values(?,?,?)";
Statement st = conn.createStatement();
FileInputStream str=new FileInputStream(f);
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"fff");
pstmt.setBinaryStream(2,str,str.available());
pstmt.setInt(3,str.available());
pstmt.execute();
}catch(Exception e){System.out.println(e);}
读取blob代码()
try{
Connection conn = ConnBean.getConnection();
Statement stmt=conn.createStatement();
ResultSet rset=stmt.executeQuery("SELECT dx0,excel from excel where id = 'fff'");
while (rset.next())
{// Get LOB locators into Java wrapper classes.
int id=rset.getInt(1);//获取文件长度。
BLOB blob=((OracleResultSet)rset).getBLOB(2);//获取文件字段。
// int chunk=blob.getChunkSize();
int chunk=id;
byte [] buffer=new byte[chunk];
System.out.println(chunk);
File fileout = new File("d:/");
File ftemp = fileout.createTempFile("bbb" + "_",".xls",fileout);
// FileOutputStream fouts = new FileOutputStream(ftemp);
//File binaryFile=new File("d:/3.xls");
FileOutputStream fileoutstream=new FileOutputStream(ftemp);//创建文件输出流。
InputStream instream=blob.getBinaryStream();//建立输入流,并将字段comment的值以流的形式,放入instream变量。
instream.read(buffer,0,chunk);//将文件流存入变量buffer,以buffer为中转
fileoutstream.write(buffer,0,chunk);//将buffer写入文件输出流。
System.out.println("write ok!");
instream.close();//关闭流
fileoutstream.close();
}
rset.close();
stmt.close();
conn.close();
System.out.print("ok");
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
帮我看看,要多少分都给
在blob中读写都没有返回错误,可是写出来得excel文件,提示不能识别的文件格式
打开后,里面什么也没有,为什么??
写入blob代码()
try{
String fileurl = "d:/1.xls";
File f = new File(fileurl);
FileInputStream is=new FileInputStream(f);
String sql="insert into excel(id,excel,dx0) values(?,?,?)";
Statement st = conn.createStatement();
FileInputStream str=new FileInputStream(f);
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"fff");
pstmt.setBinaryStream(2,str,str.available());
pstmt.setInt(3,str.available());
pstmt.execute();
}catch(Exception e){System.out.println(e);}
读取blob代码()
try{
Connection conn = ConnBean.getConnection();
Statement stmt=conn.createStatement();
ResultSet rset=stmt.executeQuery("SELECT dx0,excel from excel where id = 'fff'");
while (rset.next())
{// Get LOB locators into Java wrapper classes.
int id=rset.getInt(1);//获取文件长度。
BLOB blob=((OracleResultSet)rset).getBLOB(2);//获取文件字段。
// int chunk=blob.getChunkSize();
int chunk=id;
byte [] buffer=new byte[chunk];
System.out.println(chunk);
File fileout = new File("d:/");
File ftemp = fileout.createTempFile("bbb" + "_",".xls",fileout);
// FileOutputStream fouts = new FileOutputStream(ftemp);
//File binaryFile=new File("d:/3.xls");
FileOutputStream fileoutstream=new FileOutputStream(ftemp);//创建文件输出流。
InputStream instream=blob.getBinaryStream();//建立输入流,并将字段comment的值以流的形式,放入instream变量。
instream.read(buffer,0,chunk);//将文件流存入变量buffer,以buffer为中转
fileoutstream.write(buffer,0,chunk);//将buffer写入文件输出流。
System.out.println("write ok!");
instream.close();//关闭流
fileoutstream.close();
}
rset.close();
stmt.close();
conn.close();
System.out.print("ok");
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
帮我看看,要多少分都给
|
package test;
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.driver.*;
//needed for new CLOB and BLOB classes
import oracle.sql.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author yukm
* @version 1.0
*/
public class TestOracle {
static void readFromFile (BLOB blob,File file) throws Exception {
File binaryFile = file;
FileInputStream in = new FileInputStream(binaryFile);
OutputStream out = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
System.out.print("The chunk size is " + chunk);
byte [] buffer = new byte[chunk ];
int length;
while ((length = in.read(buffer)) != -1)
out.write(buffer, 0, length);
in.close();
out.close();
}
static void writeToFile (BLOB blob,File file) throws Exception {
int chunk = blob.getChunkSize();
byte[] buffer = new byte[chunk];
int length;
FileOutputStream outFile = null;
outFile = new FileOutputStream(file);
InputStream instream = blob.getBinaryStream();
while ((length = instream.read(buffer)) != -1) {
outFile.write(buffer, 0, length);
}
instream.close();
outFile.close();
}
public static void main(String args[]){
try {
Driver drv=new oracle.jdbc.driver.OracleDriver();
Properties props=new Properties();
props.put("user","system");
props.put("password","manager");
Connection con=drv.connect("jdbc:oracle:thin:@168.200.8.25:1521:TEST",props);
con.setAutoCommit(false);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM music");
while(rs.next())
{
System.out.println(rs.getString("NAME"));
}
rs.close();
BLOB blob=null;
st.execute("INSERT INTO music VALUES('家家有本难念的经',3,'../music/jiajiayoubennianniandejing.mp3',empty_blob())");
st.execute("COMMIT");
rs=st.executeQuery("SELECT * FROM music WHERE ID=3 FOR UPDATE");
while(rs.next())
{
blob = ((OracleResultSet)rs).getBLOB("CONTENT");
// blob=(BLOB)rs.getBlob("CONTENT");
}
File file=new File("d:\mypic.jpg");
readFromFile(blob,file);
file=new File("d:\mypic1.jpg");
writeToFile(blob,file);
con.close();
}catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
import java.sql.*;
import java.io.*;
import java.util.*;
import oracle.jdbc.driver.*;
//needed for new CLOB and BLOB classes
import oracle.sql.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author yukm
* @version 1.0
*/
public class TestOracle {
static void readFromFile (BLOB blob,File file) throws Exception {
File binaryFile = file;
FileInputStream in = new FileInputStream(binaryFile);
OutputStream out = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
System.out.print("The chunk size is " + chunk);
byte [] buffer = new byte[chunk ];
int length;
while ((length = in.read(buffer)) != -1)
out.write(buffer, 0, length);
in.close();
out.close();
}
static void writeToFile (BLOB blob,File file) throws Exception {
int chunk = blob.getChunkSize();
byte[] buffer = new byte[chunk];
int length;
FileOutputStream outFile = null;
outFile = new FileOutputStream(file);
InputStream instream = blob.getBinaryStream();
while ((length = instream.read(buffer)) != -1) {
outFile.write(buffer, 0, length);
}
instream.close();
outFile.close();
}
public static void main(String args[]){
try {
Driver drv=new oracle.jdbc.driver.OracleDriver();
Properties props=new Properties();
props.put("user","system");
props.put("password","manager");
Connection con=drv.connect("jdbc:oracle:thin:@168.200.8.25:1521:TEST",props);
con.setAutoCommit(false);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM music");
while(rs.next())
{
System.out.println(rs.getString("NAME"));
}
rs.close();
BLOB blob=null;
st.execute("INSERT INTO music VALUES('家家有本难念的经',3,'../music/jiajiayoubennianniandejing.mp3',empty_blob())");
st.execute("COMMIT");
rs=st.executeQuery("SELECT * FROM music WHERE ID=3 FOR UPDATE");
while(rs.next())
{
blob = ((OracleResultSet)rs).getBLOB("CONTENT");
// blob=(BLOB)rs.getBlob("CONTENT");
}
File file=new File("d:\mypic.jpg");
readFromFile(blob,file);
file=new File("d:\mypic1.jpg");
writeToFile(blob,file);
con.close();
}catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
}
|
http://www.csdn.net/Expert/TopicView.asp?id=83096&datebasetype=200101