Commit e205463d by zt

初步完成,待测试

parent 06320eea
......@@ -44,6 +44,30 @@
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<!-- json转换工具-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package com.bbd.email;
import com.bbd.utils.PropertiesUtil;
import com.opendata.api.ODPRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
/**
* 发邮件
*
* @author zangtao
* @create 2019 - 04 -11 16:25
*/
public class Email {
private static final Logger logger = LoggerFactory.getLogger(Email.class);
public static String sendEmail(){
//读取配置文件
Properties properties = PropertiesUtil.getProperties();
//接口地址
String emailUrl = properties.getProperty("emailUrl");
//接口地址
String method = properties.getProperty("emailMethod");
//接口地址
String Appsecret = properties.getProperty("Appsecret");
//接口地址
String AccessToken = properties.getProperty("AccessToken");
String res = new ODPRequest(emailUrl, Appsecret)
.addTextSysPara("Method", method)
.addTextSysPara("AccessToken", AccessToken)
.addTextSysPara("Format", "json")
//应用参数
// .addTextAppPara("From", "sdhkyxglzx@hnair.com")//邮件服务发件人参数//sdhkyxglzx@hnair.com
// .addTextAppPara("To", "zangtao@bbdtek.com")//邮件服务收件人参数
// .addTextAppPara("UserName", "sdhkyxglzx")//发件人的内网账号
// .addTextAppPara("UserPwd", Coder.getBASE64("075.wxp"))//发件人的密码,需要base64编码
// .addTextAppPara("Subject", "邮件标题-测试")//邮件标题
// .addTextAppPara("Body", Coder.getBASE64("邮件正文,测试英文字符:You and me are working in the same organization for the same purpose."))//邮件内容参数,需要base64编码
//// .addTextAppPara("Attachments", mapList)//附件
// .post();
.addTextAppPara("StartDate", "2016-11-15")
.addTextAppPara("EndDate", "2016-11-15").post();
return res;
}
}
package com.bbd.entity;
/**
* 短信公共参数
*
* @author zangtao
* @create 2019 - 04 -11 15:39
*/
public class AccessInfo {
private String aicc;
private String Aict;
private String aicp;
private String aisign;
public String getAicc() {
return aicc;
}
public void setAicc(String aicc) {
this.aicc = aicc;
}
public String getAict() {
return Aict;
}
public void setAict(String aict) {
Aict = aict;
}
public String getAicp() {
return aicp;
}
public void setAicp(String aicp) {
this.aicp = aicp;
}
public String getAisign() {
return aisign;
}
public void setAisign(String aisign) {
this.aisign = aisign;
}
}
package com.bbd.sms;
import com.bbd.entity.AccessInfo;
import com.bbd.utils.HttpClientUtils;
import com.bbd.utils.PropertiesUtil;
import com.bbd.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
/**
* 发送短信
*
* @author zangtao
* @create 2019 - 04 -11 15:35
*/
public class Sms {
private static final Logger logger = LoggerFactory.getLogger(Sms.class);
public static String sendSms(AccessInfo accessInfo,String mobile,String msg){
//读取配置文件
Properties properties = PropertiesUtil.getProperties();
String smsUrl = properties.getProperty("smsUrl");
String result;
HttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
+ "&mobile=" + mobile + "&msg=" + msg);
HttpResponse response = httpClient.execute(httpPost);
logger.info(response.toString());
}catch (Exception ex){
ex.printStackTrace();
}
//发送get请求
// if (StringUtils.isEmpty(accessInfo.getAisign())){
// // + "&Ai.ct=" + accessInfo.getAict()
// result = HttpClientUtils.doGet(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
// + "&mobile=" + mobile + "&msg=" + msg );
// }else{
// result = HttpClientUtils.doGet(smsUrl + "?ai.cp=" + accessInfo.getAicp() + "&ai.cc=" + accessInfo.getAicc()
// + "&Ai.ct=" + accessInfo.getAict() + "&ai.sign=" + accessInfo.getAisign() + "&mobile=" + mobile + "&msg=" + msg );
// }
return null;
}
public static void main(String[] args) {
AccessInfo accessInfo = new AccessInfo();
accessInfo.setAicc("5");
accessInfo.setAicp("123.56.146.7");
// accessInfo.setAict("21");
String res = Sms.sendSms(accessInfo,"13222650486","HelloWorld");
System.out.println(res);
}
}
package com.bbd.utils;
import sun.misc.BASE64Decoder;
import java.io.*;
public class Coder {
// 将 s 进行 BASE64 编码
public static String getBASE64(byte[] s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s);
}
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}
// 将 BASE64 编码的字符串 s 进行解码
public static String getFromBASE64(String s) {
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) {
return null;
}
}
public static String fileCode(String fileurl) throws Exception {
String result = null;
// 第1步、使用File类找到一个文件
File f = new File(fileurl); // 声明File对象
// 第2步、通过子类实例化父类对象
InputStream input = null; // 准备好一个输入的对象
input = new FileInputStream(f); // 通过对象多态性,进行实例化
// 第3步、进行读操作
// byte b[] = new byte[input..available()] ; 跟使用下面的代码是一样的
byte b[] = new byte[(int) f.length()]; // 数组大小由文件决定
int len = input.read(b); // 读取内容
// 第4步、关闭输出流
input.close(); // 关闭输出流\
// System.out.println("读入数据的长度:" + len);
result = Coder.getBASE64(b);
// System.out.println("内容为:" + result); // 把byte数组变为字符串输出
return result;
}
public static String fileStringCode(String fileurl) throws Exception {
String result = "";
String encoding = "GBK";
File file = new File(fileurl);
if (file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
result += lineTxt;
}
// System.out.println("内容为:" + result);
read.close();
}
return result;
}
}
package com.bbd.utils;
import com.google.common.collect.Lists;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author lisu
* @date 2016/6/13 15:50
*/
public class HttpClientUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
static int successCode = 200;
public static String doGet(String url, Map<String, String> param) {
return doGetWithAuth(url, null, param, null);
}
public static String doGet(String url, Map<String, String> headers, Map<String, String> param) {
return doGetWithAuth(url, headers, param, null);
}
public static String doGet(String url, Map<String, String> headers, Map<String, Object> param,
Map<String, String> auth) {
// 创建Httpclient对象
CloseableHttpClient httpclient = getCloseableHttpClient(auth);
String resultString = "";
CloseableHttpResponse response = null;
try {
if (param != null) {
StringBuilder sb = new StringBuilder("");
for (String key : param.keySet()) {
sb.append(key + "=" + param.get(key) + "&");
}
url += "?" + sb.toString();
}
logger.info("doGetWithAuth请求url:{}" + url);
// 创建http GET请求
HttpGet httpGet = new HttpGet(url);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == successCode) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGetWithAuth(String url, Map<String, String> headers, Map<String, String> param,
Map<String, String> auth) {
// 创建Httpclient对象
CloseableHttpClient httpclient = getCloseableHttpClient(auth);
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
logger.info("doGetWithAuth请求Uri:{}" + uri);
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == successCode) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
return doPost(url, param, null);
}
public static String doPostWithAuth(String url, Map<String, String> param, Map<String, String> head,
Map<String, String> auth) {
CloseableHttpClient httpClient = getCloseableHttpClient(auth);
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = Lists.newArrayListWithCapacity(param.size());
paramList.addAll(param.keySet().stream().map(key -> new BasicNameValuePair(key, param.get(key))).collect(Collectors.toList()));
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "utf-8");
httpPost.setEntity(entity);
}
if (head != null) {
List<Header> headers = Lists.newArrayListWithCapacity(head.size());
headers.addAll(head.keySet().stream().map(key -> new BasicHeader(key, head.get(key)))
.collect(Collectors.toList()));
httpPost.setHeaders(headers.toArray(new Header[headers.size()]));
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
private static CloseableHttpClient getCloseableHttpClient(Map<String, String> auth) {
// 创建Httpclient对象
CloseableHttpClient httpClient;
if (!CollectionUtils.isEmpty(auth)) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(auth.get("userName"),
auth.get("password"));
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
} else {
httpClient = HttpClients.createDefault();
}
return httpClient;
}
public static String doPost(String url, Map<String, String> param, Map<String, String> head) {
return doPostWithAuth(url, param, head, null);
}
public static String doPost(String url) {
return doPost(url, null);
}
public static String doPostJsonWithAuth(String url, String json, Map<String, String> auth) {
// 创建Httpclient对象
CloseableHttpClient httpClient = getCloseableHttpClient(auth);
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doPostJson(String url, String json) {
return doPostJsonWithAuth(url, json, null);
}
}
package com.bbd.utils;
import java.io.InputStream;
import java.util.Properties;
/**
* @author 臧涛
*/
public class PropertiesUtil {
private PropertiesUtil() {
}
private static Properties properties = null;
public static Properties getProperties() {
if (properties == null) {
try {
Properties properties = new Properties();
InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("global.properties");
properties.load(in);
return properties;
} catch (Exception e) {
e.printStackTrace();
}
}
return properties;
}
}
package com.bbd.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author lisu
*/
public class StringUtils {
public static boolean rightRandom(String str) {
String regex = "^[a-zA-Z0-9]{6}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
boolean isMatch = m.matches();
return isMatch;
}
/**
* 手机号码校验
*
* @param phone 手机号
* @param regex 正则
* @return
*/
public static boolean isPhone(String phone, String regex) {
if (phone.length() != 11) {
return false;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
return isMatch;
}
}
/**
* 把原始字符串分割成指定长度的字符串列表
*
* @param inputString 原始字符串
* @param length 指定长度
* @return
*/
public static List<String> getStrList(String inputString, int length) {
int size = inputString.length() / length;
if (inputString.length() % length != 0) {
size += 1;
}
return getStrList(inputString, length, size);
}
/**
* 把原始字符串分割成指定长度的字符串列表
*
* @param inputString 原始字符串
* @param length 指定长度
* @param size 指定列表大小
* @return
*/
public static List<String> getStrList(String inputString, int length, int size) {
List<String> list = new ArrayList<>();
for (int index = 0; index < size; index++) {
String childStr = substring(inputString, index * length, (index + 1) * length);
list.add(childStr);
}
return list;
}
/**
* 分割字符串,如果开始位置大于字符串长度,返回空
*
* @param str 原始字符串
* @param f 开始位置
* @param t 结束位置
* @return
*/
public static String substring(String str, int f, int t) {
if (f > str.length()) {
return null;
}
if (t > str.length()) {
return str.substring(f, str.length());
} else {
return str.substring(f, t);
}
}
/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
public static String decryptBASE64(String key) {
byte[] decodedBytes = Base64.getDecoder().decode(key);
return new String(decodedBytes);
}
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(String key) {
return Base64.getEncoder().encodeToString(key.getBytes());
}
public static boolean isEmpty(String str) {
return str == null || "".equals(str);
}
/**
* 利用java原生的摘要实现SHA256加密
*
* @param str 加密后的报文
* @return
*/
public static String getSHA256(String str) {
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
* 将byte转为16进制
*
* @param bytes
* @return
*/
private static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
//1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
/**
* 获取6位随机字符
*
* @return
*/
public static String getRandom(){
String a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] rands = new char[6];
for (int i = 0; i < rands.length; i++) {
int rand = (int) (Math.random() * a.length());
rands[i] = a.charAt(rand);
}
StringBuffer stringBuffer = new StringBuffer();
for(int i=0;i<rands.length;i++){
stringBuffer = stringBuffer.append(rands[i]);
}
return stringBuffer.toString();
}
/**
* 获取精确到秒的时间戳
* @return
*/
public static int getSecondTimestamp(Date date){
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime());
int length = timestamp.length();
if (length > 3) {
return Integer.valueOf(timestamp.substring(0,length-3));
} else {
return 0;
}
}
}
#短信外网测试URL
smsUrl=http://114.251.242.194:808/flightinterface/uss/json/mobile/messSend.json
#短信内网测试URL
#smsUrl=http://10.70.35.68:808/flightinterface/uss/json/mobile/messSend.json?
#邮件测试环境
emailUrl=http://10.70.72.110/api/inner/ESBService
#邮件正式环境
#emailUrl=http://esb.hna.net/api
#API接口名称,查看个人订单-接口名称
emailMethod=Exchange_MailService_SendMail
#app密钥,查看个人中心-app key
Appsecret=9eztwb08qdvkzk0zadzdvtl6j1bssqvp
#应用票据,查看个人中心-app key
AccessToken=A6BD747ECB212285E6DC3528EFB482AF04B40F17
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局参数 -->
<settings>
<!-- 使全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true"/>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
<setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
<setting name="useGeneratedKeys" value="false"/>
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
<setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
<setting name="defaultExecutorType" value="SIMPLE"/>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
<setting name="localCacheScope" value="SESSION"/>
<!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 -->
<setting name="jdbcTypeForNull" value="NULL"/>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://106.75.97.50:3306/jd_foc"/>
<property name="username" value="focuser"/>
<property name="password" value="1qaz@WSX2019"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--<mapper resource="mapper/*.xml"/>-->
<mapper resource="mapper/AppMapper.xml"/>
<mapper resource="mapper/SmsMapper.xml"/>
<mapper resource="mapper/StatisticMapper.xml"/>
<!-- Mapper扫描包,必须同目录同名称下-->
<!--<package name="com.sms.statistic.mapper"/>-->
</mappers>
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment