环境搭建
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.1.0.7.0</version> <scope>test</scope></dependency>
数据库
create table WORD_FILE( gyo_num NUMBER(6), word_kb CHAR(6), word_filename VARCHAR2(80), biko VARCHAR2(80), WORD_FILE BLOB, usr_id VARCHAR2(20), ymdt DATE)
实现代码
需要通过创建blob来创建blobBLOB.createTemporary
创建方法,然后通过setbyte设置Blob中的数据
package com.yellowcong.utils;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,2017114test.doc"准备","VENAS',TO_DATE('17-11-14','RR-MM-DD'),?)"); String inFile = "C:/Users/zhangrw/Desktop/test/2nanQz3wsfn8rkrtza5p8xQy8prbyHwhy.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,2017114test.doc"准备","VENAS',TO_DATE('17-11-14','RR-MM-DD'),?)"); String inFile = "C:/Users/zhangrw/Desktop/test/2nanQz3wsfn8rkrtza5p8xQy8prbyHwhy.jpg"; byte [] result = file2Byte(inFile); ///设置自己的代码文件 ps.setBytes(1, result); ps.executeUpdate(); } catch (SQLException e) { // TODO 自动生成された catch ブロック 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); //set start is 1 ///这个setByteses 指定起点,然后设置字节 blob.setBytes(1, result); //pub byte 这种方法是添加byte/// blob.putBytes(1, result); return blob; } catch (Exception e) { // TODO 自动生成された catch ブロック 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) { // TODO 自动生成された catch ブロック 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 { //通过getbinaryStreame 获取输入流的方法 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) { // TODO 自动生成された catch ブロック e.printStackTrace(); } try { if (out != null) { out.close(); } } catch (IOException e) { // TODO 自动生成された catch ブロック e.printStackTrace(); } } } static { try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); } catch (Exception e) { // TODO 自动生成された catch ブロック 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) { // TODO 自动生成された catch ブロック e.printStackTrace(); } return null; }}
上传结果
这是测试图片。如果是word文档,那将是二进制的乱码