public boolean addFileContent(final DocTransfer docTransfer, String enconding, boolean isBom, String fileName) {
boolean bool = false; StringBuffer updateXmlSqlStr = new StringBuffer();
// DOC_TRANSFER_BACKUP DOC_TRANSFER
String sql2 = updateXmlSqlStr.append("insert into ").append(" EDI.DOC_TRANSFER(").append(" FILE_TRANS_ID , BUSINESS_ID,MESSAGE_ID,FUNCTION_CODE,MESSAGE_TYPE,REPRESENTATIVE_PERSON_NAME,")
.append(" SENDER_CODE,RECEIVER_CODE,TRANS_FILE_NAME,SEND_STATUS,SEND_USER,SEND_TIME,OLD_VERSION_FILE_NAME,SGN_COMPANY,TRANS_FILE_CONTENT ) ")
.append(" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,empty_Blob()) ").toString();
processUpdateWithoutBatch(sql2, new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
long currentTime = System.currentTimeMillis();
if (docTransfer.getSendTime() != null) {
currentTime = docTransfer.getSendTime().getTime();
}
ps.setLong(1, docTransfer.getFileTransId().longValue());
// System.out.println("numbrId="+docTransfer.getBusinessId());
ps.setLong(2, docTransfer.getFileTransId().longValue());
ps.setString(3, docTransfer.getMessageId());
ps.setString(4, docTransfer.getFunctionCode());
ps.setString(5, docTransfer.getMessageType());
ps.setString(6, docTransfer.getRepresentativePersonName());
ps.setString(7, docTransfer.getSenderCode());
ps.setString(8, docTransfer.getReceiverCode());
ps.setString(9, docTransfer.getTransFileName());
ps.setString(10, docTransfer.getSendStatus());
ps.setString(11, docTransfer.getSendUser());// SGN_COMPANY
ps.setTimestamp(12, new Timestamp(currentTime));
ps.setString(13, docTransfer.getOldVersionFileName());
ps.setString(14, docTransfer.getSgnCompany());
}
});
updateXmlSqlStr.setLength(0);
updateXmlSqlStr.append("select TRANS_FILE_CONTENT ").append("from EDI.DOC_TRANSFER ").append(" where FILE_TRANS_ID=?").append(" for update");
final java.sql.Blob blob = (java.sql.Blob) processQuery(updateXmlSqlStr.toString(), new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
System.out.println("numbrId=" + docTransfer.getFileTransId());
ps.setLong(1, docTransfer.getFileTransId().longValue());
}
}, new ResultSetCallBacker() {
public Object extractData(ResultSet rs) throws SQLException {
java.sql.Blob blob = null;
if (rs.next()) {
blob = rs.getBlob(1);// rs.getClob(1);
// log.info("CLob==="+String.valueOf(clob));
}
return blob;
}
});
try {
OutputStream out = (OutputStream) MethodUtils.invokeMethod(blob, "getBinaryOutputStream", null);
BufferedWriter writer = null;
File file = new File(fileName);
InputStream fin = new FileInputStream(file);
if (docTransfer.getTransFileName().endsWith(".ZIP")) {
// 将输入流写到输出流
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fin.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
// 依次关闭(注意顺序)
fin.close();
out.close();
} else if(docTransfer.getTransFileName().endsWith(".xml")||docTransfer.getTransFileName().endsWith(".XML")){
//String S = new String (docTransfer.getTransFileContent().getBytes("UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(fin,"UTF-8"));
String data = null;
while ((data = br.readLine()) != null) {
//System.out.println(data);
out.write(data.getBytes("UTF-8"));
out.flush();
}
br.close();
out.close();
fin.close();
}else{
if(!ManifestDeclConstats.DOC_CONTENT_ENCODING_UTF_8.equals(StringHelp.toUpperString(StringHelp.trimNull(enconding)))){
if(isBom==true){
final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
out.write(bom);
}
writer = new BufferedWriter(new OutputStreamWriter(out,"GBK"));
}else{
if(isBom==true){
final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
out.write(bom);
}
writer = new BufferedWriter(new OutputStreamWriter(out,ManifestDeclConstats.DOC_CONTENT_ENCODING_UTF_8));
}
writer.write(docTransfer.getTransFileContent());
fin.close();
writer.close();
out.close();
}
} catch (Exception e) {
log.info("获取数据错误");
bool = false;
return bool;
// throw new RuntimeException(e);
}
updateXmlSqlStr.setLength(0);
updateXmlSqlStr.append("update ").append("EDI.DOC_TRANSFER set TRANS_FILE_CONTENT =? where FILE_TRANS_ID=?").toString();
processUpdateWithoutBatch(updateXmlSqlStr.toString(), new PreparedStatementSetter() {
public void setValues(PreparedStatement ps) throws SQLException {
// ps.setClob(1, clob);
ps.setBlob(1, blob);
// ps.setString(2, mftSendRecords.getTransFileContent());
ps.setLong(2, docTransfer.getFileTransId().longValue());
}
});
return true;
写入ZIP压缩文件与XML 文件到oracle数据库
最新推荐文章于 2021-04-12 02:35:40 发布