1、HttpClient
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* HTTP封装
*
*/
public class HttpUtil {
// 编码方式
private static final String CONTENT_CHARSET = "UTF-8";
// 连接超时时间
private static final int CONNECTION_TIMEOUT = 30000;
// 读数据超时时间
private static final int READ_DATA_TIMEOUT = 30000;
/**
* HTTP POST请求
*
* @param uri
* @param params
* @return
* @throws Exception
*/
public final static String post(String uri, Map<String, String> params) throws Exception {
PostMethod postMethod = new PostMethod(uri);
List<NameValuePair> data = new ArrayList<NameValuePair>();
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
NameValuePair pair = new NameValuePair(entry.getKey(), entry.getValue());
data.add(pair);
}
}
postMethod.setRequestBody(data.toArray(new NameValuePair[data.size()]));
// 设置编码
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
// 不进行重连
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
HttpClient client = new HttpClient();
// 链接超时
client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIMEOUT);
// 读取超时
client.getHttpConnectionManager().getParams().setSoTimeout(READ_DATA_TIMEOUT);
String response = null;
InputStream in = null;
BufferedReader br = null;
try {
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("http状态码:" + statusCode);
}
in = postMethod.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(in, CONTENT_CHARSET));
StringBuffer stringBuffer = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
// 接口返回结果
response = stringBuffer.toString();
} finally {
try {
if (in != null)
in.close();
if (br != null)
br.close();
postMethod.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
}
2、JSONUtils
package ********;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
/**
* json工具类
*
*/
public class JsonUtils {
private static final ObjectMapper objectMapper;
static {
objectMapper = new ObjectMapper();
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
}
public static String objectJsonEncode(Object objectSource) throws Exception {
return objectMapper.writeValueAsString(objectSource);
}
public static <T> T objectJsonDecode(String objectSource, Class<T> clazz) throws Exception {
return objectMapper.readValue(objectSource, clazz);
}
public static <K, V> Map<K, V> objectJsonMapDecode(String objectSource, Class<K> keyClass, Class<V> valueClass) throws Exception {
return objectMapper.readValue(objectSource, TypeFactory.defaultInstance().constructMapType(HashMap.class, keyClass, valueClass));
}
public static <K, V> Map<Integer, Map<String, String>> objectJsonMapMapDecode(String objectSource, Class<K> keyClass, Class<V> valueClass) throws Exception {
return objectMapper.readValue(objectSource, TypeFactory.defaultInstance().constructMapType(HashMap.class, keyClass, valueClass));
}
public static <K, V> Map<Long, Map<String, Object>> objectJsonMapObjectDecode(String objectSource, Class<K> keyClass, Class<V> valueClass) throws Exception {
return objectMapper.readValue(objectSource, TypeFactory.defaultInstance().constructMapType(HashMap.class, keyClass, valueClass));
}
public static <T> List<T> objectJsonArrayDecode(String objectSource, Class<T> clazz) throws Exception {
List<T> list = null;
list = objectMapper.readValue(objectSource, TypeFactory.defaultInstance().constructCollectionType(List.class, clazz));
return list;
}
}
3、TimerUtils
package *******;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具类
*
*
*/
public class TimeUtil {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
/**
* 当前时间毫秒
*
* @return
*/
public static long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 返回指定格式时间字符串
*
* @param time
* @param format
* @return
*/
public static String toTime(Date date, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
/**
* 返回默认格式时间字符串
*
* @param time
* @return
*/
public static String toTime(Date date) {
return toTime(date, DATE_FORMAT);
}
/**
* 返回指定格式时间字符串
*
* @param time
* @param format
* @return
*/
public static String toTime(long time, String format) {
Date date = new Date(time);
return toTime(date, format);
}
/**
* 返回默认格式时间字符串
*
* @param time
* @return
*/
public static String toTime(long time) {
return toTime(time, DATE_FORMAT);
}
/**
* 返回时间日期
*
* @param date
* @return
*/
public static Date toDate(String date) {
return toDate(date, DATE_FORMAT);
}
/**
* 返回指定格式时间日期
*
* @param date
* @param format
* @return
*/
public static Date toDate(String date, String format) {
if (date != null && date.length() > 0) {
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
}
}
return null;
}
/**
* 判断能两个时间是否同一天
*
* @param d1
* @param d2
* @return
*/
public static boolean isSameDay(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
&& (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));
}
/**
* 返回当天0点时间
*
* @return
*/
public static long getMorningTimeMillis() {
return getMorningTimeMillis(new Date());
}
/**
* 返回指定时间当天的0点时间(毫秒)
*
* @param time
* @return
*/
public static long getMorningTimeMillis(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
/**
* 获取明天零点时间
*
* @return
*/
public static long getTomorrowMorningTimeMillis() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
/**
* 获取两个时间相差多少天
*
* @param time1
* @param time2
* @return
*/
public static int getDay(int time1, int time2) {
return (time2 - time1) / (3600 * 24);
}
/**
* 获取两个时间相差分钟数
*
* @param time1
* @param time2
* @return
*/
public static int getMinute(int time1, int time2) {
return (time2 - time1) / (60);
}
}