/**
*
* @param uploads
* 文件集合
* @param uploadFileNames
* 文件名称集合
* @param apply_id_name
* 外键 id 字段名称
* @param attach_id_fname
* 编号,流水号,自己的id
* @param attach_name_fname
* 附件名称字段
* @param attach_fname
* 附件字段
* @param tname
* 表名
* @param apply_id
* 外键 id 字段名称 对应的值
* @return
* @throws Exception
*/
public boolean insermultipleFile(List<File> uploads,
List<String> uploadFileNames, String apply_id_name,
String attach_id_fname, String attach_name_fname,
String attach_fname, String tname, String apply_id)
throws Exception {
// String insert_sql =
// "insert into t_base_attach(attach_id,apply_id,attach_name,attach,creater,creatime,material_num) values "
// + " (?,?,?,empty_blob(),?,?,?)";
// String update_sql =
// "select attach from t_base_attach where attach_id=? for update";
StringBuffer insert_sql = new StringBuffer();
insert_sql.append("insert into ");
insert_sql.append(tname);
insert_sql.append("(");
insert_sql.append(attach_id_fname).append(",");
insert_sql.append(apply_id_name).append(",");
insert_sql.append(attach_name_fname).append(",");
insert_sql.append(attach_fname);
insert_sql.append(") values ");
insert_sql.append(" (?,?,?,empty_blob()) ");
String update_sql = "select " + attach_fname + " from " + tname
+ " where " + attach_id_fname + "=? for update";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String attach_id = "";
try {
log.debug(insert_sql.toString());
log.debug(update_sql.toString());
conn = jt.getDataSource().getConnection();
// conn.setAutoCommit(true);
for (int i = 0; i < uploads.size(); i++) {
attach_id = queryGuidFromOracle();
ps = conn.prepareStatement(insert_sql.toString());
ps.setString(1, attach_id);
ps.setString(2, apply_id);
ps.setString(3, uploadFileNames.get(i));
ps.executeUpdate();
ps = conn.prepareStatement(update_sql);
ps.setString(1, attach_id);
rs = ps.executeQuery();
if (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs
.getBlob(attach_fname);
// String strfilename = rs.getString("VC2FILENAME");
// 到数据库的输出流
OutputStream outStream = blob.getBinaryOutputStream();
// 这里用一个文件模拟输入流
File file = uploads.get(i);
InputStream fin = null;
try {
fin = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
// blob.putBytes(1,b);
}
// 依次关闭(注意顺序)
fin.close();
outStream.flush();
outStream.close();
}
}
conn.commit();
conn.setAutoCommit(true);
close(conn, ps, rs);
} catch (SQLException e) {
try {
conn.rollback();
e.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
return true;
}