java word 添加内容吗_在Java中如何实现word的插入功能

该博客介绍了一种在Java中处理Word文档的方法,包括如何将Word文档插入到Oracle数据库中以及如何从数据库中读取Word文档。通过使用`OracleBlob`类,实现了对文件的读写,涉及了数据库连接、PreparedStatement、Blob对象的使用。
摘要由CSDN通过智能技术生成

展开全部

你所谓的插入word是将整个word文档插入数据库还是??

还是先生成word文档 再插e68a843231313335323631343130323136353331333262373338入数据

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.Blob;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import oracle.sql.BLOB;

public class OracleBlob {

private DBstep.iDBManager2000 DbaObj ;

private File file = null;

public OracleBlob() {

DbaObj=new DBstep.iDBManager2000(); //创建数据库对象

}

public void insert(String infile) throws Exception {

FileInputStream fis = null;

int iFileId = -1;

PreparedStatement pstmt = null;

ResultSet rs = null;

try {

//生成随机码

java.util.Date dt=new java.util.Date();

long lg=dt.getTime();

Long ld=new Long(lg);

String mRecordID=ld.toString();

file = new File(infile);

fis = new FileInputStream(file);

//得到数据库

iFileId = DbaObj.GetMaxID("INFO_DOCUMENT_FILE","FileId");

if (DbaObj.OpenConnection()) {

DbaObj.Conn.setAutoCommit(false);

String sql="insert into INFO_DOCUMENT_FILE(FileID,RecordID,FileName,FileType,FileSize,FileDate,FileBody,FilePath,UserName,Descript) values (?,?,?,?,?,?,empty_blob(),?,?,? )";

pstmt = DbaObj.Conn.prepareStatement(sql);

pstmt.setInt(1, iFileId);

pstmt.setString(2, mRecordID);

pstmt.setString(3, mRecordID+".doc");

pstmt.setString(4, "doc");

pstmt.setLong(5,file.length());

pstmt.setDate(6, DbaObj.GetDate());

pstmt.setString(7, "");

pstmt.setString(8, "");

pstmt.setString(9, "测试通用版本=="); // "通用版本"

pstmt.executeUpdate();

pstmt = DbaObj.Conn.prepareStatement("select FileBody,FileSize from INFO_DOCUMENT_FILE where RecordID='"+mRecordID+"' for update");

rs = pstmt.executeQuery();

while (rs.next()) {

oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("FileBody");

OutputStream os = blob.getBinaryOutputStream();

BufferedInputStream bis = new BufferedInputStream(fis);

byte[] buf = new byte[rs.getInt("FileSize")];

int len = 0;

if ((len = bis.read(buf)) != -1) {

os.write(buf, 0, len);

os.flush();

}

os.close();

bis.close();

}

DbaObj.Conn.commit();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

fis.close();

DbaObj.CloseConnection();

}

}

public void readDoc(String outfile,String id)throws Exception{

FileOutputStream fos = null;

InputStream is = null;

String Sql = "SELECT FileBody,FileSize FROM info_document_file WHERE RecordID='" + id + "'";

try {

if (DbaObj.OpenConnection()) {

try {

ResultSet result = DbaObj.ExecuteQuery(Sql);

if (result.next()) {

try {

int fileSize=result.getInt("FileSize");

Blob blob=result.getBlob("FileBody");

file = new File(outfile);

if(!file.exists())

{

file.createNewFile();//如果文件不存在,则创建

}

fos = new FileOutputStream(file);

is = blob.getBinaryStream();// 读出数据后转换为二进制流

byte[] data = new byte[fileSize];

int size = 0;

while((size = is.read(data)) != -1)

{

fos.write(data,0,size);

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

result.close();

}

catch (SQLException e)

{

e.printStackTrace();

}

}

}

finally

{

DbaObj.CloseConnection();

}

}

public static void main(String args[])

{

OracleBlob blob=new OracleBlob();

try {

blob.readDoc("c://test.doc", "1281088075378");

//blob.insert("c://信息处WebService接口文档.doc");

} catch (Exception e) {

e.printStackTrace();

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值