BOLB转word文件,和word文件转换BOLB

Posted computervip

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BOLB转word文件,和word文件转换BOLB相关的知识,希望对你有一定的参考价值。

 

1.BOLB转word文件

import java.io.*;
import java.sql.*;
 
public class Test {
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
 
    private ResultSet getResultSet() {
 
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String url = "jdbc:oracle:thin:@10.23.117.110:1521:zgzhms";
            String user = "ibms";
            String password = "ibms";
            con = DriverManager.getConnection(url, user, password);
            stmt = con.createStatement();
            String sql = "SELECT  t.TXN_TRADE FROM  T_TXN_TRADE t    WHERE  t.txn_trade_id = 1";
            rs = stmt.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
 
    public void InputDoc() {
        Test temp = new Test();
        ResultSet rset = temp.getResultSet();
        try {
            while (rset.next()) {
                oracle.sql.BLOB blob = (oracle.sql.BLOB) rset.getBlob("TXN_TRADE");
                File f = new File("C:\temp.doc");
                FileOutputStream fos = new FileOutputStream(f);
                InputStream is = blob.getBinaryStream();// 读出数据后转换为二进制流
                byte[] data = new byte[1024];
                while (is.read(data) != -1) {
                    fos.write(data);
                }
                fos.close();
                is.close();
            }
            con.commit(); // 正式提交
            rset.close();
        } catch (Exception e) {
        }
    }
 
    public static void main(String[] args) {
        Test temp = new Test();
        temp.InputDoc();
    }

2.word文件转换BOLB

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.BLOB;

public class DbBlobTest {

    private static
    final String DB_URL = "jdbc:oracle:thin:@10.0.7.170:1522:ORCL5";

    private static
    final String DB_USER = "test";

    private static final String
            DB_PASSWORD = "test";

    private static Connection conn =
            null;

    public static void main(String[] args) throws
            Exception {
        // insert into blob

        Connection conn = getConnection();


        PreparedStatement ps = conn

                .prepareStatement("INSERT INTO WORD_FILE  (GYO_NUM, WORD_KB, WORD_FILENAME, BIKO, USR_ID, YMDT, WORD_FILE) values(6, ‘KYK002‘, ‘20171114test.doc‘, ‘備考‘, ‘VENAS‘, TO_DATE(‘17-11-14‘, ‘RR-MM-DD‘), ?)");

        String inFile =
                "C:/Users/zhangrw/Desktop/test/2nANQz3wsFN8rkrTZa5P8xQY8PRBhyHw.jpg ";

        //设定的是blob类型
        ps.setBlob
                (1, file2Blob(inFile));
        ps.executeUpdate();
    }


    /**
     * test blob data 2 file
     *
     * @throws Exception
     */
    public static void testBlob2File() throws Exception {

        Connection conn = getConnection();


        PreparedStatement ps = conn
                .prepareStatement
                        ("select * from WORD_FILE a ");

        ResultSet rs =
                ps.executeQuery();
        int index = 1;
        while
                (rs.next()) {
            Blob bl = rs.getBlob("WORD_FILE");

            String outFile =
                    "C:/Users/zhangrw/Desktop/test/dou_" + (index++) + ".doc";


            blob2File(bl, outFile);
        }
    }

    /**
     * upload
     * file
     * 通过二进制的方式来上传文件
     */
    public static void testFile2byte() {
        try {
            // insert into blob
            Connection conn = getConnection();


            PreparedStatement ps = conn

                    .prepareStatement("INSERT INTO WORD_FILE(GYO_NUM, WORD_KB, WORD_FILENAME, BIKO, USR_ID, YMDT, WORD_FILE) values(4, ‘KYK002‘, ‘20171114test.doc‘, ‘備考‘, ‘VENAS‘, TO_DATE(‘17-11-14‘, ‘RR-MM-DD‘), ?)");

            String inFile = "C:/Users/zhangrw/Desktop/test/2nANQz3wsFN8rkrTZa5P8xQY8PRBhyHw.jpg ";

            byte[] result = file2Byte(inFile);

            //设定的是自己码文件
            ps.setBytes(1, result);

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 将文件转化为
     * Blob的对象
     * create file 2 blob
     *
     * @param inFile
     * @return
     */
    public static Blob file2Blob(String inFile) {

        try {

            byte[] result = file2Byte(inFile);
            //creat a new blob
//            BLOB blob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);

            BLOB blob = BLOB.empty_lob();
            //set start is 1
            //这个setBytes 是指定起点,然后设定字节
            blob.setBytes(1, result);

            //pub byte 这个方法是添加byte
//          blob.putBytes(1, result);
            return blob;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将文件转化为二进制的数据
     * create file 2 byte
     *
     * @param inFile
     * @return
     */
    public static byte[]
    file2Byte(String inFile) {
        InputStream in = null;

        try {
            in = new FileInputStream(new File(inFile));


            int len = in.available();
            byte[]
                    result = new byte[len];
            in.read(result);


            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 将Blob文件转化为Blob对象
     * convert blob to file
     *
     * @param blob
     * @param outFile
     */
    public static void blob2File(Blob blob, String outFile) {

        InputStream in = null;
        OutputStream out = null;

        try {
            //通过getBinaryStream 方法获取输入流

            in = blob.getBinaryStream();
            out = new
                    FileOutputStream(new File(outFile));
            byte[] buff
                    = new byte[1024];
            int len = 0;
            while
                    ((len = in.read(buff)) > 0) {
                out.write(buff,
                        0, len);
            }
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            try {

                if (in != null) {
                    in.close();

                }
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {

                if (out != null) {
                    out.close();

                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }

    static {

        try {
            Class.forName
                    ("oracle.jdbc.driver.OracleDriver");

            conn =
                    DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnection() {

        try {

            if (conn == null) {
                Class.forName
                        ("oracle.jdbc.driver.OracleDriver");

                conn =
                        DriverManager
                                .getConnection(DB_URL,
                                        DB_USER, DB_PASSWORD);
            }
            return conn;

        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }
}

 

以上是关于BOLB转word文件,和word文件转换BOLB的主要内容,如果未能解决你的问题,请参考以下文章

mybaties获取mysql存储的bolb字段 和展示

mysql bolb对应java啥类型

JDBC-Bolb数据类型存储和查询(练习)

Bolb存储照片

docx文件转word文档

怎样把word和excel转pdf