package com.uas.service.Impl; import com.jcraft.jsch.JSchException; import com.uas.service.UploadService; import com.uas.util.BaseUtil; import com.uas.util.FtpUtil; import com.uas.util.JdbcUtil; import com.uas.util.SFTPUtil; import org.apache.commons.net.ftp.FTPClient; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.*; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Map; import java.util.regex.Pattern; @Service public class UploadServiceImpl implements UploadService { private Pattern pat = Pattern.compile("\\{(.*?)\\}"); @Override public void uploadRun() { //联合创泰 uploadBySob("N_YITOA_LHCT"); //联合创泰电子 uploadBySob("LHCT_SZ"); //供应链测试 uploadBySob("N_YITOA_LHCT_T"); //联合创泰香港 //uploadBySob("N_YITOA_LHCT_HK"); } @Override @SuppressWarnings({ "unchecked"}) @Async("taskExecutor") public void uploadBySob(String sob) { BaseUtil.getLogger().info("upload" + " from "+sob); Statement statement = null; FTPClient client = null; SFTPUtil sftp = null; String xldata = null; String fileprefix = null; String xldate = null; String xlid = null; String xldepot = null; Map ftpConfig = JdbcUtil.getFtpConfigs(); Map config = null; Connection connect = null; try{ connect = JdbcUtil.getConnectBySob(sob); if(connect!=null){ statement = connect.createStatement(); ResultSet rs = getXmlData(statement); String depot = ""; while(rs.next()){ try{ xldata = rs.getString("xl_data"); fileprefix = rs.getString("xl_fileprefix"); xldate = rs.getString("xl_date"); xlid = rs.getString("xl_id"); xldepot = rs.getString("xl_depot"); if(xldata!=null&&xldate!=null){ //连接ftp站点 if(!depot.equals(xldepot)){ if(client!=null){ FtpUtil.closeFtpClient(client); } depot = xldepot; //注意上传位置是数据库名+xl_depot config = (Map)ftpConfig.get(sob + "-" + depot); if(config==null){ continue; } System.out.println(config); if ("SF".equals(depot)||"ZX".equals(depot)){ sftp = new SFTPUtil(config.get("user").toString(),config.get("password").toString(),config.get("ip").toString(), Integer.parseInt(config.get("port").toString())); try { sftp.login(); } catch (JSchException e) { e.printStackTrace(); } }else { client = FtpUtil.connect(config,config.get("in").toString()); } if(client==null&&sftp==null){ continue; } } if(config==null){ continue; } String folder = config.get("in").toString(); if(xldata==null||"".equals(xldata)){ continue; } boolean uploadSuccess = false; //生成文件 File file = createXmlFile(xldata,xlid,folder,fileprefix,xldate); if (client!=null) { uploadSuccess = upload(client, config.get("ip").toString(), folder, file); } if (sftp!=null){ InputStream is = new FileInputStream(file); uploadSuccess = sftp.upload("/", folder, file.getName(), is); } //如果文件上传成功,则转入正式数据记录表 if(uploadSuccess){ //转入正式 turnToFormal(connect,xlid,file.getName()); } } }catch(Exception e){ e.printStackTrace(); BaseUtil.getLogger().error(e.toString()); continue; } } rs.close(); } }catch(Exception e){ BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); }finally{ if(statement!=null){ try { statement.close(); } catch (SQLException e1) { BaseUtil.getLogger().error(e1.toString()); e1.printStackTrace(); } statement = null; } if(client!=null){ FtpUtil.closeFtpClient(client); } //把当前的连接关闭 try { if(connect!=null){ connect.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ connect = null; } } } private File createXmlFile(String data,String id,String type,String fileprefix,String date){ //生成xml文件 File xmlFile = null; try{ String xml =data; xmlFile = new File( System.getProperty("java.io.tmpdir")+File.separator +fileprefix+date + "_" + id +".xml"); if(!xmlFile.exists()){ if(!xmlFile.getParentFile().exists()){ xmlFile.mkdir(); } xmlFile.createNewFile(); } FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile()); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8"); out.write(xml); out.flush(); out.close(); fw.close(); }catch(Exception e){ BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); } return xmlFile; } private void turnToFormal(Connection connection,String id,String fileName){ //临时表数据转入正式 Statement statement = null; try{ connection.setAutoCommit(false); statement = connection.createStatement(); statement.setQueryTimeout(180); statement.execute("insert into xmldatalog(xl_id,xl_data,xl_date,xl_depot,xl_caller,xl_code,xl_from,xl_status,xl_sourceid,xl_filename) " + "select XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+fileName+"' from xmldatalogtemp where xl_id=" + id); statement.execute("delete from xmldatalogtemp where xl_id=" + id); connection.commit(); }catch(Exception e){ try { connection.rollback(); } catch (SQLException e1) { BaseUtil.getLogger().error(e1.toString()); e1.printStackTrace(); } BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); }finally{ try{ if(statement!=null){ statement.close(); } }catch(Exception e){ BaseUtil.getLogger().error(e.toString()); } statement = null; } } private boolean upload(FTPClient client,String ip,String folder,File file){ boolean flag = false; if(client!=null){ flag = FtpUtil.uploadFile(client, folder,file); } BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+""); return flag; } private ResultSet getXmlData(Statement statement) throws SQLException{ return statement.executeQuery("select xl_id,xl_data,xl_fileprefix,to_char(xl_date,'yyyymmddhh24miss') xl_date,xl_depot from xmldatalogtemp where xl_data is not null order by xl_depot desc"); } }