| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- package com.uas.main;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.OutputStreamWriter;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.apache.commons.net.ftp.FTPClient;
- import org.codehaus.jackson.map.ObjectMapper;
- import com.uas.util.BaseUtil;
- import com.uas.util.FtpUtil;
- import com.uas.util.JdbcUtil;
- public class Upload {
- public static Pattern pat = Pattern.compile("\\{(.*?)\\}");
-
- public static String getHeader(String xmltemp){ //获取Header
- return xmltemp.substring(xmltemp.indexOf("<?xml"),xmltemp.indexOf("<Item>"));
- }
-
- public static String getDetailItem(String xmltemp){ //获取Item
- return xmltemp.substring(xmltemp.indexOf("<Item>"),xmltemp.indexOf("</Detail"));
- }
-
- public static String replaceFieldByData(String str,Map<String,Object> data){ //替换xml字段为具体数据
- Matcher mat = pat.matcher(str);
- String res = str;
- while(mat.find()){
- if(data.get(mat.group(1).toUpperCase())!=null){
- res = res.replace("{" + mat.group(1).toUpperCase() + "}", replaceXmlSpecialSymbol(data.get(mat.group(1).toUpperCase()).toString()));
- }else{
- res = res.replace("{" + mat.group(1).toUpperCase() + "}", "");
- }
- }
- return res;
- }
-
- public static String replaceXmlSpecialSymbol(String data){
- return data.replace("&", "&")
- .replace("<", "<")
- .replace(">", ">")
- .replace("'", "'")
- .replace("\"", """);
- }
-
- public static String getCurrentdate(String format){ //获致当时日期
- DateFormat df = new SimpleDateFormat(format);
- Date today = Calendar.getInstance().getTime();
- return df.format(today);
- }
-
-
- public static File createXmlFile(String header,String items,String template,String id,String type){ //生成xml文件
- File xmlFile = null;
- try{
- String xml = header + items + "</Detail>" + template.toString().substring(template.toString().indexOf("</Detail>")+9);
- xmlFile = new File(System.getProperty("java.io.tmpdir")+File.separator + type + "-" + getCurrentdate("yyyyMMdd") + id+".xml");
- if(!xmlFile.exists()){
- 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.write(xml);
- fw.close();
- }catch(Exception e){
- BaseUtil.getLogger().error(e.toString());
- e.printStackTrace();
- }
- return xmlFile;
- }
-
- @SuppressWarnings("unchecked")
- public static File decodeAndGenerateXml(String template,String datastr,String xlid,String type) throws Exception{
- String header = getHeader(template.toString());
- String detailItem = getDetailItem(template.toString());
- String currentDate = getCurrentdate("yyyyMMddHHmm");
- /*System.err.println(datastr);
- System.err.println(datastr.substring(5400,5450));*/
- Map<String,Object> data = new ObjectMapper().readValue(datastr, HashMap.class);
- data.put("FILENAME", type + "-" + data.get("FILENAME"));
- data.put("CURRENTDATE", currentDate);
- header = replaceFieldByData(header,data);
-
- StringBuffer sb = new StringBuffer();
- List<Map<String,Object>> detailData = (List<Map<String,Object>>)data.get("DETAIL");
- for(Map<String,Object> detail:detailData){
- sb.append(replaceFieldByData(detailItem,detail));
- }
-
- File file = createXmlFile(header,sb.toString(),template.toString(),xlid,type);
- return file;
- }
-
- public static void turnToFormal(Connection connection,String id,File file,String sob){ //临时表数据转入正式
- Statement statement = null;
- try{
- connection.setAutoCommit(false);
- statement = connection.createStatement();
- 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) "
- + "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);
- statement.execute("delete from "+sob + "."+"xmldatalogtemp where xl_id=" + id);
- connection.commit();
- BaseUtil.getLogger().info("trun to formal success!");
- }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;
- }
- }
-
- public static boolean upload(FTPClient client,String ip,String folder,File file){
- boolean flag = false;
- if(client!=null){
- flag = FtpUtil.uploadFile(client, folder,file);
- }
- System.out.println("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
- BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
- return flag;
- }
-
- public static void run() {
- uploadBySob("YHND_SZ");
- uploadBySob("YHND_HK");
- uploadBySob("YITOA_ZX");
- uploadBySob("YHND_WH");
- uploadBySob("YHND_CQ");
- }
-
- @SuppressWarnings("unchecked")
- public static void uploadBySob(String sob) {
- Statement statement = null;
- FTPClient client = null;
-
- Connection con = null;
- con = JdbcUtil.getConnectBySob(sob);
-
- try{
- statement = con.createStatement();
- ResultSet rs = statement.executeQuery("select * from "+sob+"."+"xmldatalogtemp where xl_depot is not null order by xl_depot desc");
- String depot = "";
- while(rs.next()){
- try{
- String xldepot = rs.getString("xl_depot");
- String type = rs.getString("xl_type");
- if(!"null".equals(xldepot)&&xldepot!=null&&!"null".equals(type)&&type!=null){
- Object template = JdbcUtil.getXmlTemplate(con,sob).get(rs.getString("xl_caller")+"_" + xldepot);
- if(template!=null){
- Map<String,Object> ftpConfig = JdbcUtil.getFtpConfigs();
- Map<String,Object> config = (Map<String,Object>)ftpConfig.get(xldepot);
- String folder = config.get(type).toString();
-
- if(!depot.equals(xldepot)){ //连接ftp站点
- if(client!=null){
- FtpUtil.closeFtpClient(client);
- }
- depot = xldepot;
- client = FtpUtil.connect(config,config.get(type).toString());
- }
-
- File file = decodeAndGenerateXml(template.toString(),rs.getString("xl_data"),rs.getString("xl_id"),folder); //生成文件
- boolean uploadSuccess = upload(client,config.get("ip").toString(),folder,file);
- if(uploadSuccess){ //如果文件上传成功,则转入正式数据记录表
- turnToFormal(con,rs.getString("xl_id"),file,sob); //转入正式
- }
- file.delete();
- }else{
- BaseUtil.getLogger().info("caller:"+rs.getString("xl_caller")+" depot:"+xldepot+" template is null");
- }
- }else{
- BaseUtil.getLogger().info("warehouse ftpdepot is null");
- }
- }catch(Exception e){
- e.printStackTrace();
- BaseUtil.getLogger().error(e.toString());
- continue;
- }
- }
- if(client!=null){
- FtpUtil.closeFtpClient(client);
- client = null;
- }
- try{
- JdbcUtil.clearXmlTemplate();
- con.close();
- }catch(SQLException e){
- e.printStackTrace();
- BaseUtil.getLogger().error(e.toString());
- }finally{
- con = null;
- }
-
- }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);
- }
- }
- }
- }
|