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






猜你喜欢

转载自www.cnblogs.com/YuyuanNo1/p/10309097.html