iWebOffice2009问题
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();
??????? }
??? }
}
?
總結
以上是生活随笔為你收集整理的iWebOffice2009问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iWebOffice使用VBA控制字体
- 下一篇: MySQL 8.0.28 忘记密码,重置