Package 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;
Import Oracle.sql.BLOB;
public class Blobutil {
private static Blobutil bu;
Private String env;
public static Blobutil getinstance (String env) {
Bu = new Blobutil ();
Bu.env = env;
return bu;
}
/**
* <p> Get database links </p>
* @return
* @throws Exception
*/
Private Connection getconnection () throws Exception {
String Driver = "Oracle.jdbc.driver.OracleDriver";
Class.forName (driver);
String env = this.env;
Properties Pro = new properties ();
Read the configuration file in the classes directory
Pro.load (New FileReader (Class.class.getResource ("/config.properties"). GetFile ());
String host = Pro.getproperty (env +. Host);
String database = Pro.getproperty (env +. database);
String username = pro.getproperty (env + ". Username");
String Password = pro.getproperty (env + ". Password");
String port = pro.getproperty (env +. Port);
String url = "jdbc:oracle:thin:@" + Host + ":" + Port + ":" + database;
Return drivermanager.getconnection (URL, username, password);
}
/**
* <p> incoming project, task, attachment name, file path, write database </p>
* @param ProjectID
* @param taskId
* @param fileName
* @param file
* Whether the @return returned successfully
*/
public Boolean write (string ProjectID, String taskId, String fileName, string file) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement PS = null;
Blob blob = null;
Boolean flag = false;
try {
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, fileName);
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, fileName);
rs = Ps.executequery ();
if (Rs.next ()) {
Blob = (BLOB) Rs.getblob (1);
}
InputStream in = new FileInputStream (file);
OutputStream out = Blob.setbinarystream (1L);
byte[] buffer = new byte[1024];
int length =-1;
while (length = in.read (buffer))!=-1) {
Out.write (buffer, 0, length);
}
In.close ();
Out.close ();
Conn.commit ();
Conn.setautocommit (TRUE);
Flag = true;
}
catch (Exception e) {
IF (conn!= null) {
try {
Conn.rollback ();
catch (SQLException E1) {
E1.printstacktrace ();
}
}
}
finally {
try {
Rs.close ();
Ps.close ();
Conn.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
return flag;
}
/**
* <p> Read the database BLOB field file according to the project ID, task ID, filename, write the specified file path </p>
* @param ProjectID
* @param taskId
* @param fileName
* @param file
* Whether the @return returned successfully
*/
public boolean read (string ProjectID, String taskId, String fileName, string file) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement PS = null;
Blob blob = null;
Boolean flag = false;
try {
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, fileName);
rs = Ps.executequery ();
if (Rs.next ()) {
Blob = (BLOB) Rs.getblob (1);
}
InputStream in = Blob.getbinarystream ();
byte[] buf = new 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 = true;
}
catch (Exception e) {
E.printstacktrace ();
}
finally {
try {
Rs.close ();
Ps.close ();
Conn.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
return flag;
}
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"));
}
}