iWebOffice2009保存的数据是保存整个word文件 而不仅仅是内容 所以用二进制流导出的时候肯定会出现乱码 原先不知道 做了测试 用的mysql 没有用到oracle 因为公司昨天下午断网 所以Oracle数据库连接不上 所以只能本机测试mysql数据库
import java.io.*;
import java.util.*;
import java.sql.*;
public class BlobWord
{
private static final String URL = "jdbc:mysql://127.0.0.1:3306/dbdemo?user=root&password=root&useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;
public BlobWord()
{
}
/**
* 向数据库中插入一个新的BLOB对象(图片)
*
* @param infile - 要输入的数据文件
* @throws java.lang.Exception
*
*/
public void blobInsert(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //这种方法原理上会丢数据,因为file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容
pstmt.executeUpdate();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
//关闭所打开的对像//
pstmt.close();
fis.close();
conn.close();
}
}
/**
* 从数据库中读出BLOB对象
*
* @param outfile - 输出的数据文件
* @param picID - 要取的图片在数据库中的ID
* @throws java.lang.Exception
*
*/
public void blobRead(String outfile,int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select pic from tmp where id=?");
pstmt.setInt(1,picID); //传入要取的图片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //从数据库中一段一段的读出数据
//System.out.println(size);
if(size != -1) //-1表示读到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
/**
* 写入文本
* @param infile
* @throws Exception
*/
public void writeText(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp_text(descs,t_pic) values(?,?)");
pstmt.setString(1,file.getName()); //把传过来的第一个参数设为文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //这种方法原理上会丢数据,因为file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容
pstmt.executeUpdate();
}catch(Exception ex)
{
ex.printStackTrace();
}finally
{
//关闭所打开的对像//
pstmt.close();
fis.close();
conn.close();
}
}
/**
* 读文本
* @param outfile
* @param id
* @throws Exception
*/
public void readText(String outfile,int id) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select t_pic from tmp_text where id=?");
pstmt.setInt(1,id); //传入要取的图片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("t_pic");
int size = 0;
// System.out.println("文本信息:"+getBlogContext(rs.getBlob("t_pic")));
System.out.println("文本信息:"+new String(rs.getBytes("t_pic"),"gbk"));
while((size = is.read(Buffer)) != -1)
{
fos.write(Buffer,0,size);
}
System.out.println("文件导出成功");
}catch(Exception e)
{
e.printStackTrace();
System.out.println("文件导出失败");
}
finally
{
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
private String getBlogContext(Blob blob )
{
String result="";
try
{
if(blob!=null){
StringBuffer buffer = new StringBuffer();
InputStream is = null;
is = blob.getBinaryStream();
InputStreamReader isr = new InputStreamReader(is);
if (isr.ready()) {
Reader reader = new BufferedReader(isr);
int ch;
while ((ch = reader.read()) > -1) {
buffer.append((char) ch);
}
}
isr.close();
is.close();
result=buffer.toString();
}
}catch (Exception e) {
e.printStackTrace();
return result;
}
return result;
}
//以下为WebOffice2009测试模块
/**
* 生成word文件
*/
public void readDoc(String outfile,String id)throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select FileBody,FileSize from document_file where RecordID=?");
pstmt.setString(1,id);
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if(!file.exists())
{
file.createNewFile();//如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("FileBody");// 读出数据后转换为二进制流
byte[] data = new byte[rs.getInt("FileSize")];
int size = 0;
while((size = is.read(data)) != -1)
{
fos.write(data,0,size);
}
System.out.println("文件导入成功");
}catch(Exception e)
{
e.printStackTrace();
System.out.println("文件导入失败");
}
finally
{
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}
/**
* 写入word文档
* @param infile
* @throws Exception
*/
public void writeDoc(String infile)throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(URL);
//生成随机码
java.util.Date dt=new java.util.Date();
long lg=dt.getTime();
Long ld=new Long(lg);
java.text.SimpleDateFormat d=new java.text.SimpleDateFormat("yyyy-MM-dd");
file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into document_file(RecordID,FileName,FileType,FileSize,FileBody) values(?,?,?,?,?)");
pstmt.setString(1, ld.toString());
pstmt.setString(2,file.getName()); //把传过来的第一个参数设为文件名
pstmt.setString(3,".doc");
pstmt.setInt(4,4999);
pstmt.setBinaryStream(5,fis,fis.available()); //第二个参数为文件的内容
pstmt.executeUpdate();
System.out.println("文件插入成功");
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("文件插入失败");
}
finally
{
//关闭所打开的对像//
pstmt.close();
fis.close();
conn.close();
}
}
public static void main(String[] args)
{
try
{
BlobWord blob = new BlobWord();
//blob.blobInsert("C:\\jimi_dingbu-shang.jpg");
//blob.blobRead("c://a-shang.jpg",1);
//blob.writeText("c:\\轻工平台设计文档整合版本.doc");
//blob.readText("c://readText.doc", 2);
blob.readDoc("c://readText.doc", "1281060416546");
// blob.writeDoc("c:\\周报.doc");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}