复制代码代码如下:
Paket com.wanmei.meishu;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
importiere oracle.sql.BLOB;
öffentliche Klasse BlobUtil {
privates statisches BlobUtil bu;
private String env;
öffentliches statisches BlobUtil getInstance(String env) {
bu = new BlobUtil();
bu.env = env;
zurück bu;
}
/**
* <p>得到数据库链接</p>
* @zurückkehren
* @throws-Ausnahme
*/
private Verbindung getConnection() löst Ausnahme aus {
String drivers = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
String env = this.env;
Eigenschaften pro = neue Eigenschaften();
// 读取classes 目录下的配置文件
pro.load(new FileReader(Class.class.getResource("/config.properties").getFile()));
String host = pro.getProperty(env + „.host“);
String-Datenbank = pro.getProperty(env + „.database“);
String username = pro.getProperty(env + „.username“);
String-Passwort = pro.getProperty(env + „.password“);
String port = pro.getProperty(env + „.port“);
String url = "jdbc:oracle:thin:@" + host + :: + port +:: + Datenbank;
return DriverManager.getConnection(URL, Benutzername, Passwort);
}
/**
* <p>传入项目,任务,附件名称,文件路径,写入数据库</p>
* @param Projekt-ID
* @param taskId
* @param Dateiname
* @param-Datei
* @return 返回是否成功
*/
public boolean write(String projectId, String taskId, String fileName, String file) {
Verbindungsverbindung = null;
ResultSet rs = null;
PreparedStatement ps = null;
BLOB blob = null;
boolesches Flag = false;
versuchen {
conn = getConnection();
conn.setAutoCommit(false);
String sql = "INSERT INTO PS_ZP_PRJ_WBS_BLOB BLB(ZP_PRJ_ID, ZZ_SEQ_NUM, ZZ_FILE_NAME, ZZ_IMAGE_BLOB) VALUES(?, ? ,? ,empty_blob())";
ps = conn.prepareStatement(sql);
ps.setString(1, projectId);
ps.setString(2, taskId);
ps.setString(3, Dateiname);
ps.executeUpdate();
sql = "SELECT ZZ_IMAGE_BLOB FROM PS_ZP_PRJ_WBS_BLOB WHERE ZP_PRJ_ID = ? AND ZZ_SEQ_NUM = ? AND ZZ_FILE_NAME = ? FOR UPDATE";
ps = conn.prepareStatement(sql);
ps.setString(1, projectId);
ps.setString(2, taskId);
ps.setString(3, Dateiname);
rs = ps.executeQuery();
if(rs.next()) {
blob = (BLOB) rs.getBlob(1);
}
InputStream in = new FileInputStream(file);
OutputStream out = blob.setBinaryStream(1L);
byte[] buffer = neues byte[1024];
int Länge = -1;
while ((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
}
in.close();
out.close();
conn.commit();
conn.setAutoCommit(true);
Flag = wahr;
}
Catch(Ausnahme e) {
if(conn != null) {
versuchen {
conn.rollback();
} Catch (SQLException e1) {
e1.printStackTrace();
}
}
}
Endlich {
versuchen {
rs.close();
ps.close();
conn.close();
}
Catch(Ausnahme e) {
e.printStackTrace();
}
}
Rückkehrflag;
}
/**
* <p>根据项目Id, 任务Id, 文件名读取数据库blob字段文件, 写入指定的文件路径</p>
* @param Projekt-ID
* @param taskId
* @param Dateiname
* @param-Datei
* @return 返回是否成功
*/
public boolean read(String projectId, String taskId, String fileName, String file) {
Verbindungsverbindung = null;
ResultSet rs = null;
PreparedStatement ps = null;
BLOB blob = null;
boolesches Flag = false;
versuchen {
conn = getConnection();
String sql = "SELECT ZZ_IMAGE_BLOB FROM PS_ZP_PRJ_WBS_BLOB WHERE ZP_PRJ_ID = ? AND ZZ_SEQ_NUM = ? AND ZZ_FILE_NAME = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, projectId);
ps.setString(2, taskId);
ps.setString(3, Dateiname);
rs = ps.executeQuery();
if(rs.next()) {
blob = (BLOB) rs.getBlob(1);
}
InputStream in = blob.getBinaryStream();
byte[] buf = neues Byte[1024];
int bytesIn = 0;
FileOutputStream out = new FileOutputStream(file);
while ((bytesIn = in.read(buf, 0, 1024)) != -1) {
out.write(buf, 0, bytesIn);
}
in.close();
out.close();
Flag = wahr;
}
Catch(Ausnahme e) {
e.printStackTrace();
}
Endlich {
versuchen {
rs.close();
ps.close();
conn.close();
}
Catch(Ausnahme e) {
e.printStackTrace();
}
}
Rückkehrflag;
}
public static void main(String[] args) {
BlobUtil bu = BlobUtil.getInstance("MSDEV");
System.out.println(bu.write("CB", "001", "image1", "D://61e44b02jw1dw4xbp2zo6j.jpg"));
System.out.println(bu.read("CB", "001", "image1", "D://2.jpg"));
}
}