oracle怎么取二进制流存储的word文本到本地?用java可以可以取到吗?
java提取word文档存入oracle (2011-04-22 12:10:37)转载▼
今天发现java工程报错了,但工程中的文件没有提示错误,找了下发现是工程下的.classpath文件配置有错,有个lib库被删除了,但在这个文件中,还配置这它的路径。
//java调用电脑程序打开word文档,
Runtime.getRuntime().exec("C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE aa.doc");
//提取测试程序
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class StreamDemo_17 {
public static void main(String[] args) {
StreamDemo_17 s = new StreamDemo_17();
String path = "E:\a\test.doc";
try {
s.readDocFile(path);
} catch (IOException e) {
System.out.println("读取失败:" + path + "文件不存在" + e.getMessage());
}
}
public void readDocFile(String origFileName) throws IOException {
System.out.println("C:\fileText.doc中的内容如下:\n");
// 创建文件输入流
FileInputStream in = new FileInputStream(new File(origFileName));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor(in);
// 对DOC文件进行提取
text = extractor.getText();
System.out.println(text);
}
}
//将内容转换为BLOB类型存入数据库
public void docInsertIntoDatabae(String fileName,String content,Long lastmodify){
try{
conn.setAutoCommit(false);
BLOB blob = null;
blob=(BLOB) conn.createBlob();
OutputStream out1 = blob.getBinaryOutputStream();
byte[]co=content.getBytes();
out1.write(co);out1.close();
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd H:m:s");
String dateFormat=sd.format(lastmodify);
System.out.print("dateFormat="+dateFormat);
UUID uuid = UUID.randomUUID();
id=uuid.toString();
System.out.println("id="+id);
PreparedStatement pstmt = conn.prepareStatement("insert into word_search(id,tilte,content,original,lastmodify) values('"+id+"','"+fileName+"',?,empty_blob(),to_date('"+dateFormat+"','yyyy-mm-dd hh24:mi:ss'))");
// PreparedStatement pstmt = conn.prepareStatement("insert into word_search(id,content) values('dd',?)");
//
pstmt.setBlob(1, blob);///////////////////////
pstmt.executeUpdate(); System.out.println("kkkkkkkkkkkkkkkkkkkk");
pstmt.close();
pstmt = conn.prepareStatement("select original from word_search where id= ? for update");
pstmt.setString(1,id);
ResultSet rset = pstmt.executeQuery();
if (rset.next()) blob = (BLOB) rset.getBlob(1);
pstmt.close();
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
pstmt = conn.prepareStatement("update word_search set original=? where id=?");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[(int)fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1,blob);
pstmt.setString(2,id);
pstmt.executeUpdate();
pstmt.close();
conn.commit();
} catch(SQLException sqle) {
System.err.println(sqle.getMessage());
sqle.printStackTrace();
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
}
//从数据库中读取BLOB文档成word文档
public void OpenDoc(String id){
Connection con = ConnectionUtil.getConnecttion();
ResultSet rs;
Statement stmt;
InputStream is;
FileOutputStream fos;
try {
con.setAutoCommit(false);
stmt = con.createStatement();
String sql = "select * from word_search ";
rs = stmt.executeQuery(sql);
while (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("CONTENT");
// oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("CONTENT");
File f = new File("E:\1.doc");
fos = new FileOutputStream(f);
is = blob.getBinaryStream();
byte[] data = new byte[1024];
while (is.read(data) != -1) {
fos.write(data);
}
}
con.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}fin
http://blog.sina.com.cn/s/blog_4165232d0102e51g.html
http://wlh269.iteye.com/blog/493739
参考
http://blog.sina.com.cn/s/blog_7da9c8da0100q6wr.html
Java Oracle Blob 导入导出Word
http://hpfgc.blog.163.com/blog/static/1479955720107189106190/