| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- 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<String,Object> ftpConfig = JdbcUtil.getFtpConfigs();
- Map<String,Object> 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<String,Object>)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");
- }
- }
|