From f731b9adc29fa64eeb13ef91abc432cea6aa07ef Mon Sep 17 00:00:00 2001 From: lixiangrong Date: Thu, 28 Apr 2016 15:56:38 +0800 Subject: [PATCH] Change-Id: I6d773b85ba240a0b2965cbd5c51f8ef66231ce96 --- src/main/java/osc/git/eh3/sftp/SFTPUtil.java | 87 ++++++++++++++----- .../eh3/test/OperateKPIBudgetRedisData.java | 2 +- src/main/java/osc/git/eh3/test/TestCode.java | 74 +++++++++++++++- .../osc/git/eh3/testopen/TestOpenPuton.java | 8 +- src/main/resources/generatorConfig.xml | 71 ++++++++------- 5 files changed, 186 insertions(+), 56 deletions(-) diff --git a/src/main/java/osc/git/eh3/sftp/SFTPUtil.java b/src/main/java/osc/git/eh3/sftp/SFTPUtil.java index b1c3c3c..6be2f12 100644 --- a/src/main/java/osc/git/eh3/sftp/SFTPUtil.java +++ b/src/main/java/osc/git/eh3/sftp/SFTPUtil.java @@ -1,22 +1,27 @@ package osc.git.eh3.sftp; import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; -import java.io.OutputStream; import java.util.Properties; +import java.util.Vector; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; +import com.jcraft.jsch.SftpException; /** * SFTP工具类 + * * @author lixiangrong * */ public class SFTPUtil { + private static Log log = LogFactory.getLog(SFTPUtil.class); private static String ip; private static String user; private static String psw; @@ -40,11 +45,13 @@ public class SFTPUtil { propFile = null; } } catch (Exception e) { + log.error("读取sftp配置文件失败:" + e.getMessage()); e.printStackTrace(); } } private static ChannelSftp getSFTP() throws Exception { + log.info("正在连接服务器[" + ip + "]....."); ChannelSftp sftp = null; JSch jsch = new JSch(); if (port <= 0) { @@ -57,6 +64,7 @@ public class SFTPUtil { // 如果服务器连接不上,则抛出异常 if (session == null) { + log.error("连接服务器[" + ip + "]失败....."); throw new Exception("session is null"); } @@ -72,9 +80,11 @@ public class SFTPUtil { channel = (Channel) session.openChannel("sftp"); channel.connect(1000); sftp = (ChannelSftp) channel; - + log.info("连接服务器[" + ip + "]成功....."); } catch (Exception e) { + log.error("服务器[" + ip + "]创建sftp通信通道失败:" + e.getMessage()); e.printStackTrace(); + closeSFTP(); } return sftp; } @@ -90,42 +100,77 @@ public class SFTPUtil { /** * SFTP上传文件 - * @param desPath ftp服务器目录 - * @param desFileName 上传后的文件名 - * @param resFile 要上传的文件 + * + * @param desPath + * ftp服务器目录 + * @param desFileName + * 上传后的文件名 + * @param resFile + * 要上传的文件 * @throws Exception */ - public static void putFile(String desPath, String desFileName, File resFile) throws Exception { + public static void putFile(String desPath, String desFileName, File resFile) { try { ChannelSftp sftp = getSFTP(); + mkdirs(sftp, desPath); // 进入服务器指定的文件夹 sftp.cd(desPath); - OutputStream outstream = sftp.put(desFileName); - InputStream instream = new FileInputStream(resFile); - - byte b[] = new byte[1024]; - int n; - while ((n = instream.read(b)) != -1) { - outstream.write(b, 0, n); - } - outstream.flush(); - outstream.close(); - instream.close(); - + sftp.put(resFile.getAbsolutePath(), desFileName, ChannelSftp.OVERWRITE); + log.info("文件[" + desPath + desFileName + "]上传完成..."); closeSFTP(); } catch (Exception e) { + log.error("文件[" + desPath + desFileName + "]上传失败:" + e.getMessage()); e.printStackTrace(); + closeSFTP(); } } public static void main(String[] args) { try { - SFTPUtil.putFile("/data", "212321.txt", new File("D:/1.txt")); + SFTPUtil.putFile("/upload/222/1111", "212321.txt", new File("D:/1.txt")); } catch (Exception e) { e.printStackTrace(); } } -} + + /** + * 在远程服务器创建多级目录 + * + * @param sftp + * @param desPath + * @throws Exception + */ + private static void mkdirs(ChannelSftp sftp, String desPath) throws Exception { + String[] paths = desPath.split("/"); + String path = ""; + for (int i = 0; i < paths.length; i++) { + path += paths[i] + "/"; + if (!isExistDir(sftp, path)) { + sftp.mkdir(path); + } + } + } + + /** + * 判断远程目录是否存在 + * + * @param sftp + * @param desPath + * @return + */ + private static boolean isExistDir(ChannelSftp sftp, String desPath) { + boolean isExist = false; + try { + Vector content = sftp.ls(desPath); + if (content != null) { + isExist = true; + } + } catch (SftpException e) { + isExist = false; + } + return isExist; + } +} \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java b/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java index bb49210..15a1e4e 100644 --- a/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java +++ b/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java @@ -9,7 +9,7 @@ public class OperateKPIBudgetRedisData { public static void main(String[] args) throws IOException { - showSom("09e5f11c-5c6d-44e4-a599-1d2981d283c3"); + showSom("58d355d7-4c66-461c-b63c-146c149bdcac"); // delete("c46b8885-4c1d-48f9-befb-3ba3a0f33d4a"); } diff --git a/src/main/java/osc/git/eh3/test/TestCode.java b/src/main/java/osc/git/eh3/test/TestCode.java index 9688259..2c6691d 100644 --- a/src/main/java/osc/git/eh3/test/TestCode.java +++ b/src/main/java/osc/git/eh3/test/TestCode.java @@ -1,14 +1,19 @@ package osc.git.eh3.test; import java.math.BigDecimal; +import java.net.URLDecoder; +import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.Set; +import java.util.UUID; import java.util.regex.Pattern; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; import osc.git.eh3.utils.AESTool; import osc.git.eh3.utils.Base64; @@ -246,8 +251,73 @@ public class TestCode { // } // System.out.println(budget.doubleValue()); - String REG_FLOAT = "^[1-9]\\d*.?\\d+$"; // 浮点正数 - System.out.println(Pattern.compile(REG_FLOAT).matcher("1.21").matches()); +// String REG_FLOAT = "^[1-9]\\d*.?\\d+$"; // 浮点正数 +// System.out.println(Pattern.compile(REG_FLOAT).matcher("1.21").matches()); + +// String str ="浮点数sss"; +// String s1 = new String(str.getBytes("utf-8"),"gbk"); +// System.out.println(s1); +// System.out.println(new String(s1.getBytes("gbk"))); +// System.out.println(); +// +// String s2 = URLEncoder.encode(str, "utf-8"); +// System.out.println(s2); +// System.out.println(URLDecoder.decode(s2,"utf-8")); + + +// Object object = null; +// JSONObject creativeGroupObj = JSONObject.fromObject(object); +// System.out.println(creativeGroupObj.isEmpty()); +// +// System.out.println(UUID.randomUUID().toString()); + +// JSONArray putTime = JSONArray.fromObject("[{\"monday\":[\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"tuesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"wednesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"thursday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"friday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"saturday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"sunday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]}]"); +// JSONArray periods = new JSONArray(); +// for (Object object : putTime) { +// JSONObject putTimeObj = JSONObject.fromObject(object); +// if (!putTimeObj.isEmpty()) { +// Set keySet = putTimeObj.keySet(); +// JSONObject period = new JSONObject(); +// for (String key : keySet) { +// JSONArray value = putTimeObj.optJSONArray(key); +// int start = -1,end = -1; +// StringBuffer sb = new StringBuffer(); +// for (int i = 0; i < value.size(); i++) { +// Object object2 = value.get(i); +// // 第一次出现 1 +// if (object2.equals("1") && start==-1) { +// start=i; +// end = 0; +// } +// // 出现1后的第一次出现0结束 +// if (object2.equals("0") && start>-1) { +// end=i-1; +// sb.append(start+"-"+end+","); +// start = -1;end = -1; +// } +// } +// period.put("week", key); +// period.put("ranges",sb.toString().substring(0, (sb.length()-1))); +// } +// periods.add(period); +// } +// } +// System.out.println(periods.toString()); + +// JSONObject period = new JSONObject(); +// period.put("test", 100.32); +// System.out.println(period.optString("test")); + +// BigDecimal clicks = new BigDecimal(100.23); +// System.out.println(clicks.intValue()); + +// System.out.println(Long.parseLong("8000.01")); + + JSONObject jsonParam = new JSONObject(); + JSONArray jsonArray = new JSONArray(); + jsonArray.add("000000"); + jsonParam.put("app", jsonArray); + System.out.println(jsonParam); } diff --git a/src/main/java/osc/git/eh3/testopen/TestOpenPuton.java b/src/main/java/osc/git/eh3/testopen/TestOpenPuton.java index e77cc81..5202837 100644 --- a/src/main/java/osc/git/eh3/testopen/TestOpenPuton.java +++ b/src/main/java/osc/git/eh3/testopen/TestOpenPuton.java @@ -10,7 +10,7 @@ import osc.git.eh3.utils.Base64; import osc.git.eh3.utils.HttpClientUtil; public class TestOpenPuton { - public static String URL = "http://127.0.0.1:3/dsp-open/opendsp.do"; + public static String URL = "http://127.0.0.1:8080/dsp-open/pxene/dsp.do"; private static String key = "adjdjfjfjfjdkdkd";// private static String appid = "t123456";// 用户 private static String token = "cst123456";// 令牌 @@ -23,8 +23,10 @@ public class TestOpenPuton { JSONObject json = new JSONObject(); - json.put("param", "sdfdfdsfsdf"); - HttpClientUtil.sendPostJSONData(URL, json.toString()); + json.put("param", "中午"); + String str = "{ \"order_info\": { \"order_id\": 1001, \"order_name\": \"test\", \"brand_zh\": \"复歌\", \"brand_en\": \"fugetech\", \"note\": \"note\", \"industry\": \"个人护理/药品>护发品\", \"cost_type\": 1, \"cost_single\": 124, \"ad_owner\": {}, \"start_time\": \"2015-12-01\", \"end_time\": \"2015-12-30\", \"buy_type\": 1, \"plat\": 1, \"type\": 1, \"landing_page\": \"www.fugetech.com\", \"order_area\": [], \"order_budget\": {}, \"put_time\": [], \"order_goal\": {}, \"order_freq\": {}, \"order_pmp\": {}, \"media_white\": [], \"media_black\": [], \"creative_group\": [] } }"; + + System.out.println(HttpClientUtil.sendPostJSONData(URL, str)); } public static String getData(String encryptString) throws Exception { diff --git a/src/main/resources/generatorConfig.xml b/src/main/resources/generatorConfig.xml index 3b1e1de..27913ea 100644 --- a/src/main/resources/generatorConfig.xml +++ b/src/main/resources/generatorConfig.xml @@ -1,29 +1,42 @@ - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
\ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
\ No newline at end of file