UploadServiceImpl.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package com.uas.service.Impl;
  2. import com.jcraft.jsch.JSchException;
  3. import com.uas.service.UploadService;
  4. import com.uas.util.BaseUtil;
  5. import com.uas.util.FtpUtil;
  6. import com.uas.util.JdbcUtil;
  7. import com.uas.util.SFTPUtil;
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.springframework.scheduling.annotation.Async;
  10. import org.springframework.stereotype.Service;
  11. import java.io.*;
  12. import java.sql.Connection;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.Map;
  17. import java.util.regex.Pattern;
  18. @Service
  19. public class UploadServiceImpl implements UploadService {
  20. private Pattern pat = Pattern.compile("\\{(.*?)\\}");
  21. @Override
  22. public void uploadRun() {
  23. //联合创泰
  24. uploadBySob("N_YITOA_LHCT");
  25. //联合创泰电子
  26. uploadBySob("LHCT_SZ");
  27. //供应链测试
  28. uploadBySob("N_YITOA_LHCT_T");
  29. //联合创泰香港
  30. //uploadBySob("N_YITOA_LHCT_HK");
  31. }
  32. @Override
  33. @SuppressWarnings({ "unchecked"})
  34. @Async("taskExecutor")
  35. public void uploadBySob(String sob) {
  36. BaseUtil.getLogger().info("upload" + " from "+sob);
  37. Statement statement = null;
  38. FTPClient client = null;
  39. SFTPUtil sftp = null;
  40. String xldata = null;
  41. String fileprefix = null;
  42. String xldate = null;
  43. String xlid = null;
  44. String xldepot = null;
  45. Map<String,Object> ftpConfig = JdbcUtil.getFtpConfigs();
  46. Map<String,Object> config = null;
  47. Connection connect = null;
  48. try{
  49. connect = JdbcUtil.getConnectBySob(sob);
  50. if(connect!=null){
  51. statement = connect.createStatement();
  52. ResultSet rs = getXmlData(statement);
  53. String depot = "";
  54. while(rs.next()){
  55. try{
  56. xldata = rs.getString("xl_data");
  57. fileprefix = rs.getString("xl_fileprefix");
  58. xldate = rs.getString("xl_date");
  59. xlid = rs.getString("xl_id");
  60. xldepot = rs.getString("xl_depot");
  61. if(xldata!=null&&xldate!=null){
  62. //连接ftp站点
  63. if(!depot.equals(xldepot)){
  64. if(client!=null){
  65. FtpUtil.closeFtpClient(client);
  66. }
  67. depot = xldepot;
  68. //注意上传位置是数据库名+xl_depot
  69. config = (Map<String,Object>)ftpConfig.get(sob + "-" + depot);
  70. if(config==null){
  71. continue;
  72. }
  73. System.out.println(config);
  74. if ("SF".equals(depot)||"ZX".equals(depot)){
  75. sftp = new SFTPUtil(config.get("user").toString(),config.get("password").toString(),config.get("ip").toString(),
  76. Integer.parseInt(config.get("port").toString()));
  77. try {
  78. sftp.login();
  79. } catch (JSchException e) {
  80. e.printStackTrace();
  81. }
  82. }else {
  83. client = FtpUtil.connect(config,config.get("in").toString());
  84. }
  85. if(client==null&&sftp==null){
  86. continue;
  87. }
  88. }
  89. if(config==null){
  90. continue;
  91. }
  92. String folder = config.get("in").toString();
  93. if(xldata==null||"".equals(xldata)){
  94. continue;
  95. }
  96. boolean uploadSuccess = false;
  97. //生成文件
  98. File file = createXmlFile(xldata,xlid,folder,fileprefix,xldate);
  99. if (client!=null) {
  100. uploadSuccess = upload(client, config.get("ip").toString(), folder, file);
  101. }
  102. if (sftp!=null){
  103. InputStream is = new FileInputStream(file);
  104. uploadSuccess = sftp.upload("/", folder, file.getName(), is);
  105. }
  106. //如果文件上传成功,则转入正式数据记录表
  107. if(uploadSuccess){
  108. //转入正式
  109. turnToFormal(connect,xlid,file.getName());
  110. }
  111. }
  112. }catch(Exception e){
  113. e.printStackTrace();
  114. BaseUtil.getLogger().error(e.toString());
  115. continue;
  116. }
  117. }
  118. rs.close();
  119. }
  120. }catch(Exception e){
  121. BaseUtil.getLogger().error(e.toString());
  122. e.printStackTrace();
  123. }finally{
  124. if(statement!=null){
  125. try {
  126. statement.close();
  127. } catch (SQLException e1) {
  128. BaseUtil.getLogger().error(e1.toString());
  129. e1.printStackTrace();
  130. }
  131. statement = null;
  132. }
  133. if(client!=null){
  134. FtpUtil.closeFtpClient(client);
  135. }
  136. //把当前的连接关闭
  137. try {
  138. if(connect!=null){
  139. connect.close();
  140. }
  141. } catch (SQLException e) {
  142. e.printStackTrace();
  143. }finally{
  144. connect = null;
  145. }
  146. }
  147. }
  148. private File createXmlFile(String data,String id,String type,String fileprefix,String date){ //生成xml文件
  149. File xmlFile = null;
  150. try{
  151. String xml =data;
  152. xmlFile = new File( System.getProperty("java.io.tmpdir")+File.separator +fileprefix+date + "_" + id +".xml");
  153. if(!xmlFile.exists()){
  154. if(!xmlFile.getParentFile().exists()){
  155. xmlFile.mkdir();
  156. }
  157. xmlFile.createNewFile();
  158. }
  159. FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile());
  160. OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8");
  161. out.write(xml);
  162. out.flush();
  163. out.close();
  164. fw.close();
  165. }catch(Exception e){
  166. BaseUtil.getLogger().error(e.toString());
  167. e.printStackTrace();
  168. }
  169. return xmlFile;
  170. }
  171. private void turnToFormal(Connection connection,String id,String fileName){ //临时表数据转入正式
  172. Statement statement = null;
  173. try{
  174. connection.setAutoCommit(false);
  175. statement = connection.createStatement();
  176. statement.setQueryTimeout(180);
  177. 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) "
  178. + "select XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+fileName+"' from xmldatalogtemp where xl_id=" + id);
  179. statement.execute("delete from xmldatalogtemp where xl_id=" + id);
  180. connection.commit();
  181. }catch(Exception e){
  182. try {
  183. connection.rollback();
  184. } catch (SQLException e1) {
  185. BaseUtil.getLogger().error(e1.toString());
  186. e1.printStackTrace();
  187. }
  188. BaseUtil.getLogger().error(e.toString());
  189. e.printStackTrace();
  190. }finally{
  191. try{
  192. if(statement!=null){
  193. statement.close();
  194. }
  195. }catch(Exception e){
  196. BaseUtil.getLogger().error(e.toString());
  197. }
  198. statement = null;
  199. }
  200. }
  201. private boolean upload(FTPClient client,String ip,String folder,File file){
  202. boolean flag = false;
  203. if(client!=null){
  204. flag = FtpUtil.uploadFile(client, folder,file);
  205. }
  206. BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
  207. return flag;
  208. }
  209. private ResultSet getXmlData(Statement statement) throws SQLException{
  210. 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");
  211. }
  212. }