向oracle数据库存储文件,并读出文件
/**
* 从数据库中查询IRI和KLO模型的数据,并下载到本地
* @param dataTime 时次
* @param outIRIFilePath IRI文件下载到本地的路径
* @param outKLOFilePath KLO文件下载到本地的路径
* @param outAnaFilePath 插值文件下载到本地的路径
*/
@SuppressWarnings("static-access")
public static void selectBlogInfo(String dataTime, String outIRIFilePath, String outKLOFilePath, String outAnaFilePath) {
try {
Connection con = DBConnectionManager.getInstance().getConnection();
Statement st = con.createStatement();
String sql = "select * from MODELTEC where DATATIME=to_date('"+dataTime+"', 'YYYY-mm-dd HH24')";
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
Blob blod = rs.getBlob("IRIDATA");
InputStream reader = blod.getBinaryStream();
dataTime = dataTime.replaceAll("-", "");
dataTime = dataTime.replaceAll(" ", "");
String iriFilePath = outIRIFilePath+"\\"+dataTime+"0000.iri.grd";
File file = new File(iriFilePath);
OutputStream writer;
writer = new BufferedOutputStream(new FileOutputStream(file));
byte buf[] = new byte[1024];
for (int i = 0; (i = reader.read(buf)) > 0;) {
writer.write(buf, 0, i);
}
writer.close();
reader.close();
blod = rs.getBlob("IRIDATA");
reader = blod.getBinaryStream();
String kloFilePath = outKLOFilePath+"\\"+dataTime+"0000.klo.grd";
file = new File(kloFilePath);
writer = new BufferedOutputStream(new FileOutputStream(file));
buf = new byte[1024];
for (int i = 0; (i = reader.read(buf)) > 0;) {
writer.write(buf, 0, i);
}
writer.close();
reader.close();
blod = rs.getBlob("ANADATA");
reader = blod.getBinaryStream();
String anaFilePath = outAnaFilePath+"\\"+dataTime+"0000.grd";
file = new File(anaFilePath);
writer = new BufferedOutputStream(new FileOutputStream(file));
buf = new byte[1024];
for (int i = 0; (i = reader.read(buf)) > 0;) {
writer.write(buf, 0, i);
}
writer.close();
reader.close();
}
DBConnectionManager.closeConnection();
if(con!=null){con.close();}
if(st!=null){st.close();}
if(rs!=null){rs.close();}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 把IRI和KLO模型的文件上传到数据库中
* @param dataTime 时次
* @param iriFilePath 要上传IRI文件的绝对路径
* @param kloFilePath 要上传KLO文件的据对路径
* @param ANAFilePath 要上传插值文件的据对路径
*/
@SuppressWarnings("static-access")
public static void insertBlogInfo(String dataTime, String IRIFilePath, String KLOFilePath, String ANAFilePath) {
try {
Connection con = DBConnectionManager.getInstance().getConnection();
// 处理事务
boolean defaultCommit;
defaultCommit = con.getAutoCommit();
con.setAutoCommit(false);
Statement st = con.createStatement();
String sql = "select * from MODELTEC where DATATIME=to_date('"+dataTime+"', 'YYYY-mm-dd HH24')";
ResultSet rs = st.executeQuery(sql);
if(rs.next()){
System.out.println(dataTime+"时次已经入库!");
return ;
}
// 插入一个空对象
sql = "insert into MODELTEC(ID, DATATIME, IRIDATA, KLODATA, ANADATA) values(" +
"SEQU_MODEL_ID.nextval, " +
"to_date('"+dataTime+"','YYYY-mm-dd HH24'), " +
"empty_blob(), " +
"empty_blob(), " +
"empty_blob())";
st.executeUpdate(sql);
// 用for update方式锁定数据行
sql = "select IRIDATA,KLODATA,ANADATA from MODELTEC where DATATIME=to_date('"+dataTime+"', 'YYYY-mm-dd HH24') for update";
rs = st.executeQuery(sql);
if (rs.next()) {
// 得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB
BLOB blob = (BLOB) rs.getBlob("IRIDATA");
// 到数据库的输出流
OutputStream outStream = blob.getBinaryOutputStream();
// 这里用一个文件模拟输入流
InputStream fin = new FileInputStream(new File(IRIFilePath));
// 将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
// 依次关闭(注意顺序)
fin.close();
outStream.flush();
outStream.close();
// 得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB
blob = (BLOB) rs.getBlob("KLODATA");
// 到数据库的输出流
outStream = blob.getBinaryOutputStream();
// 这里用一个文件模拟输入流
fin = new FileInputStream(new File(IRIFilePath));
// 将输入流写到输出流
b = new byte[blob.getBufferSize()];
len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
// 依次关闭(注意顺序)
fin.close();
outStream.flush();
outStream.close();
// 得到java.sql.Blob对象,然后Cast为oracle.sql.BLOB
blob = (BLOB) rs.getBlob("ANADATA");
// 到数据库的输出流
outStream = blob.getBinaryOutputStream();
// 这里用一个文件模拟输入流
fin = new FileInputStream(new File(ANAFilePath));
// 将输入流写到输出流
b = new byte[blob.getBufferSize()];
len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
// 依次关闭(注意顺序)
fin.close();
outStream.flush();
outStream.close();
con.commit();
/* 恢复原提交状态 */
con.setAutoCommit(defaultCommit);
DBConnectionManager.closeConnection();
if(con!=null){con.close();}
if(st!=null){st.close();}
if(rs!=null){rs.close();}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}