首先从/网站上下载最新的pdfbox.jar包和fontbox.jar 两个jar包,将两个jar包放到你的classpath下面,读取代码如下:
String filePath = "E:\\tes.pdf",nr="";
PDDocument doc=PDDocument.load(filePath);
PDFTextStripper stripper=new PDFTextStripper();
nr = Text(doc);
nr = nr.replace(" ", "");
doc.close();
System.out.println("pdf内容:"+nr);
import oracle.jdbc.driver.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Clob;
public class TestOpenDoc {
public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的
public OraclePreparedStatement opst = null; //**PreparedStatement用
public Connection conn = null;
public Statement stmt = null;
public TestOpenDoc() {
}
public boolean getConnect() {
/
/这是我的数据库所在
String serverName = "prosrv";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";
conn = Connection(url, "appuser", "appuser");
}
catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
public static void main(String[] args) {
TestOpenDoc test = new TestOpenDoc();
if (!Connect()) {
System.out.println("数据库连结错误");
return ;
}
try{
byte a[] = null; //**将测试文件test.doc读入此字节数组
java.io.FileInputStream fin = null;
java.io.FileOutputStream fout = null;
//Oracle提供的
try {
java.io.File f1 = new java.io.File("c:/test.doc");
java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写
//入该文 件,和源文件对比测试用
fin = new java.io.FileInputStream(f1);
fout = new java.io.FileOutputStream(f2);
int flength = (int) f1.length(); //**读入文件的字节长度
System.out.println("file length::" + flength);
a = new byte[flength];
int i = 0;
int itotal = 0;
//* 将文件读入字节数组
for (; itotal < flength; itotal = i + itotal) {
i = ad(a, itotal, flength - itotal);
}
fin.close();
System.out.println("read itotal::" + itotal);
//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql =
"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
OraclePreparedStatement opst = (OraclePreparedStatement)
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
opst.setInt(2, flength);
opst.clearParameters();
// /**插入其它数据后,定位BLOB字段
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement) prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
OracleResultSet ors = (OracleResultSet) uteQuery();
if (()) {
oracle.sql.BLOB blob = BLOB(1); //**得到BLOB字段
int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段
System.out.println("j:" + j);
ors.close();
Clob clob;
clob = Clob("");
String str;
str = String();
str = SubString(0L,(int)clob.length());
System.out.println(str);
}
System.out.println("insert into ok");
byte b[] = null; //**保存从BLOB读出的字节
opst.clearParameters();
mysql = "select filebody from filelist where filename=?";
opst = (OraclePreparedStatement)
prepareStatement(mysql);
opst.setString(1, "wordtemplate2");
ors = (OracleResultSet) uteQuery();
if (()) {
oracle.sql.BLOB blob2 = BLOB(1);
System.out.println("blob2 length:" + blob2.length());
b = Bytes(1, flength); //**从BLOB取出字节流数据
System.out.println("b length::" + b.length);
}
ors.close();
// 将从BLOB读出的字节写入文件
fout.write(b, 0, b.length);
fout.close(); java replace方法
System.out.println("write itotal::" + b.length);
}
catch (Exception e) {
System.out.println("errror :" + e.toString());
e.printStackTrace();
}
finally { //**关闭所有数据联接
}
}
catch(Exception e){
System.out.println(e);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论