Upload.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package com.uas.main;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.FileWriter;
  5. import java.io.OutputStreamWriter;
  6. import java.sql.Connection;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.text.DateFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import org.apache.commons.net.ftp.FTPClient;
  20. import org.codehaus.jackson.map.ObjectMapper;
  21. import com.uas.util.BaseUtil;
  22. import com.uas.util.FtpUtil;
  23. import com.uas.util.JdbcUtil;
  24. public class Upload {
  25. public static Pattern pat = Pattern.compile("\\{(.*?)\\}");
  26. public static String getHeader(String xmltemp){ //获取Header
  27. return xmltemp.substring(xmltemp.indexOf("<?xml"),xmltemp.indexOf("<Item>"));
  28. }
  29. public static String getDetailItem(String xmltemp){ //获取Item
  30. return xmltemp.substring(xmltemp.indexOf("<Item>"),xmltemp.indexOf("</Detail"));
  31. }
  32. public static String replaceFieldByData(String str,Map<String,Object> data){ //替换xml字段为具体数据
  33. Matcher mat = pat.matcher(str);
  34. String res = str;
  35. while(mat.find()){
  36. if(data.get(mat.group(1).toUpperCase())!=null){
  37. res = res.replace("{" + mat.group(1).toUpperCase() + "}", replaceXmlSpecialSymbol(data.get(mat.group(1).toUpperCase()).toString()));
  38. }else{
  39. res = res.replace("{" + mat.group(1).toUpperCase() + "}", "");
  40. }
  41. }
  42. return res;
  43. }
  44. public static String replaceXmlSpecialSymbol(String data){
  45. return data.replace("&", "&amp;")
  46. .replace("<", "&lt;")
  47. .replace(">", "&gt;")
  48. .replace("'", "&apos;")
  49. .replace("\"", "&quot;");
  50. }
  51. public static String getCurrentdate(String format){ //获致当时日期
  52. DateFormat df = new SimpleDateFormat(format);
  53. Date today = Calendar.getInstance().getTime();
  54. return df.format(today);
  55. }
  56. public static File createXmlFile(String header,String items,String template,String id,String type){ //生成xml文件
  57. File xmlFile = null;
  58. try{
  59. String xml = header + items + "</Detail>" + template.toString().substring(template.toString().indexOf("</Detail>")+9);
  60. xmlFile = new File(System.getProperty("java.io.tmpdir")+File.separator + type + "-" + getCurrentdate("yyyyMMdd") + id+".xml");
  61. if(!xmlFile.exists()){
  62. xmlFile.createNewFile();
  63. }
  64. FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile());
  65. OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8");
  66. out.write(xml);
  67. out.flush();
  68. out.close();
  69. //fw.write(xml);
  70. fw.close();
  71. }catch(Exception e){
  72. BaseUtil.getLogger().error(e.toString());
  73. e.printStackTrace();
  74. }
  75. return xmlFile;
  76. }
  77. @SuppressWarnings("unchecked")
  78. public static File decodeAndGenerateXml(String template,String datastr,String xlid,String type) throws Exception{
  79. String header = getHeader(template.toString());
  80. String detailItem = getDetailItem(template.toString());
  81. String currentDate = getCurrentdate("yyyyMMddHHmm");
  82. /*System.err.println(datastr);
  83. System.err.println(datastr.substring(5400,5450));*/
  84. Map<String,Object> data = new ObjectMapper().readValue(datastr, HashMap.class);
  85. data.put("FILENAME", type + "-" + data.get("FILENAME"));
  86. data.put("CURRENTDATE", currentDate);
  87. header = replaceFieldByData(header,data);
  88. StringBuffer sb = new StringBuffer();
  89. List<Map<String,Object>> detailData = (List<Map<String,Object>>)data.get("DETAIL");
  90. for(Map<String,Object> detail:detailData){
  91. sb.append(replaceFieldByData(detailItem,detail));
  92. }
  93. File file = createXmlFile(header,sb.toString(),template.toString(),xlid,type);
  94. return file;
  95. }
  96. public static void turnToFormal(Connection connection,String id,File file,String sob){ //临时表数据转入正式
  97. Statement statement = null;
  98. try{
  99. connection.setAutoCommit(false);
  100. statement = connection.createStatement();
  101. statement.execute("insert into "+sob + "."+"xmldatalog(xl_id,xl_data,xl_date,xl_depot,xl_caller,xl_code,xl_from,xl_status,xl_sourceid,xl_filename) "
  102. + "select "+sob + "."+"XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+file.getName()+"' from xmldatalogtemp where xl_id=" + id);
  103. statement.execute("delete from "+sob + "."+"xmldatalogtemp where xl_id=" + id);
  104. connection.commit();
  105. BaseUtil.getLogger().info("trun to formal success!");
  106. }catch(Exception e){
  107. try {
  108. connection.rollback();
  109. } catch (SQLException e1) {
  110. BaseUtil.getLogger().error(e1.toString());
  111. e1.printStackTrace();
  112. }
  113. BaseUtil.getLogger().error(e.toString());
  114. e.printStackTrace();
  115. }finally{
  116. try{
  117. if(statement!=null){
  118. statement.close();
  119. }
  120. }catch(Exception e){
  121. BaseUtil.getLogger().error(e.toString());
  122. }
  123. statement = null;
  124. }
  125. }
  126. public static boolean upload(FTPClient client,String ip,String folder,File file){
  127. boolean flag = false;
  128. if(client!=null){
  129. flag = FtpUtil.uploadFile(client, folder,file);
  130. }
  131. System.out.println("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
  132. BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
  133. return flag;
  134. }
  135. public static void run() {
  136. uploadBySob("YHND_SZ");
  137. uploadBySob("YHND_HK");
  138. uploadBySob("YITOA_ZX");
  139. uploadBySob("YHND_WH");
  140. uploadBySob("YHND_CQ");
  141. }
  142. @SuppressWarnings("unchecked")
  143. public static void uploadBySob(String sob) {
  144. Statement statement = null;
  145. FTPClient client = null;
  146. Connection con = null;
  147. con = JdbcUtil.getConnectBySob(sob);
  148. try{
  149. statement = con.createStatement();
  150. ResultSet rs = statement.executeQuery("select * from "+sob+"."+"xmldatalogtemp where xl_depot is not null order by xl_depot desc");
  151. String depot = "";
  152. while(rs.next()){
  153. try{
  154. String xldepot = rs.getString("xl_depot");
  155. String type = rs.getString("xl_type");
  156. if(!"null".equals(xldepot)&&xldepot!=null&&!"null".equals(type)&&type!=null){
  157. Object template = JdbcUtil.getXmlTemplate(con,sob).get(rs.getString("xl_caller")+"_" + xldepot);
  158. if(template!=null){
  159. Map<String,Object> ftpConfig = JdbcUtil.getFtpConfigs();
  160. Map<String,Object> config = (Map<String,Object>)ftpConfig.get(xldepot);
  161. String folder = config.get(type).toString();
  162. if(!depot.equals(xldepot)){ //连接ftp站点
  163. if(client!=null){
  164. FtpUtil.closeFtpClient(client);
  165. }
  166. depot = xldepot;
  167. client = FtpUtil.connect(config,config.get(type).toString());
  168. }
  169. File file = decodeAndGenerateXml(template.toString(),rs.getString("xl_data"),rs.getString("xl_id"),folder); //生成文件
  170. boolean uploadSuccess = upload(client,config.get("ip").toString(),folder,file);
  171. if(uploadSuccess){ //如果文件上传成功,则转入正式数据记录表
  172. turnToFormal(con,rs.getString("xl_id"),file,sob); //转入正式
  173. }
  174. file.delete();
  175. }else{
  176. BaseUtil.getLogger().info("caller:"+rs.getString("xl_caller")+" depot:"+xldepot+" template is null");
  177. }
  178. }else{
  179. BaseUtil.getLogger().info("warehouse ftpdepot is null");
  180. }
  181. }catch(Exception e){
  182. e.printStackTrace();
  183. BaseUtil.getLogger().error(e.toString());
  184. continue;
  185. }
  186. }
  187. if(client!=null){
  188. FtpUtil.closeFtpClient(client);
  189. client = null;
  190. }
  191. try{
  192. JdbcUtil.clearXmlTemplate();
  193. con.close();
  194. }catch(SQLException e){
  195. e.printStackTrace();
  196. BaseUtil.getLogger().error(e.toString());
  197. }finally{
  198. con = null;
  199. }
  200. }catch(Exception e){
  201. BaseUtil.getLogger().error(e.toString());
  202. e.printStackTrace();
  203. }finally{
  204. if(statement!=null){
  205. try {
  206. statement.close();
  207. } catch (SQLException e1) {
  208. BaseUtil.getLogger().error(e1.toString());
  209. e1.printStackTrace();
  210. }
  211. statement = null;
  212. }
  213. if(client!=null){
  214. FtpUtil.closeFtpClient(client);
  215. }
  216. }
  217. }
  218. }