怎样把图像文件写到BLOB 字段中

ninibaobei 2003-08-29 10:22:54
怎样把图像文件写到BLOB 字段中,用jsp 和java
...全文
48 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
beckhambobo 2003-11-21
  • 打赏
  • 举报
回复
http://search.csdn.net/expert/topic/61/6101/2002/11/5/1151465.htm
jackiedlh 2003-11-21
  • 打赏
  • 举报
回复
高手!
l2g32003 2003-08-29
  • 打赏
  • 举报
回复
///// writeCLOB 对ascii文件的写
writeBLOB 对二进制文件的写
writeBFILE
*/

// import the JDBC packages
import java.sql.*;
import java.io.*;

// import the Oracle JDBC extension packages
import oracle.sql.*;
import oracle.jdbc.*;

public class LobExample1 {

public static void main(String [] args)
throws SQLException, IOException {

// register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
);

// create a Connection object, and connect to the database
// as lob_user using the Oracle JDBC Thin driver
Connection myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"lob_user",
"lob_password"
);

// disable auto-commit mode
myConnection.setAutoCommit(false);

// create a statement object
Statement myStatement = myConnection.createStatement();

String sourceDirectory = "C:\\sample_files\\";
writeCLOB(myStatement, sourceDirectory + "textContent.txt");
writeBLOB(myStatement, sourceDirectory + "binaryContent.doc");
addBFILE(myStatement, "SAMPLE_FILES_DIR", "textContent.txt");
addBFILE(myStatement, "SAMPLE_FILES_DIR", "binaryContent.doc");

// close the JDBC objects
myStatement.close();
myConnection.close();

} // end of main()


private static void writeCLOB(
Statement myStatement,
String fileName
) throws SQLException, IOException {

// step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO clob_content(file_name, clob_column) " +
"VALUES ('" + fileName + "', EMPTY_CLOB())"
);

// step 2: retrieve the row containing the LOB locator
ResultSet clobResultSet = myStatement.executeQuery(
"SELECT clob_column " +
"FROM clob_content " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
clobResultSet.next();

// step 3: create a LOB object and read the LOB locator
CLOB myClob =
((OracleResultSet) clobResultSet).getCLOB("clob_column");

// step 4: get the chunk size of the LOB from the LOB object
int chunkSize = myClob.getChunkSize();

// step 5: create a buffer to hold a block of data from the file
char [] textBuffer = new char[chunkSize];

// step 6: create a file object
File myFile = new File(fileName);

// step 7: create input stream objects to read the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile);
InputStreamReader myReader =
new InputStreamReader(myFileInputStream);
BufferedReader myBufferedReader = new BufferedReader(myReader);

// step 8: read the file contents and write it to the LOB
long position = 1;
int charsRead;

while ((charsRead = myBufferedReader.read(textBuffer)) != -1) {

// write the buffer contents to myClob using the putChars() method
myClob.putChars(position, textBuffer);

// increment the end position
position += charsRead;

} // end of while

// step 9: perform a commit
myStatement.execute("COMMIT");

// step 10: close the objects used to read the file
myBufferedReader.close();
myReader.close();
myFileInputStream.close();

System.out.println("Wrote content from file " +
fileName + " to CLOB");

} // end of writeCLOB()


private static void writeBLOB(
Statement myStatement,
String fileName
) throws SQLException, IOException {

// step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO blob_content(file_name, blob_column) " +
"VALUES ('" + fileName + "', EMPTY_BLOB())"
);

// step 2: retrieve the row containing the LOB locator
ResultSet blobResultSet = myStatement.executeQuery(
"SELECT blob_column " +
"FROM blob_content " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
blobResultSet.next();

// step 3: create a LOB object and read the LOB locator
BLOB myBlob =
((OracleResultSet) blobResultSet).getBLOB("blob_column");

// step 4: get the chunk size of the LOB from the LOB object
int chunkSize = myBlob.getChunkSize();

// step 5: create a buffer to hold a block of data from the file
byte [] byteBuffer = new byte[chunkSize];

// step 6: create a file object to open the file
File myFile = new File(fileName);

// step 7: create an input stream object to read the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile);

// step 8: read the file contents and write it to the LOB
long position = 1;
int bytesRead;

while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) {

// write the buffer contents to myBlob using the putBytes() method
myBlob.putBytes(position, byteBuffer);

// increment the end position
position += bytesRead;

} // end of while

// step 9: perform a COMMIT
myStatement.execute("COMMIT");

// step 10: close the objects used to read the file
myFileInputStream.close();

System.out.println("Wrote content from file " +
fileName + " to BLOB");

} // end of writeBLOB()


private static void addBFILE(
Statement myStatement,
String directory,
String fileName
) throws SQLException {

myStatement.executeUpdate(
"INSERT INTO bfile_content(file_name, bfile_column) " +
"VALUES ('" + fileName + "', " +
"BFILENAME('" + directory + "', '" + fileName + "'))"
);
myStatement.execute("COMMIT");

System.out.println("Added pointer to file " +
fileName + " to BFILE in database directory " + directory);

} // end of addBFILE()

}
LGQDUCKY 2003-08-29
  • 打赏
  • 举报
回复
些一个存储过程,用JAVA调用
前提工作
grant create any directory to scott;
grant create any library to scott;
create or replace directory utllobdir as 'd:\oracle';
create table bfile_tab (bfile_column BFILE);
create table utl_lob_test (blob_column BLOB);

set serveroutput on

然后执行下面语句就将d:\oracle目录下的A.jpg存入到utl_lob_test
表中的blob_column字段中了。
declare
a_blob BLOB;
a_bfile BFILE := BFILENAME('UTLLOBDIR','A.jpg');
begin
insert into bfile_tab values (a_bfile)
returning bfile_column into a_bfile;
insert into utl_lob_test values (empty_blob())
returning blob_column into a_blob;
dbms_lob.fileopen(a_bfile);
dbms_lob.loadfromfile(a_blob, a_bfile, dbms_lob.getlength(a_bfile));
dbms_lob.fileclose(a_bfile);
commit;
end;
/

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧