Java常用公共工具记录

1、日期处理工具

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期常用格式转换
 */
public class DateTimeUtil {

    static {
        ymdhmsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        ymdhmsFormat2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        ymdFormat = new SimpleDateFormat("yyyyMMdd");
        ymdFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        hmsFormat = new SimpleDateFormat("HHmmss");
        ymFormat = new SimpleDateFormat("yyyyMM");
        c = Calendar.getInstance();
    }

    private static SimpleDateFormat ymdhmsFormat;

    private static SimpleDateFormat ymdhmsFormat2;

    private static SimpleDateFormat ymdFormat;
    
    private static SimpleDateFormat ymdFormat2;

    private static SimpleDateFormat hmsFormat;

    private static SimpleDateFormat ymFormat;//年月

    private static Calendar c;

    public static Date dateOnly(Date date) {
        return yyyyMMddToDate(parseToyyyyMMdd(date));
    }

    /**
     * 转换为 yyyyMMddHHmmss格式
     */
    public static String parseToyyyyMMddHHmmss(Date date) {

        if (date == null) {
            return null;
        }
        return ymdhmsFormat.format(date);

    }
    
    /**
     * 转换为 yyyyMMdd HH:mm:ss格式
     */
    public static String parseToyyyyMMddHHmmss2(Date date) {

        if (date == null) {
            return null;
        }
        return ymdhmsFormat2.format(date);

    }

    /**
     * 转换为HHmmss格式
     */
    public static String parseToHHmmss(Date date) {
        if (date == null) {
            return null;
        }
        return hmsFormat.format(date);
    }

    /**
     * 转换为yyyyMMdd格式
     */
    public static String parseToyyyyMMdd(Date date) {
        if (date == null) {
            return null;
        }

        return ymdFormat.format(date);
    }

    /**
     * 转换为yyyyMM格式
     */
    public static int parseToyyyyMM(Date date) {
        if (date == null) {
            return 0;
        }

        return Integer.valueOf(ymFormat.format(date));
    }

    public static Date yyyyMMddHHmmssToDate(String yyyyMMddHHmmss) {
        try {

            return ymdhmsFormat.parse(yyyyMMddHHmmss);
        }
        catch (Exception e) {
            return null;
        }

    }

    public static Date yyyyMMddToDate(String yyyyMMdd) {
        try {

            return ymdFormat.parse(yyyyMMdd);
        }
        catch (Exception e) {
            return null;
        }

    }

    public static Date yyyyMMToDate(String yyyyMM) {
        try {

            return ymFormat.parse(yyyyMM);
        }
        catch (Exception e) {
            return null;
        }

    }
    
    /**
     * yyyy-MM-dd转换成date
     * @author linbingwen
     * @since  2016年4月14日 
     * @param yyyyMMdd2
     * @return
     */
    public static Date yyyyMMddToDate2(String yyyyMMdd2) {
        try {

            return ymdFormat2.parse(yyyyMMdd2);
        }
        catch (Exception e) {
            return null;
        }

    }

    public static Date HHmmssToDate(String HHmmss) {
        try {

            return hmsFormat.parse(HHmmss);
        }
        catch (Exception e) {
            return null;
        }

    }

    public static Date getDate(Date srcDate, Integer daysToAdd) {

        c.setTime(srcDate);
        c.add(Calendar.DATE, daysToAdd); // number of days to add

        return c.getTime();
    }

    public static Date yyyyMMddHHmmssToDate2(String yyyyMMddHHmmss) {
        try {
            return ymdhmsFormat2.parse(yyyyMMddHHmmss);
        }
        catch (Exception e) {
            return null;
        }

    }

    public static final int daysBetween(Date early, Date late) {

        java.util.Calendar calst = java.util.Calendar.getInstance();
        java.util.Calendar caled = java.util.Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        // 设置时间为0时
        calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
        calst.set(java.util.Calendar.MINUTE, 0);
        calst.set(java.util.Calendar.SECOND, 0);
        caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
        caled.set(java.util.Calendar.MINUTE, 0);
        caled.set(java.util.Calendar.SECOND, 0);
        // 得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;

        return days;
    }

    public static Date getNextDayOfWeek(Date date, int dayOfWeek) {
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        if (dayOfWeek > 7 || dayOfWeek < 1) {
            throw new RuntimeException("星期:" + dayOfWeek + "不存在");
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        while (true) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (preWeekDay(day) == dayOfWeek) {
                return cal.getTime();
            }
            cal.add(Calendar.DATE, 1);
        }
    }

    public static Date getNextMonthDate(Date date, int nextMonthDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int day = cal.get(Calendar.DATE);
        if (day <= nextMonthDate) {
            cal.set(Calendar.DATE, nextMonthDate);
        }
        else {
            cal.set(Calendar.DATE, 1);
            cal.add(Calendar.MONTH, 1);
            cal.set(Calendar.DATE, nextMonthDate);
        }
        return cal.getTime();
    }

    public static int nextWeekDay(int day) {
        if (day == 7) {
            return 1;
        }
        else {
            return day++;
        }
    }

    public static int preWeekDay(int day) {
        if (day == 1) {
            return 7;
        }
        else {
            return day - 1;
        }
    }
    
	/**
	 * 计算两个日期相差的天数
	 * @param beginDate 【YYYYMMDD】
	 * @param endDate  【YYYYMMDD】
	 * @return Integer 
	 * @author linbingwen
	 * @since 2015年7月21日
	 */
	public static long diffDate(Date beginDate,Date endDate){						
			Calendar theCa1= Calendar.getInstance();  
			Calendar theCa2= Calendar.getInstance();  
			theCa1.setTime(beginDate);  
			theCa2.setTime(endDate);      
	        long between_days=(theCa2.getTimeInMillis()-theCa1.getTimeInMillis())/(1000*3600*24);          
	        return between_days;
	}
	
	/**
	 * 分钟差
	* @Title: diffMinute 
	* @Description: TODO
	* @author : liuqiuyun
	* @param @param beginDate
	* @param @param endDate
	* @param @return    设定文件 
	* @return long    返回类型 
	* @throws 
	 */
	public static long diffMinute(Date beginDate,Date endDate){						
		Calendar theCa1= Calendar.getInstance();  
		Calendar theCa2= Calendar.getInstance();  
		theCa1.setTime(beginDate);  
		theCa2.setTime(endDate);      
        long between_minutes=(theCa2.getTimeInMillis()-theCa1.getTimeInMillis())/(1000*60);          
        return between_minutes;
	}
	
	/**
	 * 获取月份差第一天
	* @Title: getMonthFirstDate 
	* @Description: TODO
	* @author : liuqiuyun
	* @param @param date
	* @param @param monthToAdd
	* @param @param minOrMax 月初还是月末
	* @param @return    设定文件 
	* @return Date    返回类型 
	* @throws 
	 */
	public static Date getMonthFirstDate(Date date,int monthToAdd, String minOrMax) {
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, monthToAdd);
		if(minOrMax.equals("min")){
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		}else if(minOrMax.equals("max")){
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		}
		
		return calendar.getTime();
	}
	
	public static long getLastMonth(Date date) {
		Date lastDate = getMonthFirstDate(date,-1,"min");
		long lastMonth = parseToyyyyMM(lastDate);
		
		return lastMonth;
	}
	
	

    public static void main(String[] args) throws InterruptedException {
//        Calendar cal = Calendar.getInstance();
//        System.out.println(" cal.get(Calendar.DAY_OF_WEEK);:" + cal.get(Calendar.DAY_OF_WEEK));
//        System.out.println(" cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);:" + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
//
//        System.out.println(getNextDayOfWeek(cal.getTime(), 0));
//        System.out.println(getNextDayOfWeek(cal.getTime(), 7));
//        System.out.println(getNextDayOfWeek(cal.getTime(), 1));
//        System.out.println(getNextDayOfWeek(cal.getTime(), 2));
//
//        System.out.println(getNextMonthDate(cal.getTime(), 0));
//        System.out.println(parseToyyyyMMdd(getNextMonthDate(cal.getTime(), 15)));       
        
        System.out.println(parseToyyyyMMdd(getMonthFirstDate(yyyyMMddToDate("20160618"),-1,"max")));
        
//        System.out.println(yyyyMMddToDate2("2012-09-01"));
//
//        Date start = new Date();
//        System.out.println(start);
//        Thread.sleep(60*1000*5+1000);
//        Date end = new Date();
//        System.out.println(end);
//        System.out.println(diffMinute(start,end));
    }
}
2、http请求处理工具

import java.io.ByteArrayInputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import com.lz.common.util.http.FileItem;

public class HttpClientUtil {

	private static Logger logger = LoggerFactory
			.getLogger(HttpClientUtil.class);

	public static void main(String[] args) {
		HttpURLConnection conn = null;
		InputStream ins = null;
		try {
//			String boundary = System.currentTimeMillis() + "";
//			String ctype = "multipart/form-data;boundary=" + boundary;
//			conn = HttpClientUtil
//					.getConnection(
//							null,
//							new URL(
//									"http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/64/CkvJo1cQVPyATVbKACIyO0-AKoo7735712"),
//							"GET", ctype, null);
//			conn.setConnectTimeout(1000);
//			conn.setReadTimeout(5000);
			byte[] bytes = downLoadFile("http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/64/CkvJo1cQVPyATVbKACIyO0-AKoo7735712");
            InputStream inputStream =new  ByteArrayInputStream(bytes);
			// ins = conn.getInputStream();
			File file = new File("D:/6.docx");
			FileOutputStream fot = new FileOutputStream(file);
			com.slob.util.io.IOUtil.inputStreamToOutputStream(inputStream, fot);
		} catch (Exception e) {

		}
	}
	
	public static byte[] downLoadFile(String url) throws IOException {
		HttpURLConnection conn = null;
		InputStream ins = null;
		byte[] bytes  = null;
		try {
			String boundary = System.currentTimeMillis() + "";
			String ctype = "multipart/form-data;boundary=" + boundary;
			conn = HttpClientUtil.getConnection(null,new URL(url),"GET", ctype, null);
			conn.setConnectTimeout(1000);
			conn.setReadTimeout(5000);
			ins = conn.getInputStream();
			bytes = readBytes(ins);
			return bytes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (ins != null) {
				ins.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
	}

	public static String uploadFile(String url, String fieldName,
			String fileName, InputStream ips, ResponseProcess respProcess)
			throws IOException {

		HttpURLConnection conn = null;
		OutputStream out = null;
		try {
			String boundary = System.currentTimeMillis() + "";
			String ctype = "multipart/form-data;boundary=" + boundary;
			conn = HttpClientUtil.getConnection(null, new URL(url), "POST",
					ctype, null);
			conn.setConnectTimeout(1000);
			conn.setReadTimeout(5000);
			out = conn.getOutputStream();

			byte[] entryBoundaryBytes = ("\r\n--" + boundary + "\r\n")
					.getBytes("UTF-8");
			out.write(entryBoundaryBytes);

			byte[] data = new byte[1024 * 1024];
			int size = ips.read(data);
			byte[] fileBytes = getFileEntry(fieldName, fileName,
					getMimeType(data), "UTF-8");
			out.write(fileBytes);

			while (size > 0) {
				out.write(data, 0, size);
				size = ips.read(data);
			}
			byte[] endBoundaryBytes = ("\r\n--" + boundary + "--\r\n")
					.getBytes("UTF-8");
			out.write(endBoundaryBytes);

			return respProcess.processResponse(conn);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (out != null) {
				out.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
	}

	// public static String doPost(Proxy proxy, String url, String requestType,
	// Map<String, String> params,
	// Map<String, FileItem> fileParams, String charset, int connectTimeout, int
	// readTimeout,
	// Map<String, String> headerMap, ResponseProcess respProcess) throws
	// IOException {
	//
	// String boundary = System.currentTimeMillis() + "";
	// HttpURLConnection conn = null;
	// OutputStream out = null;
	// String rsp = null;
	// try {
	// try {
	// String ctype = "multipart/form-data;boundary=" + boundary;
	// conn = getConnection(proxy, new URL(url), requestType, ctype, headerMap);
	// conn.setConnectTimeout(connectTimeout);
	// conn.setReadTimeout(readTimeout);
	// }
	// catch (IOException e) {
	// logger.error(url, e);
	// throw e;
	// }
	//
	// try {
	// out = conn.getOutputStream();
	//
	// byte[] entryBoundaryBytes = ("\r\n--" + boundary +
	// "\r\n").getBytes(charset);
	//
	// if (params != null) {
	// // 文本
	// Set<Entry<String, String>> textEntrySet = params.entrySet();
	// for (Entry<String, String> textEntry : textEntrySet) {
	// byte[] textBytes = getTextEntry(textEntry.getKey(), textEntry.getValue(),
	// charset);
	// out.write(entryBoundaryBytes);
	// out.write(textBytes);
	// }
	// }
	//
	// // 文件
	// if (fileParams != null) {
	// Set<Entry<String, FileItem>> fileEntrySet = fileParams.entrySet();
	// for (Entry<String, FileItem> fileEntry : fileEntrySet) {
	// FileItem fileItem = fileEntry.getValue();
	// if (fileItem.getContent() == null) {
	// continue;
	// }
	// byte[] fileBytes = getFileEntry(fileEntry.getKey(),
	// fileItem.getFileName(),
	// fileItem.getMimeType(), charset);
	// out.write(entryBoundaryBytes);
	// out.write(fileBytes);
	// out.write(fileItem.getContent());
	// }
	// }
	//
	// byte[] endBoundaryBytes = ("\r\n--" + boundary +
	// "--\r\n").getBytes(charset);
	// out.write(endBoundaryBytes);
	// rsp = respProcess.processResponse(conn);
	// }
	// catch (IOException e) {
	// logger.error(url, e);
	// throw e;
	// }
	// }
	// finally {
	// if (out != null) {
	// out.close();
	// }
	// if (conn != null) {
	// conn.disconnect();
	// }
	// }
	//
	// return rsp;
	// }

	public static String doGet(Proxy proxy, String url, String charset,
			ResponseProcess respProcess) throws IOException {

		if (url == null) {
			return "";
		}
		HttpURLConnection conn = null;
		String rsp = null;
		try {
			String ctype = "application/x-www-form-urlencoded;charset="
					+ charset;
			conn = getConnection(proxy, new URL(url), "GET", ctype, null);
			rsp = respProcess.processResponse(conn);
		} catch (IOException e) {
			logger.error(url, e);
			throw e;
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}

		return rsp;
	}

	private static class DefaultTrustManager implements X509TrustManager {

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public void checkClientTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}
	}

	public static HttpURLConnection getConnection(Proxy proxy, URL url,
			String method, String ctype, Map<String, String> headerMap)
			throws IOException {
		HttpURLConnection conn = null;
		if ("https".equals(url.getProtocol())) {
			SSLContext ctx = null;
			try {
				ctx = SSLContext.getInstance("TLS");
				ctx.init(new KeyManager[0],
						new TrustManager[] { new DefaultTrustManager() },
						new SecureRandom());
			} catch (Exception e) {
				throw new IOException(e);
			}
			HttpsURLConnection connHttps;
			if (proxy != null) {
				connHttps = (HttpsURLConnection) url.openConnection(proxy);
			} else {
				connHttps = (HttpsURLConnection) url.openConnection();
			}
			connHttps.setSSLSocketFactory(ctx.getSocketFactory());
			connHttps.setHostnameVerifier(new HostnameVerifier() {

				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
			conn = connHttps;
		} else {
			if (proxy != null) {
				conn = (HttpURLConnection) url.openConnection(proxy);
			} else {
				conn = (HttpURLConnection) url.openConnection();
			}
		}

		conn.setRequestMethod(method);
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setRequestProperty("Accept",
				"text/xml,text/javascript,text/html,application/json");
		conn.setRequestProperty("User-Agent", "java");
		conn.setRequestProperty("Content-Type", ctype);
		if (headerMap != null) {
			for (Map.Entry<String, String> entry : headerMap.entrySet()) {
				conn.setRequestProperty(entry.getKey(), entry.getValue());
			}
		}
		return conn;
	}

	private static byte[] getTextEntry(String fieldName, String fieldValue,
			String charset) throws IOException {
		StringBuilder entry = new StringBuilder();
		entry.append("Content-Disposition:form-data;name=\"");
		entry.append(fieldName);
		entry.append("\"\r\nContent-Type:text/plain\r\n\r\n");
		entry.append(fieldValue);
		return entry.toString().getBytes(charset);
	}

	private static byte[] getFileEntry(String fieldName, String fileName,
			String mimeType, String charset) throws IOException {
		StringBuilder entry = new StringBuilder();
		entry.append("Content-Disposition:form-data;name=\"");
		entry.append(fieldName);
		entry.append("\";filename=\"");
		entry.append(fileName);
		entry.append("\"\r\nContent-Type:");
		entry.append(mimeType);
		entry.append("\r\n\r\n");
		return entry.toString().getBytes(charset);
	}

	public static interface ResponseProcess {

		String processResponse(HttpURLConnection conn);
	};

	public static String getMimeType(byte[] bytes) {
		String suffix = getFileSuffix(bytes);
		String mimeType;

		if ("JPG".equals(suffix)) {
			mimeType = "image/jpeg";
		} else if ("GIF".equals(suffix)) {
			mimeType = "image/gif";
		} else if ("PNG".equals(suffix)) {
			mimeType = "image/png";
		} else if ("BMP".equals(suffix)) {
			mimeType = "image/bmp";
		} else {
			mimeType = "application/octet-stream";
		}

		return mimeType;
	}

	/**
	 * 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
	 * 
	 * @param bytes
	 *            文件字节流
	 * @return JPG, GIF, PNG or null
	 */
	public static String getFileSuffix(byte[] bytes) {
		if (bytes == null || bytes.length < 10) {
			return null;
		}

		if (bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F') {
			return "GIF";
		} else if (bytes[1] == 'P' && bytes[2] == 'N' && bytes[3] == 'G') {
			return "PNG";
		} else if (bytes[6] == 'J' && bytes[7] == 'F' && bytes[8] == 'I'
				&& bytes[9] == 'F') {
			return "JPG";
		} else if (bytes[0] == 'B' && bytes[1] == 'M') {
			return "BMP";
		} else {
			return null;
		}
	}
	
	public static byte[] readBytes(InputStream in) throws IOException {  
        byte[] temp = new byte[in.available()];  
        byte[] result = new byte[0];  
        int size = 0;  
        while ((size = in.read(temp)) != -1) {  
            byte[] readBytes = new byte[size];  
            System.arraycopy(temp, 0, readBytes, 0, size);  
            result = mergeArray(result,readBytes);  
        }  
        return result;  
    }  
	
	public static byte[] mergeArray(byte[]... a) {  
        // 合并完之后数组的总长度  
        int index = 0;  
        int sum = 0;  
        for (int i = 0; i < a.length; i++) {  
            sum = sum + a[i].length;  
        }  
        byte[] result = new byte[sum];  
        for (int i = 0; i < a.length; i++) {  
            int lengthOne = a[i].length;  
            if(lengthOne==0){  
                continue;  
            }  
            // 拷贝数组  
            System.arraycopy(a[i], 0, result, index, lengthOne);  
            index = index + lengthOne;  
        }  
        return result;  
    }  
 

}

3、图像处理工具

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * 功能概要:图片处理工具类
 * 
 * @author linbingwen
 * @since 2016年3月30日
 */
public class ImageUtil {

	/**
	 * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
	 * 
	 * @author linbingwen
	 * @since 2016年3月30日
	 * @param imgFilePath 图片路径     
	 * @return base64编码的字符串
	 * @throws IOException 
	 */
	public static String imageToBase64(String imgFilePath) throws IOException {
		byte[] data = null;
		InputStream in = null;
		try {
			in = new FileInputStream(imgFilePath);
			data = new byte[in.available()];	// 读取图片字节数组
			in.read(data);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
		}
		BASE64Encoder encoder = new BASE64Encoder();// 对字节数组Base64编码
		return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	}

	/**
	 * 对字节数组字符串进行Base64解码并生成图片
	 * 
	 * @author linbingwen
	 * @since 2016年3月30日
	 * @param imgStr base64编码的数据
	 * @param imgFilePath 要保存的图片路径
	 * @param imgFileName 要保存的图片名
	 * @return
	 * @throws IOException 
	 */
	public static Boolean base64ToImage(String base64, String imgFilePath,String imgFileName) throws IOException {
		if (base64 == null) {
			return null;
		}
		BASE64Decoder decoder = new BASE64Decoder();
		OutputStream out = null;
		try {
			byte[] bytes = decoder.decodeBuffer(base64);// Base64解码
			for (int i = 0; i < bytes.length; ++i) {
				if (bytes[i] < 0) {// 调整异常数据
					bytes[i] += 256;
				}
			}
			InputStream input = new ByteArrayInputStream(bytes);
			out = new FileOutputStream(imgFilePath + imgFileName);// 生成jpeg图片
			out.write(bytes);
			out.flush();
			return true;
		} catch (Exception e) {
			return false;
		} finally {
			if (out != null) {
			    out.close();
			}
		}
	}
	
	/**
	 * 将base64编码的字符串转成InputStream
	 * @author linbingwen
	 * @since  2016年3月30日 
	 * @param base64
	 * @return InputStream
	 */
	public static InputStream base64ToInputStream(String base64) {
		if (base64 == null) {
			return null;
		}
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] bytes = decoder.decodeBuffer(base64);// Base64解码
			for (int i = 0; i < bytes.length; ++i) {
				if (bytes[i] < 0) {// 调整异常数据
					bytes[i] += 256;
				}
			}
			InputStream input = new ByteArrayInputStream(bytes);
			return input;
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 将网络图片进行Base64位编码
	 * @author linbingwen
	 * @since  2016年3月30日 
	 * @param imageUrl 图片的url路径,如http://.....xx.jpg
	 * @return 返回Base64编码过的字节数组字符串
	 * @throws IOException 
	 */
	public static String imageUrlToBase64(String imageUrl) throws IOException {
		ByteArrayOutputStream outputStream = null;
		try {
			URL  url = new URL(imageUrl);
			BufferedImage bufferedImage = ImageIO.read(url);
			outputStream = new ByteArrayOutputStream();
			ImageIO.write(bufferedImage, "jpg", outputStream);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
			   outputStream.close();
			}
		}
		BASE64Encoder encoder = new BASE64Encoder();// 对字节数组Base64编码
		return encoder.encode(outputStream.toByteArray());
	}
	
	/**
	 * 
	 * @author linbingwen
	 * @since  2016年4月13日 
	 * @param imageUrl 图片url
	 * @param imageType 图片格式 如 jpg/bmp/png
	 * @return
	 * @throws IOException
	 */
	public static byte[] imageUrlToBytes(String imageUrl,String imageType) throws IOException {
		ByteArrayOutputStream outputStream = null;
		byte[] bytes  = null;
		try {
			URL  url = new URL(imageUrl);
			BufferedImage bufferedImage = ImageIO.read(url);
			outputStream = new ByteArrayOutputStream();
			ImageIO.write(bufferedImage, imageType, outputStream);
			bytes = outputStream.toByteArray();
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
			   outputStream.close();
			}
		}
		return bytes;
	}
	
	public static void main(String[] args) throws IOException {
		String imgFilePath = "D:\\";
	    String  ttString = imageUrlToBase64("http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/25/CkvJolb5nIOAcHMZAADU5zn6AlI7014866");
	    base64ToImage(ttString, imgFilePath, "44.bmp");
	    
	    urlBase64ToFile("http://10.75.201.68:8888/cfile/file/image?imageId=group2/M00/05/97/CkvJo1dxbqKAEBqzAARHjfpHsPk7518600");
	}

	/**
	 * @author linbingwen
	 * @since  2016年6月28日 
	 * @param string    
	 */
	private static void urlBase64ToFile(String string) {
		
	}


}

4、JSON转换处理工具

/**
 * 功能概要:java与json转换工具类
 * 
 * @author linbingwen
 * @since  2016年4月20日 
 */
import java.text.SimpleDateFormat;   
import java.util.ArrayList;   
import java.util.Collection;   
import java.util.Date;   
import java.util.HashMap;   
import java.util.Iterator;   
import java.util.List;   
import java.util.Map;   
  
import net.sf.ezmorph.MorpherRegistry;   
import net.sf.ezmorph.object.DateMorpher;   
import net.sf.json.JSONArray;   
import net.sf.json.JSONObject;   
import net.sf.json.JsonConfig;   
import net.sf.json.processors.JsonValueProcessor;   
import net.sf.json.util.JSONUtils;   
import net.sf.json.xml.XMLSerializer;   
  
public class JsonUtil {   
	
	private static String YYYY_MM_DD = "yyyy-MM-dd";
	private static String YYYY_MM_DD_HH_MM_ss = "yyyy-MM-dd HH:mm:ss";
	private static String HH_MM_ss = "HH-mm-ss";
	private static String YYYYMMDD = "yyyyMMdd";
	private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	private static String HHMMss = "HHmmss";
  
    /**  
     * 设置日期转换格式  
     */  
    static {   
        //注册器   
        MorpherRegistry mr = JSONUtils.getMorpherRegistry();   
  
        //可转换的日期格式,即Json串中可以出现以下格式的日期与时间   
        DateMorpher dm = new DateMorpher(new String[] { YYYY_MM_DD,   
                YYYY_MM_DD_HH_MM_ss, HH_MM_ss, YYYYMMDD,   
                YYYYMMDDHHMMSS, HHMMss});   
        mr.registerMorpher(dm);   
    }   
  
    /**  
    * 从json串转换成实体对象  
    * @param jsonObjStr e.g. {'name':'get','dateAttr':'2009-11-12'}  
    * @param clazz Person.class  
    * @return  
    */  
    public static Object getDtoFromJsonObjStr(String jsonObjStr, Class clazz) {     
        return JSONObject.toBean(JSONObject.fromObject(jsonObjStr), clazz);   
    }   
  
    /**  
    * 从json串转换成实体对象,并且实体集合属性存有另外实体Bean  
    * @param jsonObjStr e.g. {'data':[{'name':'get'},{'name':'set'}]}  
    * @param clazz e.g. MyBean.class  
    * @param classMap e.g. classMap.put("data", Person.class)  
    * @return Object  
    */  
    public static Object getDtoFromJsonObjStr(String jsonObjStr, Class clazz, Map classMap) {   
        return JSONObject.toBean(JSONObject.fromObject(jsonObjStr), clazz, classMap);   
    }   
  
    /**  
    * 把一个json数组串转换成普通数组  
    * @param jsonArrStr  e.g. ['get',1,true,null]  
    * @return Object[]  
    */  
    public static Object[] getArrFromJsonArrStr(String jsonArrStr) {   
        return JSONArray.fromObject(jsonArrStr).toArray();   
    }   
  
    /**  
    * 把一个json数组串转换成实体数组  
    * @param jsonArrStr e.g. [{'name':'get'},{'name':'set'}]  
    * @param clazz e.g. Person.class  
    * @return Object[]  
    */  
    public static Object[] getDtoArrFromJsonArrStr(String jsonArrStr, Class clazz) {   
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);   
        Object[] objArr = new Object[jsonArr.size()];   
        for (int i = 0; i < jsonArr.size(); i++) {   
            objArr[i] = JSONObject.toBean(jsonArr.getJSONObject(i), clazz);   
        }   
        return objArr;   
    }   
  
    /**  
    * 把一个json数组串转换成实体数组,且数组元素的属性含有另外实例Bean  
    * @param jsonArrStr e.g. [{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]  
    * @param clazz e.g. MyBean.class  
    * @param classMap e.g. classMap.put("data", Person.class)  
    * @return Object[]  
    */  
    public static Object[] getDtoArrFromJsonArrStr(String jsonArrStr, Class clazz,   
            Map classMap) {   
        JSONArray array = JSONArray.fromObject(jsonArrStr);   
        Object[] obj = new Object[array.size()];   
        for (int i = 0; i < array.size(); i++) {   
            JSONObject jsonObject = array.getJSONObject(i);   
            obj[i] = JSONObject.toBean(jsonObject, clazz, classMap);   
        }   
        return obj;   
    }   
  
    /**  
    * 把一个json数组串转换成存放普通类型元素的集合  
    * @param jsonArrStr  e.g. ['get',1,true,null]  
    * @return List  
    */  
    public static List getListFromJsonArrStr(String jsonArrStr) {   
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);   
        List list = new ArrayList();   
        for (int i = 0; i < jsonArr.size(); i++) {   
            list.add(jsonArr.get(i));   
        }   
        return list;   
    }   
  
    /**  
    * 把一个json数组串转换成集合,且集合里存放的为实例Bean  
    * @param jsonArrStr e.g. [{'name':'get'},{'name':'set'}]  
    * @param clazz  
    * @return List  
    */  
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz) {   
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);   
        List list = new ArrayList();   
        for (int i = 0; i < jsonArr.size(); i++) {   
            list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz));   
        }   
        return list;   
    }   
  
    /**  
    * 把一个json数组串转换成集合,且集合里的对象的属性含有另外实例Bean  
    * @param jsonArrStr e.g. [{'data':[{'name':'get'}]},{'data':[{'name':'set'}]}]  
    * @param clazz e.g. MyBean.class  
    * @param classMap e.g. classMap.put("data", Person.class)  
    * @return List  
    */  
    public static List getListFromJsonArrStr(String jsonArrStr, Class clazz, Map classMap) {   
        JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);   
        List list = new ArrayList();   
        for (int i = 0; i < jsonArr.size(); i++) {   
            list.add(JSONObject.toBean(jsonArr.getJSONObject(i), clazz, classMap));   
        }   
        return list;   
    }   
  
    /**  
    * 把json对象串转换成map对象  
    * @param jsonObjStr e.g. {'name':'get','int':1,'double',1.1,'null':null}  
    * @return Map  
    */  
    public static Map getMapFromJsonObjStr(String jsonObjStr) {   
        JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);   
  
        Map map = new HashMap();   
        for (Iterator iter = jsonObject.keys(); iter.hasNext();) {   
            String key = (String) iter.next();   
            map.put(key, jsonObject.get(key));   
        }   
        return map;   
    }   
  
    /**  
    * 把json对象串转换成map对象,且map对象里存放的为其他实体Bean  
    * @param jsonObjStr e.g. {'data1':{'name':'get'},'data2':{'name':'set'}}  
    * @param clazz e.g. Person.class  
    * @return Map  
    */  
    public static Map getMapFromJsonObjStr(String jsonObjStr, Class clazz) {   
        JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);   
  
        Map map = new HashMap();   
        for (Iterator iter = jsonObject.keys(); iter.hasNext();) {   
            String key = (String) iter.next();   
            map.put(key, JSONObject.toBean(jsonObject.getJSONObject(key), clazz));   
        }   
        return map;   
    }   
  
    /**  
     * 把json对象串转换成map对象,且map对象里存放的其他实体Bean还含有另外实体Bean  
     * @param jsonObjStr e.g. {'mybean':{'data':[{'name':'get'}]}}  
     * @param clazz e.g. MyBean.class  
     * @param classMap  e.g. classMap.put("data", Person.class)  
     * @return Map  
     */  
    public static Map getMapFromJsonObjStr(String jsonObjStr, Class clazz, Map classMap) {   
        JSONObject jsonObject = JSONObject.fromObject(jsonObjStr);   
  
        Map map = new HashMap();   
        for (Iterator iter = jsonObject.keys(); iter.hasNext();) {   
            String key = (String) iter.next();   
            map.put(key, JSONObject   
                    .toBean(jsonObject.getJSONObject(key), clazz, classMap));   
        }   
        return map;   
    }   
  
    /**  
     * 把实体Bean、Map对象、数组、列表集合转换成Json串  
     * @param obj   
     * @return  
     * @throws Exception String  
     */  
    public static String getJsonStr(Object obj) {   
        String jsonStr = null;   
        //Json配置       
//        JsonConfig jsonCfg = new JsonConfig();   
//  
//        //注册日期处理器   
//        jsonCfg.registerJsonValueProcessor(java.util.Date.class,   
//                new JsonDateValueProcessor(YYYY_MM_DD_HH_MM_ss));   
        if (obj == null) {   
            return "{}";   
        }   
  
        if (obj instanceof Collection || obj instanceof Object[]) {   
            jsonStr = JSONArray.fromObject(obj).toString();   
        } else {   
            jsonStr = JSONObject.fromObject(obj).toString();   
        }   
  
        return jsonStr;   
    }   
  
    /**  
     * 把json串、数组、集合(collection map)、实体Bean转换成XML  
     * XMLSerializer API:  
     * http://json-lib.sourceforge.net/apidocs/net/sf/json/xml/XMLSerializer.html  
     * 具体实例请参考:  
     * http://json-lib.sourceforge.net/xref-test/net/sf/json/xml/TestXMLSerializer_writes.html  
     * http://json-lib.sourceforge.net/xref-test/net/sf/json/xml/TestXMLSerializer_writes.html  
     * @param obj   
     * @return  
     * @throws Exception String  
     */  
    public static String getXMLFromObj(Object obj) {   
        XMLSerializer xmlSerial = new XMLSerializer();   
  
        //Json配置       
        JsonConfig jsonCfg = new JsonConfig();   
  
        //注册日期处理器   
        jsonCfg.registerJsonValueProcessor(java.util.Date.class,   
                new JsonDateValueProcessor(YYYY_MM_DD_HH_MM_ss));   
  
        if ((String.class.isInstance(obj) && String.valueOf(obj).startsWith("["))   
                || obj.getClass().isArray() || Collection.class.isInstance(obj)) {   
            JSONArray jsonArr = JSONArray.fromObject(obj, jsonCfg);   
            return xmlSerial.write(jsonArr);   
        } else {   
            JSONObject jsonObj = JSONObject.fromObject(obj, jsonCfg);   
            return xmlSerial.write(jsonObj);   
        }   
    }   
  
    /**  
     * 从XML转json串  
     * @param xml  
     * @return String  
     */  
    public static String getJsonStrFromXML(String xml) {   
        XMLSerializer xmlSerial = new XMLSerializer();   
        return String.valueOf(xmlSerial.read(xml));   
    }   
  
}   
  
/**
 * 
 * 功能概要:json日期值处理器实现    
 * 
 * @author linbingwen
 * @since  2016年4月20日
 */
class JsonDateValueProcessor implements JsonValueProcessor {   
  
    private String format ="yyyy-MM-dd HH-mm-ss";   
  
    public JsonDateValueProcessor() {   
  
    }   
  
    public JsonDateValueProcessor(String format) {   
        this.format = format;   
    }   
  
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {   
        return process(value, jsonConfig);   
    }   
  
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {   
        return process(value, jsonConfig);   
    }   
  
    private Object process(Object value, JsonConfig jsonConfig) {   
        if (value instanceof Date) {   
            String str = new SimpleDateFormat(format).format((Date) value);   
            return str;   
        }   
        return value == null ? null : value.toString();   
    }   
  
    public String getFormat() {   
        return format;   
    }   
  
    public void setFormat(String format) {   
        this.format = format;   
    }   
  
} 

5、邮件发送处理工具

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MailSendUtil {

    private static Logger logger = LoggerFactory.getLogger(MailSendUtil.class);

    private MimeMessage mimeMsg; // MIME邮件对象

    private Session session; // 邮件会话对象

    private Properties props; // 系统属性

    private boolean needAuth = false; // smtp是否需要认证

    private String username; // smtp认证用户名

    private String password; // smtp认证用户密码

    private Multipart mp; // 含标题,邮件内容,附件

    /**
     * Constructor
     * 
     * @param smtp
     *            邮件发送服务器
     */
    public MailSendUtil(String smtp) {
        setSmtpHost(smtp);
        createMimeMessage();
    }

    /**
     * 设置邮件发送服务器
     * 
     * @param hostName
     *            String
     */
    public void setSmtpHost(String hostName) {
        logger.info("设置系统属性:mail.smtp.host = " + hostName);
        if (props == null)
            props = System.getProperties(); // 获得系统属性对象
        props.put("mail.smtp.host", hostName); // 设置SMTP主机
    }

    /**
     * 创建MIME邮件对象
     * 
     * @return
     */
    public boolean createMimeMessage() {
        try {
            logger.info("准备获取邮件会话对象!");

            session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });// 获得邮件会话对象
        }
        catch (Exception e) {
            logger.error("获取邮件会话对象时发生错误!" + e);
            return false;
        }

        logger.info("准备创建MIME邮件对象!");
        try {
            mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
            mp = new MimeMultipart();

            return true;
        }
        catch (Exception e) {
            logger.error("创建MIME邮件对象失败!" + e);
            return false;
        }
    }

    /**
     * 设置SMTP是否需要验证
     * 
     * @param need
     */
    public void setNeedAuth(boolean need) {
        logger.info("设置smtp身份认证:mail.smtp.auth = " + need);
        if (props == null)
            props = System.getProperties();
        if (need) {
            props.put("mail.smtp.auth", "true");
        }
        else {
            props.put("mail.smtp.auth", "false");
        }
    }

    /**
     * 设置用户名和密码
     * 
     * @param name
     * @param pass
     */
    public void setNamePass(String name, String pass) {
        username = name;
        password = pass;
    }

    /**
     * 设置邮件主题
     * 
     * @param mailSubject
     * @return
     */
    public boolean setSubject(String mailSubject) {
        logger.info("设置邮件主题!");
        try {
            mimeMsg.setSubject(mailSubject);
            return true;
        }
        catch (Exception e) {
            logger.error("设置邮件主题发生错误!");
            return false;
        }
    }

    /**
     * 设置邮件正文
     * 
     * @param mailBody
     *            String
     */
    public boolean setBody(String mailBody) {
        try {
            BodyPart bp = new MimeBodyPart();
            bp.setContent("" + mailBody, "text/html;charset=GBK");
            mp.addBodyPart(bp);
            return true;
        }
        catch (Exception e) {
            logger.error("设置邮件正文时发生错误!" + e);
            return false;
        }
    }

    /**
     * 添加附件
     * 
     * @param filename
     *            String
     */
    public boolean addFileAffix(String filename) {
        if (filename == null) {
            return true;
        }

        logger.info("增加邮件附件:" + filename);
        try {
            BodyPart bp = new MimeBodyPart();
            FileDataSource fileds = new FileDataSource(filename);
            bp.setDataHandler(new DataHandler(fileds));
            bp.setFileName(MimeUtility.encodeText(fileds.getName()));

            mp.addBodyPart(bp);

            return true;
        }
        catch (Exception e) {
            logger.error("增加邮件附件:" + filename + "发生错误!" + e);
            return false;
        }
    }

    /**
     * 设置发信人
     * 
     * @param from
     *            String
     */
    public boolean setFrom(String from) {
        logger.info("设置发信人!");
        try {
            mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 设置收信人
     * 
     * @param to
     *            String
     */
    public boolean setTo(String to) {
        logger.info("设置收件人:" + to);
        if (to == null)
            return false;
        try {
            mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 设置抄送人
     * 
     * @param copyto
     *            String
     */
    public boolean setCopyTo(String copyto) {
        if (copyto == null)
            return false;
        try {
            mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress.parse(copyto));

            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    /**
     * 发送邮件
     */
    public boolean sendOut() {
        try {
            mimeMsg.setContent(mp);
            mimeMsg.saveChanges();
            logger.info("正在发送邮件....");
            Transport transport = session.getTransport("smtp");
            transport.connect((String) props.get("mail.smtp.host"), username, password);
            transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
//            transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
//            Transport.send(mimeMsg);

            logger.info("发送邮件成功!");
            transport.close();

            return true;
        }
        catch (Exception e) {
            logger.error("邮件发送失败!" + e);
            return false;
        }
    }

    /**
     * 调用sendOut方法完成邮件发送
     * 
     * @param smtp
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param username
     * @param password
     * @return boolean
     */
    public static boolean send(String smtp, String from, String to, String subject, String content, String username, String password) {
        MailSendUtil theMail = new MailSendUtil(smtp);
        theMail.setNeedAuth(true); // 需要验证

        if (!theMail.setSubject(subject))
            return false;
        if (!theMail.setBody(content))
            return false;
        if (!theMail.setTo(to))
            return false;
        if (!theMail.setFrom(from))
            return false;
        theMail.setNamePass(username, password);

        if (!theMail.sendOut())
            return false;
        return true;
    }

    /**
     * 调用sendOut方法完成邮件发送,带抄送
     * 
     * @param smtp
     * @param from
     * @param to
     * @param copyto
     * @param subject
     * @param content
     * @param username
     * @param password
     * @return boolean
     */
    public static boolean sendAndCc(String smtp, String from, String to, String copyto, String subject, String content, String username, String password) {
        MailSendUtil theMail = new MailSendUtil(smtp);
        theMail.setNeedAuth(true); // 需要验证

        if (!theMail.setSubject(subject))
            return false;
        if (!theMail.setBody(content))
            return false;
        if (!theMail.setTo(to))
            return false;
        if (!theMail.setCopyTo(copyto))
            return false;
        if (!theMail.setFrom(from))
            return false;
        theMail.setNamePass(username, password);

        if (!theMail.sendOut())
            return false;
        return true;
    }

    /**
     * 调用sendOut方法完成邮件发送,带附件
     * 
     * @param smtp
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param username
     * @param password
     * @param filename
     *            附件路径
     * @return
     */
    public static boolean send(String smtp, String from, String to, String subject, String content, String username, String password, String filename) {
        MailSendUtil theMail = new MailSendUtil(smtp);
        theMail.setNeedAuth(true); // 需要验证
        logger.info("发送邮件至:{} " + to);
       
        if (!theMail.setSubject(subject))
            return false;
        if (!theMail.setBody(content))
            return false;
        if (!theMail.addFileAffix(filename))
            return false;
        if (!theMail.setTo(to))
            return false;
        if (!theMail.setFrom(from))
            return false;
        theMail.setNamePass(username, password);

        if (!theMail.sendOut())
            return false;
        return true;
    }

    /**
     * 调用sendOut方法完成邮件发送,带附件和抄送
     * 
     * @param smtp
     * @param from
     * @param to
     * @param copyto
     * @param subject
     * @param content
     * @param username
     * @param password
     * @param filename
     * @return
     */
    public static boolean sendAndCc(String smtp, String from, String to, String copyto, String subject, String content, String username, String password, String filename) {
        MailSendUtil theMail = new MailSendUtil(smtp);
        theMail.setNeedAuth(true); // 需要验证

        if (!theMail.setSubject(subject))
            return false;
        if (!theMail.setBody(content))
            return false;
        if (!theMail.addFileAffix(filename))
            return false;
        if (!theMail.setTo(to))
            return false;
        if (!theMail.setCopyTo(copyto))
            return false;
        if (!theMail.setFrom(from))
            return false;
        theMail.setNamePass(username, password);

        if (!theMail.sendOut())
            return false;
        return true;
    }

    public static void main(String[] args) {
        String smtp = "10.75.210.10";
        String from = "test1@xxxxx";
        String to = "liuqiuyun@xxxx";
        String subject = "管理系统";
        String content = "邮件内容";
        String username = "test1";
        String password = "Password1";
        String filename = "D:\\file\\ces\\INT_MMS_SETTLE_20150211_0001.DATA";
        try {
            MailSendUtil.send(smtp, from, to, subject, content, username, password, filename);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

6、SFTP处理工具

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.lz.lsf.exception.BusinessException;

/**
 * 
 * 功能概要:SFTP客户端
 * 
 * @author linbingwen
 * @since  2015年8月5日
 */
public class SftpClient {

    private String m_host = "127.0.0.1";

    private int m_port = 22;

    private String m_username = "ctsUser";

    private String m_password = "ctsUser";

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private Channel m_channel = null;

    public SftpClient(String host, int Port, String userName, String password) {
        this.m_host = host;
        this.m_port = Port;
        this.m_username = userName;
        this.m_password = password;
    }

    public void reConnect() {
        try {
            this.connect();
        }
        catch (Exception e) {
            logger.warn("m_channel disconnect fail!", e);
        }
    }

    public void connect() {
        JSch jsch = new JSch();
        try {
            jsch.getSession(m_username, m_host, m_port);
            Session sshSession = jsch.getSession(m_username, m_host, m_port);
            logger.info("username:" + m_username + ", host:" + m_host + ",port:" + m_port);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(config);
            logger.debug("StrictHostKeyChecking", "no");
            sshSession.setPassword(m_password);
            sshSession.connect();
            logger.debug("Session connected.");
            m_channel = sshSession.openChannel("sftp");
            logger.debug("Opening Channel.");
            m_channel.connect();
            logger.info("Connected to {} success! ", m_host);
        }
        catch (JSchException e) {
            logger.error("connected to " + m_host + "Fail! ");
            throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL, e, "connected to " + m_host + "Fail! ");
        }
    }

    public void disConnect() {
        try {
            if (m_channel == null)
                return;

            synchronized (m_channel) {
                if (m_channel.getSession().isConnected())
                    m_channel.getSession().disconnect();
            }

            m_channel.disconnect();

        }
        catch (JSchException e) {
            logger.warn("m_channel disconnect fail!", e);
        }
        finally {
            if (m_channel != null)
                m_channel = null;
        }
    }

    public boolean isTryConnect() {
        int tryConnectCount = 0;
        try {
            while (true) {
                tryConnectCount++;
                if (m_channel.getSession().isConnected())
                    return true;
                else {
                    if (tryConnectCount >= 3)
                        return false;
                    else {
                        this.reConnect();
                    }
                }
            }
        }
        catch (JSchException e) {
            logger.warn("m_channel isConnected fail!", e);
            return false;
        }
    }

    /**
     * 上传文件
     * 
     * @param directoryName
     *            上传的目录
     * @param uploadFileName
     *            要上传的文件
     * @param sftp
     * @throws SftpException
     * @throws FileNotFoundException
     * @throws JSchException
     */
    public void upload(String remotePathDirName, String uploadFileName) {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
           throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        try {
            sftp.cd(remotePathDirName);
            File uploadFile = new File(uploadFileName);
            sftp.put(new FileInputStream(uploadFile), uploadFile.getName());
            logger.debug("Upload file:{} to remote dir:{}", uploadFileName, remotePathDirName);
        }
        catch (FileNotFoundException e) {
            logger.error("download remote path({})FileNotFound{}", remotePathDirName, uploadFileName);
            throw new BusinessException(CommonErrorCode.NOT_EXISTS_PATH_SFTP_REMOTE, e, "FileNotFound:" + uploadFileName);
        }
        catch (SftpException e) {
            logger.error("download remote path({}) not exists!{}", remotePathDirName, e);
            throw new BusinessException(CommonErrorCode.NOT_EXISTS_PATH_SFTP_REMOTE, e, "remote path:" + remotePathDirName);
        }
    }

    public void uploadBatch(String directoryName, List<String> fileNameList) {
        for (String fileName : fileNameList) {
            this.upload(directoryName, fileName);
        }
    }

    /**
     * 下载文件
     * 
     * @param directoryName
     *            下载目录
     * @param downloadFileName
     *            下载的文件
     * @param saveFileName
     *            存在本地的路径
     * @param sftp
     * @throws SftpException
     * @throws FileNotFoundException
     * @throws JSchException
     */
    public void download(String remotePathDirName, String localPathDirName, String downloadFileName) {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
          //  throw new BusinessException(ActErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        try {
            sftp.cd(remotePathDirName);
            File saveFile = new File(localPathDirName + "//" + downloadFileName);
            sftp.get(downloadFileName, new FileOutputStream(saveFile));
            logger.debug("Download file:{} save as {}", downloadFileName, localPathDirName + "//" + downloadFileName);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            logger.error("download remote path{}//{}", remotePathDirName, downloadFileName);
            throw new BusinessException(CommonErrorCode.NOT_FINDFIELS_REMOTE_PATH, e, "FileNotFound:" + downloadFileName);
        }
        catch (SftpException e) {
            logger.error("download remote path({}) fail!{}", remotePathDirName + downloadFileName, e);
            throw new BusinessException(CommonErrorCode.NOT_EXISTS_PATH_SFTP_REMOTE, e, "remote path:" + remotePathDirName);
        }
    }

    public void downloadBatch(String directoryName, String localPathDirName, List<String> downloadFileNameList) {
        for (String fileName : downloadFileNameList) {
            this.download(directoryName, localPathDirName, fileName);
        }
    }

    public boolean isFileExists(String remotePathDirName) {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
            throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        try {
            Vector<LsEntry> filesName = sftp.ls(remotePathDirName);
            return filesName.size() > 0;
        }
        catch (SftpException e) {
            logger.warn("download remote path({}) not exists!{}", remotePathDirName, e);
            return false;
        }
    }

    /**
     * 删除文件
     * 
     * @param directory
     *            要删除文件所在目录
     * @param deleteFileName
     *            要删除的文件
     * @param sftp
     * @throws SftpException
     * @throws JSchException
     */
    public void delete(String directory, String deleteFileName) throws SftpException, JSchException {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
           throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        sftp.cd(directory);
        sftp.rm(deleteFileName);
        logger.info("Delete file:{} from remote dir:{}", deleteFileName, directory);
    }

    /**
     * 列出目录下的文件
     * 
     * @param directoryName
     *            要列出的目录
     * @param sftp
     * @return
     * @throws SftpException
     * @throws JSchException
     */
    @SuppressWarnings("unchecked")
    public Vector<LsEntry> listFiles(String directoryName) throws SftpException, JSchException {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
           throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        Vector<LsEntry> filesName = sftp.ls(directoryName);
        return filesName;
    }

    /**
     * 列出目录下符合要求的文件
     * 
     * @param directoryName
     *            要列出的目录
     * @param reg
     *            文件名前缀
     * @param postfix
     *            文件名后缀(格式)
     * @return
     * @throws SftpException
     * @throws JSchException
     */
    @SuppressWarnings("unchecked")
    public Vector<LsEntry> listFiles(String remotePathDirName, String reg, String postfix) {
        ChannelSftp sftp = (ChannelSftp) m_channel;
        if (!this.isTryConnect()) {
            logger.error("尝试连接SFTP服务器失败!");
           throw new BusinessException(CommonErrorCode.ERROR_CONNECT_SFTP_FAIL);
        }
        Vector<LsEntry> filesName;
        try {
            filesName = sftp.ls(remotePathDirName);
            Vector<LsEntry> filterFilesName = new Vector<LsEntry>();
            for (LsEntry lsEntry : filesName) {
                if (lsEntry.getFilename().indexOf(reg) > -1 && lsEntry.getFilename().endsWith(postfix)) {
                    filterFilesName.add(lsEntry);
                }
            }
            return filterFilesName;
        }
        catch (SftpException e) {
            logger.error("download remote path({}) not exists!{}", remotePathDirName, e);
            throw new BusinessException(CommonErrorCode.NOT_EXISTS_PATH_SFTP_REMOTE, e, "remote path" + remotePathDirName);
        }
	
    }

}

7、class工具

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import com.lz.lsf.util.StringUtil;

public class ClassUtil {

    static Set<Class> privateTypes = new HashSet<Class>();
    static {
        privateTypes.add(int.class);
        privateTypes.add(double.class);
        privateTypes.add(long.class);
        privateTypes.add(float.class);
        privateTypes.add(boolean.class);
        privateTypes.add(Integer.class);
        privateTypes.add(Double.class);
        privateTypes.add(Long.class);
        privateTypes.add(Float.class);
        privateTypes.add(String.class);
        privateTypes.add(Date.class);
        privateTypes.add(Boolean.class);
    }

 
    public static Class<?> getFieldGenricType(Field field, int index) {
        String signature = field.toGenericString();
        return getGenericeType(signature, index);
    }

    private static Class<?> getGenericeType(String signature, int index) {
        String genericStr = signature.substring(signature.indexOf("<") + 1, signature.indexOf(">"));
        String[] types = genericStr.split(",");
        if (types.length > 0 && types.length > index) {
            try {
                return Class.forName(types[index]);
            }
            catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            return null;
        }
    }

    /**
     * 通过反射, 获得定义Class时声明的父类的泛型参数的类型. 如无法找到, 返回Object.class.
     * 
     * @param clazz
     *            clazz The class to introspect
     * @param index
     *            the Index of the generic ddeclaration,start from 0.
     * @return the index generic declaration, or Object.class if cannot be determined
     */
    @SuppressWarnings("unchecked")
    public static Class<?> getSuperClassGenricType(final Class clazz, final int index) {
        Class<?> ret = null;
        // 返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
        Type genType = clazz.getGenericSuperclass();
        ret = getGenericType(genType, index);

        if (ret == null) {
            for (Type t : clazz.getGenericInterfaces()) {
                ret = getGenericType(t, index);
                if (ret != null) {
                    break;
                }
            }
        }

        return ret;
    }

    private static Class<?> getGenericType(Type type, int index) {
        if (!(type instanceof ParameterizedType)) {
            return null;
        }

        // 返回表示此类型实际类型参数的 Type 对象的数组。
        Type[] params = ((ParameterizedType) type).getActualTypeArguments();

        if (index >= params.length || index < 0) {
            return null;
        }
        if (!(params[index] instanceof Class)) {
            return null;
        }

        return (Class) params[index];
    }

    public static String getSetterMethod(String attrName) {
        String fst = attrName.substring(0, 1).toUpperCase();
        attrName = fst + attrName.substring(1);
        return "set" + attrName;
    }

    public static void setObjectValue(Object obj, String fieldName, Object value) {
        try {
            String methodName = getSetterMethod(fieldName);
            Method method = findMethod(obj.getClass(), methodName, String.class);
            if (method != null) {
                method.invoke(obj, value);
            }
            else {
                Field field = obj.getClass().getDeclaredField(fieldName);
                if (field != null) {
                    field.setAccessible(true);
                    field.set(obj, value);
                }
                else {
                    throw new RuntimeException("no field or set method found for field:" + fieldName);
                }
            }
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean isPriovateType(Class type) {
        return privateTypes.contains(type);
    }

    public static Object getPrivateTypeValue(Class type, String value) {
        if (String.class == type) {
            return value;
        }
        else if (int.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Integer.parseInt(value);
        }
        else if (double.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Double.parseDouble(value);
        }
        else if (long.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Long.parseLong(value);
        }
        else if (float.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Float.parseFloat(value);
        }
        else if (Integer.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Integer.valueOf(value);
        }
        else if (Double.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Double.valueOf(value);
        }
        else if (Long.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Long.valueOf(value);
        }
        else if (Float.class == type) {
            return StringUtil.isEmpty(value) ? 0 : Float.valueOf(value);
        }
        else if (BigDecimal.class == type) {
            return StringUtil.isEmpty(value) ? BigDecimal.ZERO :  BigDecimal.valueOf(Double.valueOf(value));
        }
        else if (boolean.class == type || Boolean.class == type) {
            return StringUtil.isEmpty(value) ? false : Boolean.valueOf(value);
        }
        else {
            return null;
        }
    }
    
    public static void main(String[] args) {
		System.out.println( Boolean.valueOf("true"));
		System.out.println( Boolean.valueOf("false"));
		String[] sp = "|1|2|||| ".split("\\|");
		System.out.println(sp);
		System.out.println("|1|2||||".endsWith("|"));
	}

    public static Method findMethod(Class<?> clazz, String methodName, Class<?> paramType) {
        Method ret = null;
        try {
            ret = clazz.getMethod(methodName, paramType);
        }
        catch (Exception e) {
            if (paramType.getSuperclass() != null) {
                ret = findMethod(clazz, methodName, paramType.getSuperclass());
            }
            if (ret == null) {
                for (Class _clazz : paramType.getInterfaces()) {
                    ret = findMethod(clazz, methodName, _clazz);
                    if (ret != null) {
                        break;
                    }
                }
            }
        }
        return ret;
    }

    @SuppressWarnings("unchecked")
    public static <T> T cloneInstance(T obj) {
        T ret;
        try {
            ret = (T) BeanUtils.cloneBean(obj);
        }
        catch (Exception e) {
            throw new RuntimeException("clone instance failed!", e);
        }

        return ret;
    }
}

8、GET/POST请求工具
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {

	static CookieStore cookieStore = null;

	private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

	public static void main(String[] args) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("pageIndex", "1");
		map.put("pageSize", "20");
		String result = HttpClientUtil.post("http://xxxxx/xxx/operator/menu/findByPage.json", map);
		logger.info("post result:" + result);

		map = new HashMap<String, String>();
		map.put("pageNumber", "1");
		map.put("pageSize", "20");
		result = HttpClientUtil.get("http://xxx/ftc-ump-mid/xxx/dict/condition.json", map);
		logger.info("get result:" + result);

	}

	@Test
	public void postTest() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("pageIndex", "1");
		map.put("pageSize", "20");
		String result = HttpClientUtil.post("http:/xxxxx/findByPage.json", map);
		logger.info("result:" + result);
	}

	@Test
	public void getTest() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("pageNumber", "1");
		map.put("pageSize", "20");
		String result = HttpClientUtil.get("http://xxxxor/dict/condition.json", map);
		logger.info("result:" + result);
	}

	/**
	 * 获取cookie的内容
	 * 
	 * @param ck
	 * @param name
	 * @return
	 */
	public static String retriveCkValue(String ck, String name) {
		if (StringUtils.isBlank(ck) || StringUtils.isBlank(name)) {
			return "";
		}

		final String delimChar = name + "=";
		int delimBegin = ck.indexOf(delimChar);
		if (delimBegin < 0) {
			return "";
		}

		String val = null;
		int delimEnd = ck.indexOf(';', delimBegin);
		if (delimEnd < 0) {
			val = ck.substring(delimBegin + delimChar.length()).trim();
		} else {
			val = ck.substring(delimBegin + delimChar.length(), delimEnd).trim();
		}
		int idx = val.indexOf('?');
		if (idx > 0) {
			val = val.substring(0, idx);
		}

		return val;
	}

	/**
	 * 将cookie保存到静态变量中供后续调用
	 * 
	 * @param httpResponse
	 */
	public static void setCookieStore(HttpResponse httpResponse) {
		logger.info("-------setCookieStore---------");

		if (httpResponse.getFirstHeader("Set-Cookie") != null) {
			cookieStore = new BasicCookieStore();
			org.apache.http.Header[] cookies = httpResponse.getHeaders("Set-Cookie");
			// Expires=Fri, 14-Apr-2017 09:42:26 GMT;

			for (int j = 0; j < cookies.length; j++) {
				String content = cookies[j].getValue();
				String cookName = content.substring(0, content.indexOf("="));

				String cookNameContent = retriveCkValue(content, cookName);
				String domain = retriveCkValue(content, "Domain");
				String path = retriveCkValue(content, "Path");

				String time = retriveCkValue(content, "Expires");
				Date expires = new Date(time);

				BasicClientCookie cookie = new BasicClientCookie(cookName, cookNameContent);
				cookie.setDomain(domain);
				cookie.setPath(path);
				cookie.setExpiryDate(expires);

				cookieStore.addCookie(cookie);
				logger.info(cookName + ":{},domain:{},path:{},expires", cookNameContent, domain, path, expires);

			}
		}
	}

	/**
	 * 模拟登陆
	 * 
	 * @param client
	 * @return
	 */
	private static String login(CloseableHttpClient client) {
		String path = "http://xxxxxx/operator.json";
		Map<String, String> params = new HashMap<String, String>();
		params.put("userId", "ITADMIN2");
		params.put("password", "123456");
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		if (params != null && !params.isEmpty()) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		HttpResponse httpResponse = null;
		try {
			// 实现将请求的参数封装到表单中,即请求体中
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);
			// 使用post方式提交数据
			HttpPost httpPost = new HttpPost(path);
			int connectionTimeout = 15000;
			int soTimeout = 15000;
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout).build();
			httpPost.setConfig(requestConfig);
			httpPost.setEntity(entity);

			httpResponse = client.execute(httpPost);
			// 获取服务器端返回的状态码和输入流,将输入流转换成字符串
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				// setCookieStore(httpResponse); // 设置cookie
				return EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpResponse != null) {
				try {
					EntityUtils.consume(httpResponse.getEntity());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return "";
	}

	/**
	 * 模拟get
	 * 
	 * @param path
	 * @param params
	 * @param encode
	 * @return
	 */
	public static String get(String path, Map<String, String> params) {
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		if (params != null && !params.isEmpty()) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		HttpResponse httpResponse = null;
		CloseableHttpClient client = null;
		try {
			// 实现将请求的参数封装到表单中,即请求体中
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, Consts.UTF_8);
			// 转换为键值对
			String str = EntityUtils.toString(entity);

			// 使用get方式提交数据
			HttpGet httpGet = new HttpGet(path + "?" + str);
			int connectionTimeout = 15000;
			int soTimeout = 15000;
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout).build();
			httpGet.setConfig(requestConfig);
			// 执行get请求,并获取服务器端的响应HttpResponse
			client = HttpClients.createDefault();

			// if (cookieStore != null) {
			// client =
			// HttpClients.custom().setDefaultCookieStore(cookieStore).build();
			// } else {
			// client = HttpClients.createDefault();
			// login(client);
			// }
			login(client);

			httpResponse = client.execute(httpGet);
			// 获取服务器端返回的状态码和输入流,将输入流转换成字符串
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpResponse != null) {
				try {
					EntityUtils.consume(httpResponse.getEntity());
				} catch (IOException e) {
					logger.error("", e);
				}
			}
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					logger.error("", e);
				}
			}
		}

		return "";
	}

	/**
	 * 模拟post
	 * 
	 * @param path
	 * @param params
	 * @param encode
	 * @return
	 */
	public static String post(String path, Map<String, String> params) {
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		if (params != null && !params.isEmpty()) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
		}
		HttpResponse httpResponse = null;
		CloseableHttpClient client = null;
		try {
			// 实现将请求的参数封装到表单中,即请求体中
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
			// 使用post方式提交数据
			HttpPost httpPost = new HttpPost(path);
			int connectionTimeout = 15000;
			int soTimeout = 15000;
			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(soTimeout).build();
			httpPost.setConfig(requestConfig);
			httpPost.setEntity(entity);
			// 执行post请求,并获取服务器端的响应HttpResponse
			HttpClients.createDefault();
			login(client);

			// if (cookieStore != null) {
			// client =
			// HttpClients.custom().setDefaultCookieStore(cookieStore).build();
			// } else {
			// client = HttpClients.createDefault();
			// login(client);
			// }

			httpResponse = client.execute(httpPost);
			// 获取服务器端返回的状态码和输入流,将输入流转换成字符串
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(httpResponse.getEntity(), "utf-8");
			}

		} catch (Exception e) {
			logger.error("", e);
		} finally {
			if (httpResponse != null) {
				try {
					EntityUtils.consume(httpResponse.getEntity());
				} catch (IOException e) {
					logger.error("", e);
				}
			}

			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					logger.error("", e);
				}
			}
		}

		return "";
	}
}

9、ES处理工具

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.count.CountRequestBuilder;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lang3.StringUtils;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ESClient {

    private static final Logger log = LoggerFactory.getLogger(ESClient.class);

    private TransportClient client;

    public ESClient() {
        this.init();
    }


    private void close() {
        if (this.client != null) {
            this.client.close();
        }
    }

    @Override
    public void finalize() throws Throwable {
        this.close();
        super.finalize();
    }

    private void init() {
        try {
            Settings settings = ImmutableSettings.settingsBuilder().loadFromClasspath("elasticsearch.yml")
                    .put("client.transport.sniff", true).build();
            this.client = new TransportClient(settings);

            int port = settings.getAsInt("client.transport.port", 9900);
            String[] ips = settings.getAsArray("client.transport.ip");

            for (String ip : ips) {
                log.info("the ip is:" + ip);
                client.addTransportAddress(new InetSocketTransportAddress(ip, port));
            }
            log.info("es连接成功:{},{}", client, JSONObject.toJSONString(client.listedNodes()));

        } catch (Exception e) {
            if (client != null) {
                client.close();
            }
            log.error("连接es失败!", e);
        }
    }

    public void createIndex(String index) throws Exception {
        client.admin().indices().prepareCreate(index).execute().actionGet();
    }

    /**
     * 为一种类型建立mapping
     *
     * @param index   索引名,相当于关系型数据库的库名
     * @param type    文档类型,相当于关系型数据库的表名
     * @param builder mapping内容, 格式 { "properties": { "fieldName1": { "type":
     *                "string", "analyzer": "ik" }, "fieldName2": { "type":
     *                "string", "index": "not_analyzed" } } }
     */
    public void mappingDoc(String index, String type, XContentBuilder builder)
            throws Exception {
        // XContentBuilder builder = XContentFactory.jsonBuilder()
        // .startObject()
        // .startObject("properties")
        // .startObject("province")
        // .field("type", "string")
        // //.field("store", "yes")
        // .field("analyzer","ik")
        // .field("index","analyzed")
        // //.field("indexAnalyzer", "ik")
        // //.field("searchAnalyzer", "ik")
        // .endObject()
        // .endObject()
        // .endObject();

        PutMappingRequest mapping = Requests.putMappingRequest(index)
                .type(type).source(builder);
        client.admin().indices().putMapping(mapping).actionGet();
    }

    /**
     * 为一份文档建立索引,采用自生成id
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param json  json格式的数据集
     * @return
     */
    public IndexResponse indexDoc(String index, String type, String json) throws Exception {
        IndexRequestBuilder builder = client.prepareIndex(index, type);
        IndexResponse response = builder.setSource(json)
                .execute()
                .actionGet();
        return response;
    }

    /**
     * 为一份文档建立索引,采用自生成id
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param kvMap 键值对形式的数据集
     * @return
     */
    public IndexResponse indexDoc(String index, String type, Map<String, Object> kvMap)
            throws Exception {
        IndexRequestBuilder builder = client.prepareIndex(index, type);
        IndexResponse response = builder.setSource(kvMap)
                .execute()
                .actionGet();
        return response;
    }

    /**
     * 为一份文档建立索引
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param id    文档id
     * @param json  json格式的数据集
     * @return
     */
    public IndexResponse indexDoc(String index, String type, String id, String json)
            throws Exception {

        IndexRequestBuilder builder = client.prepareIndex(index, type, id);
        IndexResponse response = builder.setSource(json)
                .execute()
                .actionGet();
        return response;
    }

    /**
     * 为一份文档建立索引
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param id    文档id
     * @param kvMap 键值对形式的数据集
     * @return
     */
    public IndexResponse indexDoc(String index, String type, String id, Map<String, Object> kvMap)
            throws Exception {
        IndexRequestBuilder builder = client.prepareIndex(index, type, id);
        IndexResponse response = builder.setSource(kvMap)
                .execute()
                .actionGet();
        return response;
    }


    /**
     * 为多份文档建立索引,采用自生成id
     *
     * @param index    索引名,相当于关系型数据库的库名
     * @param type     文档类型,相当于关系型数据库的表名
     * @param jsonList json格式的文档数据: List<json>
     * @return
     */
    public BulkResponse batchIndexDocsForJson(String index, String type, List<String> jsonList)
            throws Exception {
        if (jsonList.isEmpty()) {
            throw new Exception("批量创建索引时,传入的参数'jsonList'为空!");
        }

        List<IndexRequest> requestList = new ArrayList<IndexRequest>(jsonList.size());

        for (String json : jsonList) {
            IndexRequest request = client.prepareIndex(index, type)
                    .setSource(json)
                    .request();
            requestList.add(request);
        }

        BulkRequestBuilder bulkRequest = client.prepareBulk();
        for (IndexRequest request : requestList) {
            bulkRequest.add(request);
        }

        BulkResponse response = bulkRequest
                .execute()
                .actionGet();

        return response;
    }


    /**
     * 为多份文档建立索引,采用自生成id
     *
     * @param index  索引名,相当于关系型数据库的库名
     * @param type   文档类型,相当于关系型数据库的表名
     * @param kvList 键值对形式的文档数据:List<Map<field, value>>
     * @return
     */
    public BulkResponse batchIndexDocsForMap(String index, String type, List<Map<String, Object>> kvList)
            throws Exception {
        if (kvList.isEmpty()) {
            throw new Exception("批量创建索引时,传入的参数'kvList'为空!");
        }

        List<String> jsonList = new ArrayList<String>(kvList.size());
        for (Map<String, Object> kvMap : kvList) {
            jsonList.add(JSONObject.toJSONString(kvMap));
        }

        BulkResponse response = this.batchIndexDocsForJson(index, type, jsonList);
        jsonList.clear();

        return response;
    }


    /**
     * 为多份文档建立索引
     *
     * @param index     索引名,相当于关系型数据库的库名
     * @param type      文档类型,相当于关系型数据库的表名
     * @param idJsonMap id及json格式的文档数据: Map<id,json>
     * @return
     */
    public BulkResponse batchIndexDocsForJson(String index, String type, Map<String, String> idJsonMap)
            throws Exception {
        if (idJsonMap.isEmpty()) {
            throw new Exception("批量创建索引时,传入的参数'idJsonMap'为空!");
        }

        List<IndexRequest> requestList = new ArrayList<IndexRequest>(idJsonMap.size());

        for (String id : idJsonMap.keySet()) {
            String json = idJsonMap.get(id);
            IndexRequest request = client.prepareIndex(index, type, id)
                    .setSource(json)
                    .request();
            requestList.add(request);
        }

        BulkRequestBuilder bulkRequest = client.prepareBulk();
        for (IndexRequest request : requestList) {
            bulkRequest.add(request);
        }

        BulkResponse response = bulkRequest
                .execute()
                .actionGet();

        return response;
    }


    /**
     * 为多份文档建立索引
     *
     * @param index   索引名,相当于关系型数据库的库名
     * @param type    文档类型,相当于关系型数据库的表名
     * @param idKvMap id及键值对形式的文档数据:Map<id,Map<field, value>>
     * @return
     */
    public BulkResponse batchIndexDocsForMap(String index, String type, Map<String, Map<String, Object>> idKvMap)
            throws Exception {
        if (idKvMap.isEmpty()) {
            throw new Exception("批量创建索引时,传入的参数'idKvMap'为空!");
        }

        Map<String, String> idJsonMap = new HashMap<String, String>(idKvMap.size());
        for (String id : idKvMap.keySet()) {
            Map<String, Object> kvMap = idKvMap.get(id);
            idJsonMap.put(id, JSONObject.toJSONString(kvMap));
        }

        BulkResponse response = this.batchIndexDocsForJson(index, type, idJsonMap);
        idJsonMap.clear();

        return response;
    }

    /**
     * 更新一个doc, 若不存在则插入
     *
     * @param index
     * @param type
     * @param id
     * @param json
     * @param script
     * @throws java.util.concurrent.ExecutionException
     * @throws InterruptedException
     */
//    public UpdateResponse upsertDoc(String index, String type, String id, String json, String script) throws Exception {
//        IndexRequest indexRequest = new IndexRequest(index, type, id).source(json);
//        UpdateRequest updateRequest = new UpdateRequest(index, type, id);
//        //updateRequest.doc();
//        updateRequest.upsert(indexRequest);
//        updateRequest.script(script);
//
//        UpdateResponse response = client.update(updateRequest).get();
//
//        return response;
//    }
    
    public UpdateResponse upsertDoc(String index, String type, String id, String insertJson, String updateJson) throws Exception {
        IndexRequest indexRequest = new IndexRequest(index, type, id).source(insertJson);
        UpdateRequest updateRequest = new UpdateRequest(index, type, id);
        updateRequest.doc(updateJson);
        updateRequest.upsert(indexRequest);
        
        UpdateResponse response = client.update(updateRequest).get();

        return response;
    }

    /**
     * 根据条件 统计个数
     *
     * @param queryBuilder 查詢條件
     * @param index        索引库名 相當於 数据库名
     * @param type         索引类型 相當於 表名
     * @return
     */
    public long countQuery(String index, String type, QueryBuilder queryBuilder) {
        CountRequestBuilder crb = client.prepareCount(index).setTypes(type);
        if (queryBuilder != null) {
            crb.setQuery(queryBuilder);
        }

        CountResponse response = crb.execute().actionGet();
        return response.getCount();
    }

    public SearchResponse searchAgg(String index, String type, String searchType, QueryBuilder queryBuilder,
                                    AbstractAggregationBuilder aggBuilder) {
        SearchRequestBuilder builder = client.prepareSearch(index).setTypes(type);

        if (!StringUtils.isEmpty(searchType)) {
            builder.setSearchType(SearchType.valueOf(searchType));
        }

        if (queryBuilder != null) {
            builder = builder.setQuery(queryBuilder);
        }
        if (aggBuilder != null) {
            builder = builder.addAggregation(aggBuilder);
        }

        SearchResponse searchResponse = builder.execute().actionGet();

        return searchResponse;
    }

    /**
     * 删除一个文档
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param id    键值对形式的数据集
     * @return
     */
    public DeleteResponse deleteDoc(String index, String type, String id) throws InterruptedException {
        DeleteRequestBuilder builder = client.prepareDelete(index, type, id);
        DeleteResponse response = builder
                .execute()
                .actionGet();
        return response;
    }

    /**
     * 根据条件删除多个文档
     *
     * @param index        索引名,相当于关系型数据库的库名
     * @param type         文档类型,相当于关系型数据库的表名
     * @param queryBuilder 查询器
     * @return
     */
    public void deleteDocsByQuery(String index, String type, QueryBuilder queryBuilder) {
        client.prepareDeleteByQuery(index).setTypes(type).setQuery(queryBuilder)
                .execute()
                .actionGet();
    }


    /**
     * 指定id获取文档
     *
     * @param index 索引名,相当于关系型数据库的库名
     * @param type  文档类型,相当于关系型数据库的表名
     * @param id    文档id
     * @return
     */
    public Map<String, Object> getDoc(String index, String type, String id) {
        GetResponse response = client.prepareGet(index, type, id)
                .execute()
                .actionGet();

        Map<String, Object> retMap = response.getSourceAsMap();
        return retMap;
    }


    public List<Map<String, Object>> search(String index, String type, QueryBuilder queryBuilder, FilterBuilder filterBuilder) {
        SearchRequestBuilder builder = client.prepareSearch(index).setTypes(type);

        if (queryBuilder != null) {
            builder = builder.setQuery(queryBuilder);
        }
        if (filterBuilder != null) {
            builder = builder.setPostFilter(filterBuilder);
        }

        SearchResponse searchResponse = builder.execute().actionGet();

        SearchHits hits = searchResponse.getHits();
        log.info("Es Hits count: " + hits.getTotalHits());

        List<Map<String, Object>> kvList = new ArrayList<Map<String, Object>>();

        SearchHit[] hitArray = hits.getHits();
        if (hitArray.length > 0) {
            for (SearchHit hit : hitArray) {
                Map<String, Object> kvMap = hit.getSource();

                kvMap.put("version", hit.getVersion());
                kvMap.put("_id", hit.getId());
                kvList.add(kvMap);
            }
        }
        return kvList;
    }
}

10、Hbase处理工具

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 操作Hbase的常用方法
 * <p/>
 * Created by huqingmiao on 2015/4/14.
 */
public class HbaseUtil {

    private static final Logger log = LoggerFactory.getLogger(HbaseUtil.class);

    private static Configuration conf = null;

    private static HConnection conn = null;

    private static String HADOOP_HOME = "C:/hadoop";

    static {

//        try {
//            String hadoopHome = System.getProperties().getProperty("hadoop.home.dir"); //Windows下的HOME目录, 在unix下部署不需要设置
//            if (hadoopHome == null || "".equals(hadoopHome.trim())) {
//                hadoopHome = HADOOP_HOME;
//            }
//
//            File hadoopBinDir = new File(hadoopHome, "bin");      //HOME目录下的bin目录
//            if (!hadoopBinDir.exists()) {
//                hadoopBinDir.mkdirs();
//            }
//            File winExeFile = new File(hadoopBinDir.getCanonicalPath() + File.separator + "winutils.exe");
//            if (!winExeFile.exists()) {
//                winExeFile.createNewFile();
//            }
//
//            //设置环境变量
//            System.getProperties().put("hadoop.home.dir", hadoopHome);
//
//        } catch (IOException e) {
//            log.error("create ./bin/winutils.exe error.", e);
//        }

        //默认从hbase-site.xml读取配置信息
        conf = HBaseConfiguration.create();
//        conf.set("hbase.zookeeper.property.clientPort", "2181");
//        conf.set("hbase.zookeeper.quorum", "10.75.201.125");
//        conf.set("hbase.master", "10.75.201.125:60010");
        //conf.set("hbase.zookeeper.quorum", "hmaster");
        //与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
      //  conf.set("hbase.zookeeper.property.clientPort", "2181");
    }


    public HbaseUtil() {
        try {
            //预先创建了一个连接,以后的访问都共享该连接
        	//conf.addResource("hbase-site.xml");
            conn = HConnectionManager.createConnection(conf);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void finalize() throws Throwable {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        super.finalize();
    }


    /**
     * 建表
     *
     * @param tableName     表名
     * @param columnFamilys 列簇名
     * @throws Exception
     */
    public void createTable(String tableName, String[] columnFamilys) throws Exception {
        HBaseAdmin hAdmin = null;
        try {
            hAdmin = new HBaseAdmin(conf);

            if (hAdmin.tableExists(tableName)) {
                log.info("已经存在要创建的表:" + tableName);

            } else {
                HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));

                //描述列族
                for (String columnFamily : columnFamilys) {
                    tableDesc.addFamily(new HColumnDescriptor(columnFamily));
                }

                //建表
                hAdmin.createTable(tableDesc);
                log.info("成功创建表:" + tableName);
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (hAdmin != null) {
                    hAdmin.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 删除表
     *
     * @param tableName 表名
     * @throws Exception
     */
    public void deleteTable(String tableName) throws Exception {
        HBaseAdmin hAdmin = null;
        try {
            hAdmin = new HBaseAdmin(conf);

            if (hAdmin.tableExists(tableName)) {
                hAdmin.disableTable(tableName);//禁用表
                hAdmin.deleteTable(tableName);// 删除表
                log.info("成功删除表:" + tableName);

            } else {
                log.info("要删除的表不存在:" + tableName);
            }

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (hAdmin != null) {
                    hAdmin.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 向指定的行、列簇、列写入一项数据;如果该行不存在,则会插入一行。
     *
     * @param tableName 表名
     * @param rowkey    行键
     * @param colFamily 列簇名
     * @param column    列名
     * @param value     列值
     * @throws Exception
     */
    public void putData(String tableName, String rowkey,
                        String colFamily, String column, String value) throws Exception {

        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);
            Put put = new Put(Bytes.toBytes(rowkey));

            // 参数分别为:列族、列、值
            put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value));

            table.put(put);
            log.info("成功写入1项数据到{}.", tableName);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 向指定的行、列簇、列写入一项数据;如果该行不存在,则会插入一行。
     *
     * @param tableName 表名
     * @param hbCell    存放行键、列簇、列名、列值的数据单元
     * @throws Exception
     */
    public void putData(String tableName, HbaseCell hbCell) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);
            Put put = new Put(convertToBytes(hbCell.getRowkey()));

            // 参数分别为:列族、列、值
            put.add(Bytes.toBytes(hbCell.getColFamily()), Bytes.toBytes(hbCell.getColName()), convertToBytes(hbCell.getColValue()));

            table.put(put);
            log.info("成功写入1项数据到{}.", tableName);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 写入多行、多列数据
     *
     * @param tableName  表名
     * @param hbCellList 存放行键、列簇、列名、列值的数据单元.
     * @throws Exception
     */
    public void putData(String tableName, List<HbaseCell> hbCellList) throws Exception {
        if (hbCellList.isEmpty()) {
            return;
        }
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            List<Put> putList = new ArrayList<Put>();
            for (HbaseCell hbCell : hbCellList) {
                Put put = new Put(convertToBytes(hbCell.getRowkey()));
                put.add(Bytes.toBytes(hbCell.getColFamily()), Bytes.toBytes(hbCell.getColName()), convertToBytes(hbCell.getColValue()));

                putList.add(put);
            }

            table.put(putList);
            log.info("成功写入{}项数据到{}.", hbCellList.size(), tableName);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    /**
     * 写入多行、多列数据
     *
     * @param tableName  表名
     * @param hbCellList 存放行键、列簇、列名、列值的数据单元.
     * @throws Exception
     */
    public void putDataForNotNull(String tableName, List<HbaseCell> hbCellList) throws Exception {
        if (hbCellList.isEmpty()) {
            return;
        }
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            List<Put> putList = new ArrayList<Put>();
            for (HbaseCell hbCell : hbCellList) {
                if (!StringUtils.isEmpty(hbCell.getColValue() + "")) {
                    Put put = new Put(convertToBytes(hbCell.getRowkey()));
                    put.add(Bytes.toBytes(hbCell.getColFamily()), Bytes.toBytes(hbCell.getColName()), convertToBytes(hbCell.getColValue()));
                    putList.add(put);
                }
            }
            table.put(putList);
            log.info("成功写入{}项数据到{}.", hbCellList.size(), tableName);

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    /**
     * 删除一行
     *
     * @param tableName 表名
     * @param rowkey    行键
     * @throws Exception
     */

    public void delRow(String tableName, String rowkey) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);
            Delete del = new Delete(Bytes.toBytes(rowkey));

            table.delete(del);
            log.info("成功删除1行数据!");

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 删除多行
     *
     * @param tableName 表名
     * @param rowkeys   行键
     * @throws Exception
     */
    public void delMulitRows(String tableName, List<String> rowkeys) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            List<Delete> delList = new ArrayList<Delete>();
            for (String rowkey : rowkeys) {
                Delete del = new Delete(Bytes.toBytes(rowkey));
                delList.add(del);
            }
            table.delete(delList);
            delList.clear();
            log.info("成功删除{}行数据.", delList.size());

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 获取指定行的所有数据项
     *
     * @param tableName 表名
     * @param rowkey    行键
     * @return
     * @throws Exception
     */
    public Result getRow(String tableName, String rowkey) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            Get get = new Get(Bytes.toBytes(rowkey));
            Result rs = table.get(get);

//            for (Cell cell : result.rawCells()) {
//                System.out.print("Row Name: " + new String(CellUtil.cloneRow(cell)) + " ");
//                System.out.print("Timestamp: " + cell.getTimestamp() + " ");
//                System.out.print("column Family: " + new String(CellUtil.cloneFamily(cell)) + " ");
//                System.out.print("column Name:  " + new String(CellUtil.cloneQualifier(cell)) + " ");
//                System.out.println("Value: " + new String(CellUtil.cloneValue(cell)) + " ");
//            }

            return rs;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 获取指定表的所有行的数据项
     *
     * @param tableName 表名
     * @return
     * @throws Exception
     */
    public List<Result> findAllRows(String tableName) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            Scan scan = new Scan();
            ResultScanner results = table.getScanner(scan);

            List<Result> rsList = new ArrayList<Result>();
            for (Result rs : results) {
                rsList.add(rs);
            }
            return rsList;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 通用查询
     *
     * @param tableName 表名
     * @param filter    查询过滤器。单一条件查询可传Filter对象,组合条件查询可传FilterList, FilterList是Filter的子类。
     */
    public List<Result> findRow(String tableName, Filter filter) throws Exception {
        HTableInterface table = null;
        try {
            //table = new HTable(conf, tableName);
            table = conn.getTable(tableName);

            Scan scan = new Scan();
            scan.setFilter(filter);
            ResultScanner results = table.getScanner(scan);

            List<Result> rsList = new ArrayList<Result>();
            for (Result rs : results) {
                rsList.add(rs);
            }
            return rsList;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw e;
        } finally {
            try {
                if (table != null) {
                    table.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }


    /**
     * 打印、展示查询结果
     *
     * @param result
     */
    public void showData(Result result) {

        for (Cell cell : result.rawCells()) {
            log.info("Row: " + new String(CellUtil.cloneRow(cell)) + " ");
            log.info("Timestamp: " + cell.getTimestamp() + " ");
            log.info("Column Family: " + new String(CellUtil.cloneFamily(cell)) + " ");
            log.info("Column Name:  " + new String(CellUtil.cloneQualifier(cell)) + " ");
            log.info("Column Value: " + new String(CellUtil.cloneValue(cell)) + " ");
        }

    }

    /**
     * 打印、展示查询的各项列值
     *
     * @param rsList
     */
    public void showData(List<Result> rsList) {
        log.info(">>>总的数据条数:" + rsList.size());

        if (rsList.isEmpty()) {
            return;
        }
        for (Result rs : rsList) {
            Cell[] cells = rs.rawCells();
            for (Cell cell : rs.rawCells()) {
                log.info("Row: " + new String(CellUtil.cloneRow(cell)) + " ");
                log.info("Timestamp: " + cell.getTimestamp() + " ");
                log.info("Column Family: " + new String(CellUtil.cloneFamily(cell)) + " ");
                log.info("Column Name:  " + new String(CellUtil.cloneQualifier(cell)) + " ");
                log.info("Column Value: " + new String(CellUtil.cloneValue(cell)) + " ");
            }
        }
    }


    /**
     * 打印、展示查询的各项列值
     *
     * @param rsList
     */
    public void showRowkey(List<Result> rsList) {
        log.info(">>>总的数据条数:" + rsList.size());

        if (rsList.isEmpty()) {
            return;
        }
        for (Result rs : rsList) {
            log.info(new String(rs.getRow()));
        }
    }

    private byte[] convertToBytes(Object obj) throws Exception {
        if (obj == null) {
            return new byte[0];
        }
        if (obj instanceof String) {
            return Bytes.toBytes((String) obj);
        }
        if (obj instanceof Double) {
            return Bytes.toBytes((Double) obj);
        }
        if (obj instanceof Float) {
            return Bytes.toBytes((Float) obj);
        }
        if (obj instanceof Long) {
            return Bytes.toBytes((Long) obj);
        }
        if (obj instanceof Integer) {
            return Bytes.toBytes((Integer) obj);
        }
        if (obj instanceof Date) {
            return Bytes.toBytes(((Date) obj).getTime());
        }
        if (obj instanceof Timestamp) {
            return Bytes.toBytes(((Timestamp) obj).getTime());
        }
        if (obj instanceof BigDecimal) {
            return Bytes.toBytes((BigDecimal) obj);
        }
        throw new Exception("未能识别的数据类型: " + obj.getClass().getName());
    }


    // main
    public static void main(String[] args) {
        try {
        	HbaseUtil client = new HbaseUtil();

            String tableName = "testtable";

            // 创建数据库表:“studyinfo”
            String[] colFamilys = {"studyinfo", "course"};
            client.createTable(tableName, colFamilys);

            // 添加第一行数据
            client.putData(tableName, "ligan", "studyinfo", "age", "2333");
            client.putData(tableName, "ligan", "studyinfo", "sex", "boy");
            client.putData(tableName, "ligan", "course", "china", "97");
            client.putData(tableName, "ligan", "course", "math", "128");
            client.putData(tableName, "ligan", "course", "english", "85");

            // 添加第二行数据
            client.putData(tableName, "xiaoxue", "studyinfo", "age", "20");
            client.putData(tableName, "xiaoxue", "studyinfo", "sex", "boy");
            client.putData(tableName, "xiaoxue", "course", "china", "90");
            client.putData(tableName, "xiaoxue", "course", "math", "100");
            client.putData(tableName, "xiaoxue", "course", "english", "90");

            // 添加第三行数据,也可以这样写:
            HbaseCell hbCell1 = new HbaseCell("walker", "studyinfo", "age", "18");
            HbaseCell hbCell2 = new HbaseCell("walker", "studyinfo", "sex", "girl");
            HbaseCell hbCell3 = new HbaseCell("walker", "course", "math", "100");
            HbaseCell hbCell4 = new HbaseCell("walker", "course", "english", "30");
            List<HbaseCell> cellList = new ArrayList<HbaseCell>();
            cellList.add(hbCell1);
            cellList.add(hbCell2);
            cellList.add(hbCell3);
            cellList.add(hbCell4);
            client.putData(tableName, cellList);


            // 获取一条数据
            log.info("获取一条数据");
            Result rs = client.getRow(tableName, "ligan");
            client.showData(rs);


            //组合查询
            log.info("组合查询");
            List<Filter> filters = new ArrayList<Filter>();
            Filter filter1 = new SingleColumnValueFilter(Bytes
                    .toBytes("studyinfo"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, Bytes
                    .toBytes("18"));
            filters.add(filter1);

            Filter filter2 = new SingleColumnValueFilter(Bytes
                    .toBytes("course"), Bytes.toBytes("math"), CompareFilter.CompareOp.EQUAL, Bytes
                    .toBytes("100"));
            filters.add(filter2);

            FilterList filterList = new FilterList(filters);

            List<Result> rsList = client.findRow(tableName, filterList);
            log.info(">>>" + rsList.size());


            // 获取所有数据
            log.info("获取所有数据");
            rsList = client.findAllRows(tableName);
            log.info(">>>" + rsList.size());

            //删除一条数据
            log.info("删除一条数据");
            client.delRow(tableName, "tht");
            log.info(">>>" + rsList.size());

            //删除多条数据
            log.info("删除多条数据");
            List<String> rows = new ArrayList<String>();
            rows.add("xiaoxue");
            rows.add("walker");
            client.delMulitRows(tableName, rows);
            client.findAllRows(tableName);
            log.info(">>>" + rsList.size());


            //删除数据库
            log.info("删除表");
            client.deleteTable(tableName);


        } catch (Exception err) {
            err.printStackTrace();
        }
    }

}

11、mongodb处理工具

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.bson.Document;

import com.alibaba.fastjson.JSONObject;
import com.github.walker.mybatis.paginator.PageList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;


public class MongoManager {
	
	 
	    private static MongoClient  client = null;  
	  
	    private MongoManager() 
	    { 
	    	
	    }  
	  
	    static {  
	        initDBPrompties();  
	    }  
	  	  
	    /** 
	     * 初始化连接池 
	     */  
	    private static void initDBPrompties() {  	    	    		
	    	 String url=PropertyHolder.getProperty("mongodb.url");
	    	 String dbName=PropertyHolder.getProperty("mongodb.dbName"); 
	    	 String userName=PropertyHolder.getProperty("mongodb.userName");
	    	 String password=PropertyHolder.getProperty("mongodb.password");
	    	 int connectionsPerHost = Integer.valueOf(PropertyHolder.getProperty("mongodb.connectionsPerHost"));
	    	 int threads = Integer.valueOf(PropertyHolder.getProperty("mongodb.threads"));
	    	 int maxWaitTime=Integer.valueOf(PropertyHolder.getProperty("mongodb.maxWaitTime"));
	    	 int socketTimeout=Integer.valueOf(PropertyHolder.getProperty("mongodb.socketTimeout"));
	    	 int maxConnectionLifeTime=Integer.valueOf(PropertyHolder.getProperty("mongodb.maxConnectionLifeTime"));
	    	 int connectTimeout = Integer.valueOf(PropertyHolder.getProperty("mongodb.connectTimeout"));
	    	 
	    	 List<MongoCredential> credentials = new ArrayList<MongoCredential>();
	    	 ServerAddress address = new ServerAddress(url);
	    	 MongoCredential credential = MongoCredential.createCredential(userName,dbName,password.toCharArray());
			 credentials.add(credential);
	    	 
	    	 MongoClientOptions.Builder build = new MongoClientOptions.Builder();  	         
	         build.connectionsPerHost(connectionsPerHost);
	         build.maxWaitTime(maxWaitTime);
	         build.maxConnectionLifeTime(maxConnectionLifeTime);
	         build.connectTimeout(connectTimeout);
	         build.threadsAllowedToBlockForConnectionMultiplier(threads);
	         build.socketTimeout(socketTimeout);
	         MongoClientOptions options = build.build();  
	         client = new MongoClient(address, credentials, options);  
	    }  
	    
	    /**
	     * 获取数据库
	     * @param dbName 数据库
	     * @return
	     */
		public static MongoDatabase getDB(String dbName) {  
	        return client.getDatabase(dbName);
	    }
	    
	    /**
	     * 获取表
	     * @param dbName 数据库
	     * @param collectionName 集合
	     * @return
	     */
	    public static MongoCollection<Document>  getCollection(String dbName,String collectionName)
	    {
	    	 MongoCollection<Document>  collection = getDB(dbName).getCollection(collectionName);
	    	 return collection;
	    }
	    
	    /**
	     * 插入表数据
	     * @param dbName 数据库
	     * @param collectionName 集合
	     * @param json 待入库json
	     */
	    public static void insert(String dbName,String collectionName,String json)
	    {
	    	MongoCollection<Document> collection = getCollection(dbName, collectionName);
	        Document document = Document.parse(json);
	    	collection.insertOne(document);
	    }
	    
	    /**
	     * 分页查询用户操作日志
	     * @param dbName 数据库
	     * @param collectionName 集合
	     * @param acctNo 账户号
	     * @param start 
	     * @param pageSize
	     * @return
	     */
	    public static PageList<UserOpLog> findUserOpLog(String dbName,String collectionName,String acctNo,String tenantId,String keyWord,String startDate,String endDate,int start,int pageSize)
	    {   
	    	List<UserOpLog> logList = new ArrayList<UserOpLog>();
	    	MongoCollection<Document> collection = getCollection(dbName, collectionName);
	        BasicDBObject queryObject = new BasicDBObject();
	        BasicDBObject tmpObject = new BasicDBObject();
	        BasicDBObject dateObject = new BasicDBObject();	       
	        if(StringUtils.isNotEmpty(acctNo))
	        {   
	        	
	        	queryObject.put("acctNo", acctNo);		        
	        }
	        if(tenantId!=null)
	        {
	        	queryObject.put("tenantId", tenantId);		        
	        }
	        if(StringUtils.isNotEmpty(keyWord))
	        {   
	        	Pattern pattern = Pattern.compile("^.*"+keyWord+".*$", Pattern.CASE_INSENSITIVE);
	        	queryObject.put("opDesc", pattern);		        
	        }
	        
	        tmpObject.put("$gte", startDate); //大于
	        dateObject = tmpObject.append("$lte", endDate);	//小于      	               	        
	        queryObject.put("opTime", dateObject);	      
	        FindIterable<Document> iterator= collection.find(queryObject).sort((new BasicDBObject("opTime",-1)));		       
	        int count = 0;
	        MongoCursor<Document> cursor= iterator.iterator();
	        while(cursor.hasNext()) { 
	        	Document doc = cursor.next();
	        	if(count>=start && count<=pageSize+start-1)
	        	{	        		
	                UserOpLog userOpLog = new UserOpLog();
	                userOpLog.setAcctNo(doc.getString("acctNo"));
	                userOpLog.setClasz(doc.getString("clasz"));
	                userOpLog.setErrorMsg(doc.getString("errorMsg"));
	                userOpLog.setMethod(doc.getString("method"));
	                userOpLog.setName(doc.getString("name"));
	                userOpLog.setOpDesc(doc.getString("opDesc"));
	                userOpLog.setOpResult(doc.getInteger("opResult"));
	                userOpLog.setOpTime(doc.getString("opTime"));
	                userOpLog.setUri(doc.getString("uri"));
	                userOpLog.setTenantId(doc.getString("tenantId"));
	                logList.add(userOpLog);
	        	}
                count++;
            }  
	        cursor.close();
	        PageList<UserOpLog> pageList = new PageList<UserOpLog>(logList,count);
	        return pageList;	        
	    }
	    
	    /**
	     * 分页查询接口调用日志
	     * @param dbName 数据库
	     * @param collectionName 集合
	     * @param tenantId 商户ID
	     * @param appId 应用ID
	     * @param startDate 开始日期
	     * @param endDate 结束日期
	     * @param start
	     * @param pageSize
	     * @return
	     */
	    public static PageList<UserCallLog> findUserCallLog(String dbName,String collectionName,String tenantId,String appId,String startDate,String endDate,int start,int pageSize)
	    {   
	    	List<UserCallLog> logList = new ArrayList<UserCallLog>();
	    	MongoCollection<Document> collection = getCollection(dbName, collectionName);
	        BasicDBObject queryObject = new BasicDBObject();
	        BasicDBObject tmpObject = new BasicDBObject();
	        BasicDBObject dateObject = new BasicDBObject();	       
	        if(StringUtils.isNotEmpty(tenantId))
	        {
	        	queryObject.put("tenantId", tenantId);	
	        	
	        }
	        if(StringUtils.isNotEmpty(appId))
	        {
	        	queryObject.put("appId", appId);		        
	        }	       
	        
	        tmpObject.put("$gte", startDate); //大于
	        dateObject = tmpObject.append("$lte", endDate);	//小于      	               	        
	        queryObject.put("reqTime", dateObject);	      
	        FindIterable<Document> iterator= collection.find(queryObject) ;		       
	        int count = 0;
	        MongoCursor<Document> cursor= iterator.iterator();
	        while(cursor.hasNext()) { 
	        	Document doc = cursor.next();
	        	if(count>=start && count<=pageSize+start-1)
	        	{	        		
	        		UserCallLog userCallLog = new UserCallLog();
	        		userCallLog.setAppId(doc.getString("appId"));
	                userCallLog.setClientHost(doc.getString("clientHost"));
	                userCallLog.setClientIp(doc.getString("clientIp"));
	                userCallLog.setClientPort(doc.getInteger("clientPort"));
	                userCallLog.setErrorCode(doc.getString("errorCode"));
	                userCallLog.setErrorMsg(doc.getString("errorMsg"));
	                userCallLog.setFlowNo(doc.getString("flowNo"));
	                userCallLog.setInterfaceClasz(doc.getString("interfaceClasz"));
	                userCallLog.setInterfaceId(doc.getString("interfaceId"));
	                userCallLog.setMethodId(doc.getString("methodId"));
	                userCallLog.setMethodName(doc.getString("methodName"));
	                userCallLog.setReqBytes(doc.getInteger("reqBytes"));
	                userCallLog.setReqTime(doc.getString("reqTime"));
	                userCallLog.setResBytes(doc.getInteger("resBytes"));
	                userCallLog.setResTime(doc.getString("resTime"));
	                userCallLog.setSvcId(doc.getString("svcId"));
	                userCallLog.setSvcInterface(doc.getString("svcInterface"));
	                userCallLog.setTenantId(doc.getString("tenantId"));
	                userCallLog.setToken(doc.getString("token"));
	                userCallLog.setUri(doc.getString("uri"));
	                logList.add(userCallLog);
	        	}
                count++;
            }  
	        cursor.close();
	        PageList<UserCallLog> pageList = new PageList<UserCallLog>(logList,count);
	        return pageList;	        
	    }
	    
	   
}

12、redis处理工具

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class QCloudJedis {

    private static final Logger log = LoggerFactory.getLogger(QCloudJedis.class);
    
    private static final Properties properties = ClassesConfigLoader.getProperties();
    private static final String host = properties.getProperty("redis.host");
    private static final int port = Integer.valueOf(properties.getProperty("redis.port"));
    private static final String instanceid = properties.getProperty("redis.instanceid");
    private static final String password = properties.getProperty("redis.password");
    private static final int timeout = Integer.valueOf(properties.getProperty("redis.timeout", "2000"));
    private static final int maxTotal = Integer.valueOf(properties.getProperty("redis.maxTotal", "1024"));
    private static final int maxIdle = Integer.valueOf(properties.getProperty("redis.maxIdle", "10"));
    private static final int maxWaitMillis = Integer.valueOf(properties.getProperty("redis.maxWaitMillis", "3000"));
    private static final boolean testOnIdle = Boolean.valueOf(properties.getProperty("redis.testOnIdle", "true"));  //是否checkIdle
    private static final int timeCheckIdle = Integer.valueOf(properties.getProperty("redis.timeCheckIdle", "60000"));   //每隔多少秒check一次
    private static final int idleTimeout = Integer.valueOf(properties.getProperty("redis.idleTimeout", "300000"));     //超时时间
    private static final int numTestsPerEvictionRun = Integer.valueOf(properties.getProperty("redis.numTestsPerEvictionRun", "1024")); //一次驱逐过程中,最多驱逐对象的个数
    
    private static JedisPool pool = null;
    //private static Jedis jedis = null;
    private static Object lock = new Object();
    
    
    static {
    	init();
    }
    
    private static void init() {
    	if (null == pool) {
    		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
            config.setMaxTotal(maxTotal);
			config.setMaxIdle(maxIdle);
			config.setMaxWaitMillis(maxWaitMillis);

			config.setTestWhileIdle(testOnIdle);
			config.setTimeBetweenEvictionRunsMillis(timeCheckIdle);
			config.setMinEvictableIdleTimeMillis(idleTimeout);
			config.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
			synchronized (lock) {
				if (null == pool) {
					try {			            
			            
			            pool = new JedisPool(config, host, port, timeout, instanceid + ":" + password);
			            log.info("init jedis pool successful!");
			        } catch (Exception e) {
			            log.error("", e);
			        }
				}
			}
		}
    	   	
    }

//    public static Jedis getInstance() {
//    	init();
//        
//        return jedis;
//    }
    
    /**
	 * 获取一个jedis 对象
	 * 
	 * @return
	 */
	private static Jedis getJedis() {
		if (pool == null) {
			init();
		}
		return pool.getResource();
	}

	/**
	 * 返还到连接池
	 * 
	 * @param pool
	 * @param redis
	 */
	private static void returnResource(Jedis redis) {
		if (redis != null) {
			redis.close();
		}
	}
	
	// 删除key
	public static Long del(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.del(key);
		} catch (Exception e) {
			log.error("操作Redis发生异常,异常详情:", e);
			return -1L;
		} finally {
			// 返还到连接池
			returnResource(jedis);
		}
		
	}
	
	public static Boolean exists(String key) {
		Jedis jedis = null;
		Boolean flag = false;
		try {
			jedis = getJedis();
			flag = jedis.exists(key);
		} catch (Exception e) {
			log.error("操作Redis发生异常,异常详情:", e);
		} finally {
			// 返还到连接池
			returnResource(jedis);
		}

		return flag;
	}

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		String value = null;
		Jedis jedis = null;
		try {
			jedis = getJedis();
			value = jedis.get(key);
		} catch (Exception e) {
			log.error("操作Redis发生异常,异常详情:", e);
		} finally {
			// 返还到连接池
			returnResource(jedis);
		}
		return value;
	}

	// 设置
	public static String set(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.set(key, value);
		} catch (Exception e) {
			log.error("操作Redis发生异常,异常详情:", e);
			return "";
		} finally {
			// 返还到连接池
			returnResource(jedis);
		}
	}
	
	// 获取Hash值
		public static String hget(String key, String field) {
			Jedis jedis = null;
			String value = null;
			try {
				jedis = getJedis();
				value = jedis.hget(key, field);
			} catch (Exception e) {
				log.error("操作Redis发生异常,异常详情:", e);
			} finally {
				// 返还到连接池
				returnResource(jedis);
			}
			return value;
		}

		public static Map<String, String> hgetAll(String key) {
			Jedis jedis = null;
			Map<String, String> value = null;
			try {
				jedis = getJedis();
				value = jedis.hgetAll(key);
			} catch (Exception e) {
				log.error("操作Redis发生异常,异常详情:", e);
			} finally {
				// 返还到连接池
				returnResource(jedis);
			}
			return value;
		}

		// 设置Hash值
		public static Long hset(String key, String field, String value) {
			Jedis jedis = null;
			try {
				jedis = getJedis();
				return jedis.hset(key, field, value);
			} catch (Exception e) {
				log.error("操作Redis发生异常,异常详情:", e);
				return -1L;
			} finally {
				// 返还到连接池
				returnResource(jedis);
			}
		}
		
		// 删除Hash值
		public static Long hdel(String key, String... fields) {
			Jedis jedis = null;
			try {
				jedis = getJedis();
				return jedis.hdel(key, fields);
			} catch (Exception e) {
				log.error("操作Redis发生异常,异常详情:", e);
				return -1L;
			} finally {
				// 返还到连接池
				returnResource(jedis);
			}
		}
		
		public static Long expire(String key, int seconds) {
			Jedis jedis = null;
			try {
				jedis = getJedis();
				return jedis.expire(key, seconds);
			} catch (Exception e) {
				log.error("操作Redis发生异常,异常详情:", e);
				return -1L;
			} finally {
				// 返还到连接池
				returnResource(jedis);
			}
		}

    public static void main(String[] args) {
        try {            
            
            while (true) {
            	String key = "ftc-ump-mid";
                //System.out.println("before setting: " + jedis.hget(key, "attr"));
                System.out.println(">>" + QCloudJedis.hset(key, "attr", "0"));
                QCloudJedis.expire(key, 5);
                         
                System.out.println("after setting: " + QCloudJedis.hget(key, "attr"));
                System.out.println(">>" + QCloudJedis.hdel(key, "attr"));
                
                System.out.println("after delete: " + QCloudJedis.hget(key, "attr"));
                
                Thread.sleep(1000 * 10);
            }
            

            //关闭退出
            //jedis.quit();
            //jedis.close();
            

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}





©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页