场景
- 我的 Oracle 数据库服务器上有一个根目录 /rootFolder/myDir/
- 我目前在 /myDir/ 下有 20 个子文件夹,还可以扩展到更多
- 在这 20 个子文件夹中,PDF、DOC、xlsx 等类型的文档数量不断变化。
- 我需要能够将 /myDir/ 到最后一个文件夹的所有文档作为 blob 对象动态加载到表中。
到目前为止我能做的事情。
我创建了一个 Java 存储过程,它将遍历给定的目录结构并写入 FileName
、FilePath
、len
、日期
到表WEB_DIRLIST
DROP JAVA SOURCE BALT_CHECK."WebDocs";
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED BALT_CHECK."WebDocs" as import java.io.*;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.lang.String;
public class WebDocs
{
public static long fileID;
public static void GetDocs(String rootdirectory) throws SQLException
{
walkin(rootdirectory);
}
public static void walkin(String rootdirectory) throws SQLException
{
File path = new File( rootdirectory );
String[] DirList = path.list();
for( int x = 0; x < DirList.length; x++)
{
String newPath = rootdirectory + DirList[x];
if (newPath != null) {
File f = new File(newPath);
if (f.isDirectory()) {
String dirName = DirList[x];
GetDocs(newPath +"/");
}
if (f.isFile()){
WriteFile(f);
}else{
}
}
}
}
public static void WriteFile(File file) throws SQLException
{
String fileName;
String filePath;
String elementID;
long len;
Date date;
String sqlDate;
SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
fileID = fileID + 1;
elementID = String.valueOf(fileID);
fileName = file.getName();
filePath = file.getPath();
len = file.length();
date = new Date(file.lastModified());
sqlDate = df.format(date);
#sql { INSERT INTO WEB_DIRLIST (ID, FILE_NAME, FILE_SIZE, CREATE_DATE, FILE_PATH)
VALUES (:elementID, :fileName, :len, to_date(:sqlDate, 'YYYY-MM-DD HH24:MI:SS'), :filePath) };
}
}
/
这工作得很好,我的目的是让 pl/sql 存储过程使用 WEB_DIRLIST 中的值作为过程的输入,该过程获取路径和文件名并上传到表 WEB_STATICDOCS
这是我的代码。
CREATE OR REPLACE PROCEDURE BALT_CHECK.insert_doc as
f_lob BFILE;
b_lob BLOB;
BEGIN
INSERT INTO WEB_DOCS VALUES('2',EMPTY_BLOB(),'PDF','Acctg001-travelexpensereport.pdf')
RETURN blob_col INTO b_lob;
f_lob := BFILENAME('ACCTG','Acctg001-travelexpensereport.pdf');
dbms_lob.fileopen(f_lob, dbms_lob.file_readonly);
dbms_lob.loadfromfile( b_lob, f_lob, dbms_lob.getlength(f_lob));
dbms_lob.fileclose(f_lob);
COMMIT;
END;
/
我已经硬编码了 BFILENAME 输入参数,但我希望它是动态的。
当我对名称进行硬编码并在 oracle 中为我从中上传的文件夹创建目录条目时,此代码可以正常工作。 (在本例中,目录名称称为“ACCTG”。从我所能找到的情况来看,我必须在 oracle 中为我想要读取的每个目录创建一个目录条目......这是唯一的方法吗?如果我不知道目录怎么办?如果子目录下有子目录怎么办?
如果可能的话,我更愿意通过一个语句来完成此操作。这可以通过 Java 存储过程来完成吗?如果是这样怎么办?
编辑
基于@justin-cave 此处提供的示例 http://www.idevelopment.info/data/Programming/java/jdbc/LOBS/BLOBFileExample.java
他的程序怎么知道要上传的文件在服务器上的路径呢?他传递了这个名字,但是
/**
* Method used to write binary data contained in a file to an Oracle BLOB
* column. The method used to write the data to the BLOB uses the putBytes()
* method. This is one of two types of methods used to write binary data to
* a BLOB column. The other method uses Streams.
*
* @throws java.io.IOException
* @throws java.sql.SQLException
*/
public void writeBLOBPut()
throws IOException, SQLException {
FileInputStream inputFileInputStream = null;
String sqlText = null;
Statement stmt = null;
ResultSet rset = null;
BLOB image = null;
int chunkSize;
byte[] binaryBuffer;
long position;
int bytesRead = 0;
int bytesWritten = 0;
int totbytesRead = 0;
int totbytesWritten = 0;
try {
stmt = conn.createStatement();
inputBinaryFile = new File(inputBinaryFileName);
inputFileInputStream = new FileInputStream(inputBinaryFile);
sqlText =
"INSERT INTO test_blob (id, image_name, image, timestamp) " +
" VALUES(1, '" + inputBinaryFile.getName() + "', EMPTY_BLOB(), SYSDATE)";
stmt.executeUpdate(sqlText);
sqlText =
"SELECT image " +
"FROM test_blob " +
"WHERE id = 1 " +
"FOR UPDATE";
rset = stmt.executeQuery(sqlText);
rset.next();
image = ((OracleResultSet) rset).getBLOB("image");
chunkSize = image.getChunkSize();
binaryBuffer = new byte[chunkSize];
position = 1;
while ((bytesRead = inputFileInputStream.read(binaryBuffer)) != -1) {
bytesWritten = image.putBytes(position, binaryBuffer, bytesRead);
position += bytesRead;
totbytesRead += bytesRead;
totbytesWritten += bytesWritten;
}
inputFileInputStream.close();
conn.commit();
rset.close();
stmt.close();
System.out.println(
"==========================================================\n" +
" PUT METHOD\n" +
"==========================================================\n" +
"Wrote file " + inputBinaryFile.getName() + " to BLOB column.\n" +
totbytesRead + " bytes read.\n" +
totbytesWritten + " bytes written.\n"
);
} catch (IOException e) {
System.out.println("Caught I/O Exception: (Write BLOB value - Put Method).");
e.printStackTrace();
throw e;
} catch (SQLException e) {
System.out.println("Caught SQL Exception: (Write BLOB value - Put Method).");
System.out.println("SQL:\n" + sqlText);
e.printStackTrace();
throw e;
}
}
最佳答案
如果您想通过 PL/SQL 加载文件,则需要为要从中读取数据的每个目录和子目录运行 CREATE DIRECTORY
语句。这无疑是可能的,但相当笨重。
由于您已经有一个正在遍历目录结构的 Java 存储过程,因此从 Java 而不是从 PL/SQL 将数据加载到 BLOB
列中似乎更合乎逻辑。网络上有很多将数据从文件写入 BLOB
的示例。我将从 writeBLOBPut method 开始在此示例中,您也可以从 Using Large Objects 开始Java 教程中的页面。
关于java - 加载文件到oracle数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22920434/