Skip to content

Commit

Permalink
重构;HttpClientUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
zxiaofan committed Apr 7, 2017
1 parent 9d58e89 commit bc97721
Show file tree
Hide file tree
Showing 27 changed files with 216 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .classpath
Expand Up @@ -15,6 +15,8 @@
<classpathentry kind="lib" path="lib/jedis-2.9.0.jar" sourcepath="lib/jedis-2.9.0-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-pool2-2.4.2-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-pool2-2.4.2.jar" sourcepath="lib/commons-pool2-2.4.2-sources.jar"/>
<classpathentry kind="lib" path="lib/httpclient-4.5.2.jar"/>
<classpathentry kind="lib" path="lib/httpcore-4.4.5.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added lib/httpclient-4.5.2.jar
Binary file not shown.
Binary file added lib/httpcore-4.4.5.jar
Binary file not shown.
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年3月28日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.jsonBean;

import java.io.BufferedReader;
import java.io.File;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年11月6日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.jsonBean;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down
Expand Up @@ -7,7 +7,7 @@
* 修改时间:2016年7月20日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.jsonBean;

import java.io.BufferedReader;
import java.io.File;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年1月11日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.io.BufferedWriter;
import java.io.File;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年9月26日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年11月6日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.text.DateFormat;
import java.text.ParseException;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年11月17日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.io.File;
import java.io.FileInputStream;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年11月23日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年11月6日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

/**
*
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2017年2月5日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.other;

import java.io.BufferedOutputStream;
import java.io.File;
Expand Down
@@ -1,4 +1,4 @@
package utils;
package com.zxiaofan.util.other;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
Expand Down
@@ -1,5 +1,5 @@

package passwordUtil;
package com.zxiaofan.util.password;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
Expand Down
46 changes: 46 additions & 0 deletions src/com/zxiaofan/util/password/Check.java
@@ -0,0 +1,46 @@
/*
* 文件名:Check.java
* 版权:Copyright 2007-2017 zxiaofan.com. Co. Ltd. All Rights Reserved.
* 描述: Check.java
* 修改人:zxiaofan
* 修改时间:2017年4月7日
* 修改内容:新增
*/
package com.zxiaofan.util.password;

/**
* 校验器,支持生成字典数据时立即校验.
*
* @author xiaofan
*
*/
public class Check {
int minLen = 1; // 执行execute参数param最小位数

int maxLen = Integer.MAX_VALUE;

/**
* 构造函数.
*
* @param minLen
* 最少位数
* @param maxLen
* 最多位数
*/
public Check(int minLen, int maxLen) {
super();
this.minLen = minLen;
this.maxLen = maxLen;
}

/**
* 校验.
*
* @param param
* 参数
* @return 校验结果
*/
protected boolean execute(String param) {
return false;
};
}
@@ -1,4 +1,4 @@
package passwordUtil;
package com.zxiaofan.util.password;

import java.security.Key;
import java.security.SecureRandom;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2017年2月12日
* 修改内容:新增
*/
package passwordUtil;
package com.zxiaofan.util.password;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -128,40 +128,3 @@ private static List<String> permutation(List<String> elements, int num, Check ch
}

}

/**
* 校验器,支持生成字典数据时立即校验.
*
* @author xiaofan
*
*/
class Check {
int minLen = 1; // 执行execute参数param最小位数

int maxLen = Integer.MAX_VALUE;

/**
* 构造函数.
*
* @param minLen
* 最少位数
* @param maxLen
* 最多位数
*/
public Check(int minLen, int maxLen) {
super();
this.minLen = minLen;
this.maxLen = maxLen;
}

/**
* 校验.
*
* @param param
* 参数
* @return 校验结果
*/
boolean execute(String param) {
return false;
};
}
@@ -1,4 +1,4 @@
package passwordUtil;
package com.zxiaofan.util.password;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down
@@ -1,4 +1,4 @@
package privateUtil;
package com.zxiaofan.util.privateUtil;

import java.io.File;
import java.io.FileOutputStream;
Expand Down
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2016年9月30日
* 修改内容:新增
*/
package privateUtil;
package com.zxiaofan.util.privateUtil;

import java.io.File;
import java.io.FileInputStream;
Expand Down
Expand Up @@ -3,7 +3,7 @@
* @author zxiaofan
*
*/
package privateUtil;
package com.zxiaofan.util.privateUtil;
/**
* 私有工具类
*
Expand Down
138 changes: 138 additions & 0 deletions src/com/zxiaofan/util/rpc/HttpClientUtil.java
@@ -0,0 +1,138 @@
/*
* 文件名:FileUtil.java
* 版权:Copyright 2007-2016 zxiaofan.com. Co. Ltd. All Rights Reserved.
* 描述: FileUtil.java
* 修改人:zxiaofan
* 修改时间:2017年04月07日
* 修改内容:新增
*/
package com.zxiaofan.util.rpc;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;

/**
* HttpClient的Http工具类.
*
* @author yunhai
*/
public class HttpClientUtil {
/**
* 默认超时时间.
*/
public static int timeoutDefault = 60000;

/**
* 请求参数配置.
*/
private static RequestConfig requestConfigDefault = null;

/**
* HttpClient-post(默认超时时间timeoutDefault).
*
* @param httpClient
* httpClient
* @param httpPost
* httpPost
* @return result
* @throws Exception
* Exception
*/
public static String execute(HttpClient httpClient, HttpPost httpPost) throws Exception {
httpPost.setConfig(getDefaultRequestConfig());
HttpResponse execute = httpClient.execute(httpPost);
String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
return result;
}

/**
* HttpClient-post.
*
* @param httpClient
* httpClient
* @param httpPost
* httpPost
* @param requestConfig
* requestConfig
* @return result
* @throws Exception
* Exception
*/
public static String execute(HttpClient httpClient, HttpPost httpPost, RequestConfig requestConfig) throws Exception {
httpPost.setConfig(requestConfig);
HttpResponse execute = httpClient.execute(httpPost);
String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
return result;
}

/**
* HttpClient-Get(默认超时时间timeoutDefault).
*
* @param httpClient
* httpClient
* @param httpGet
* httpGet
* @return String
* @throws Exception
* Exception
*/
public static String execute(HttpClient httpClient, HttpGet httpGet) throws Exception {
httpGet.setConfig(getDefaultRequestConfig());
HttpResponse execute = httpClient.execute(httpGet);
String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
return result;
}

/**
* HttpClient-Get.
*
* @param httpClient
* httpClient
* @param httpGet
* httpGet
* @param requestConfig
* requestConfig
* @return String
* @throws Exception
* Exception
*/
public static String execute(HttpClient httpClient, HttpGet httpGet, RequestConfig requestConfig) throws Exception {
httpGet.setConfig(requestConfig);
HttpResponse execute = httpClient.execute(httpGet);
String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
return result;
}

/**
* 获取RequestConfig.
*
* @param timeout
* 超时时间
* @return RequestConfig
*/
public static RequestConfig getRequestConfig(int timeout) {
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
return requestConfig;
}

/**
* 获取RequestConfig(默认超时timeoutDefault).
*
* @return RequestConfig
*/
public static RequestConfig getDefaultRequestConfig() {
if (null == requestConfigDefault) {
synchronized (HttpClientUtil.class) {
if (null == requestConfigDefault) {
requestConfigDefault = RequestConfig.custom().setConnectionRequestTimeout(timeoutDefault).setSocketTimeout(timeoutDefault).setConnectTimeout(timeoutDefault).build();
}
}
}
return requestConfigDefault;
}

}
Expand Up @@ -6,7 +6,7 @@
* 修改时间:2017年2月8日
* 修改内容:新增
*/
package utils;
package com.zxiaofan.util.rpc;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
Expand Down
@@ -1,4 +1,4 @@
package utils;
package com.zxiaofan.util.spring;

import java.util.Map;

Expand Down

0 comments on commit bc97721

Please sign in to comment.