oracle怎么取二进制流存储的word文本到本地?用java可以可以取到吗?
5条回答 默认 最新
- WorldMobile 2015-04-20 14:22关注
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本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记