• java如何把文件转化成oracle的blob



    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;
    }
    }






  • 相关阅读:
    实践详细篇-Windows下使用Caffe训练自己的Caffemodel数据集并进行图像分类
    实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集
    实践详细篇-Windows下使用VS2015编译安装Caffe环境(CPU ONLY)
    Linux上rpm实战搭建FTP服务器
    Idea打包Maven项目
    修改Idea默认的全局设置,如Maven等
    IntelliJ IDEA 项目结构旁边出现 0%classes,0% lines covered
    Idea中JDK为1.8,还提示Diamond types are not supported at this language level
    如何避免在父类转换到子类时发生java.lang.ClassCastException
    HashMap常用方法总结
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/10309097.html
Copyright © 2020-2023  润新知