微信支付用到的几个java工具类

把微信支付demo里面的一些工具类整理了下,以备用!

目录

HttpUtil 工具类 

MD5Util 工具类

PayUtil 工具类

RandomUtil 工具类

SignUtil 工具类

XMLUtil 工具类

ZxingUtil 工具类


HttpUtil 工具类 

package util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;


public class HttpUtil {
	
    private final static int CONNECT_TIMEOUT = 5000; // in milliseconds
    private final static String DEFAULT_ENCODING = "UTF-8";

    public static String postData(String urlStr, String data){
        return postData(urlStr, data, null);
    }

    public static String postData(String urlStr, String data, String contentType){
        BufferedReader reader = null;
        try {
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.setConnectTimeout(CONNECT_TIMEOUT);
            conn.setReadTimeout(CONNECT_TIMEOUT);
            if(contentType != null)
                conn.setRequestProperty("content-type", contentType);
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
            if(data == null)
                data = "";
            writer.write(data);
            writer.flush();
            writer.close();

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            return sb.toString();
        } catch (IOException e) {
            System.err.println("Error connecting to " + urlStr + ": " + e.getMessage());
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
            }
        }
        return null;
    }
}

MD5Util 工具类

package util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;

public class MD5Util {

	/**
	 * 编码,将字节数组转成可识别字符串
	 * @param b
	 * @return
	 */
	private static String byteArrayToHexString(byte b[]) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
			resultSb.append(byteToHexString(b[i]));

		return resultSb.toString();
	}

	/**
	 * 将自己转成可识别字符串
	 * @param b
	 * @return
	 */
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n += 256;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 获取指定内容的 MD5值
	 * @param origin 被转换的内容
	 * @param charsetname 字符集
	 * @return
	 */
	public static String MD5Encode(String origin, String charsetname) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			if (charsetname == null || "".equals(charsetname))
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes()));
			else
				resultString = byteArrayToHexString(md.digest(resultString
						.getBytes(charsetname)));
		} catch (Exception exception) {
		}
		return resultString;
	}

	private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5","6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

	public static String UrlEncode(String src)  throws UnsupportedEncodingException {
		return URLEncoder.encode(src, "UTF-8").replace("+", "%20");
	}

}

PayUtil 工具类

package util;

import java.util.*;

public class PayUtil {

    /**
     * 统一下单,获取二维码字符串
     * @param order_price 价格
     * @param body 商品描述
     * @param out_trade_no 订单号
     * @return
     * @throws Exception
     */
    public static String getPayURL( String body,String out_trade_no,String total_fee) throws Exception {
        // 账号信息
        String appid = PayConfig.APP_ID;  //appid
        String mch_id = PayConfig.MCH_ID; //商业号
        String key = PayConfig.API_KEY;   //API密钥
        
        String nonce_str = RandomUtil.getRandomString(20); //随即字符串,官方文档要求长度在32位以内

        String spbill_create_ip = PayConfig.CREATE_IP;  //获取发起电脑 ip
       
        String notify_url = PayConfig.NOTIFY_URL;  //回调接口
        
        String trade_type = "NATIVE";  //支付类型

        SortedMap packageParams = new TreeMap();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("body", body);
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", total_fee);
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);

        String sign = SignUtil.createSign(packageParams,key);
        packageParams.put("sign", sign);

        String requestXML = XMLUtil.getRequestXml(packageParams);
        System.out.println(requestXML);
        
        System.out.println("--------------------");
        
        String resXml = HttpUtil.postData(PayConfig.UFDOOER_URL, requestXML);

        System.out.println(resXml);
        
        Map map = XMLUtil.doXMLParse(resXml);
       
        String urlCode = (String) map.get("code_url");

        return urlCode;
    }
    
    
}

RandomUtil 工具类

package util;

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

public class RandomUtil {

	/**
	 * 主要用于生成随机字符(nonce_str)
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length){  
		Random random = new Random();  

		StringBuffer sb = new StringBuffer();  

		for(int i = 0; i < length; i++){  
			int number = random.nextInt(3);  
			long result = 0;  
			switch(number){  
			case 0:  
				result = Math.round(Math.random() * 25 + 65);  
				sb.append(String.valueOf((char)result));  
				break;  
			case 1:  
				result = Math.round(Math.random() * 25 + 97);  
				sb.append(String.valueOf((char)result));  
				break;  
			case 2:  
				sb.append(String.valueOf(new Random().nextInt(10)));  
				break;  
			}  
		}  

		return sb.toString();     
	}  

	
	/**
	 * 获取当前时间
	 * @return
	 */
	 public static String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}
	 
	 
	 /**
	  * 用于生成订单号
	  * 使用 (随机字符串+当前时间) 拼接而成的字符串,作为订单号,
	  * 哪怕是同一时间生成的订单,订单号也不会相同。
	  * @return
	  */
	 public static String createOrderId(int length){
		String out_trade_no = getRandomString(length)+getCurrTime();
		return out_trade_no;
	 }
	 

}

SignUtil 工具类

package util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

public class SignUtil {
	
	/**
	 * 根据packageParams和API_KEY生成签名
	 * @param packageParams
	 * @param API_KEY (API密钥)
	 * @return
	 */
	public static String createSign(SortedMap packageParams, String API_KEY){
		 StringBuffer sb = new StringBuffer();
	        Set es = packageParams.entrySet();
	        Iterator it = es.iterator();
	        while (it.hasNext()) {
	            Map.Entry entry = (Map.Entry) it.next();
	            String k = (String) entry.getKey();
	            String v = (String) entry.getValue();
	            if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
	                sb.append(k + "=" + v + "&");
	            }
	        }
	        sb.append("key=" + API_KEY);
	        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
	        return sign;
	}
	
	
	
	 /**
     * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
     * @return boolean
     */
    public static boolean isTenpaySign( SortedMap packageParams, String API_KEY) {
        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            String v = (String)entry.getValue();
            if(!"sign".equals(k) && null != v && !"".equals(v)) {
                sb.append(k + "=" + v + "&");
            }
        }

        sb.append("key=" + API_KEY);

        //算出摘要
        String mysign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toLowerCase();
        String tenpaySign = ((String)packageParams.get("sign")).toLowerCase();

        return tenpaySign.equals(mysign);
    }

}

XMLUtil 工具类

package util;



import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;


//生成XML和解析 XML
 
public class XMLUtil {
	
	 /**
     * @Description:将请求参数转换为xml格式的string
     * @param parameters 请求参数
     * @return
     */
    public static String getRequestXml(SortedMap parameters) {
        StringBuffer sb = new StringBuffer();
        sb.append("");
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
                sb.append("<" + k + ">" + "");
            } else {
                sb.append("<" + k + ">" + v + "");
            }
        }
        sb.append("");
        return sb.toString();
    }
	
	
	
    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

        if(null == strxml || "".equals(strxml)) {
            return null;
        }

        Map m = new HashMap();

        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if(children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }

            m.put(k, v);
        }
        //关闭流
        in.close();

        return m;
    }

    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if(!children.isEmpty()) {
            Iterator it = children.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if(!list.isEmpty()) {
                    sb.append(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("");
            }
        }
        return sb.toString();
    }
    
    
}

ZxingUtil 工具类

package util;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;


public class ZxingUtil {
    /**
     * Zxing图形码生成工具
     *
     * @param contents        : 内容
     * @param format          : 图片格式,可选[png,jpg,bmp]
     * @param width           : 宽
     * @param height          : 高
     * @param saveImgFilePath : 存储图片的完整位置,包含文件名
     * @return
     */
    public static Boolean encode(String contents, String format, int width, int height, String saveImgFilePath) {
        Boolean bool = false;
        BufferedImage image = createImage(contents,width,height);
        if (image != null) {
            bool = writeToFile(image, format, saveImgFilePath);
        }
        return bool;
    }

    public static void encode(String contents, int width, int height) {
        createImage(contents,width, height);
    }

    public static BufferedImage createImage(String contents ,int width, int height) {
        BufferedImage bufImg=null;
        Map hints = new HashMap();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 10);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            // contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
            MatrixToImageConfig config = new MatrixToImageConfig(0xFF000001, 0xFFFFFFFF);
            bufImg = MatrixToImageWriter.toBufferedImage(bitMatrix, config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bufImg;
    }


    /**
     * 将BufferedImage对象写入文件
     *
     * @param bufImg
     *            BufferedImage对象
     * @param format
     *            图片格式,可选[png,jpg,bmp]
     * @param saveImgFilePath
     *            存储图片的完整位置,包含文件名
     * @return
     */
    @SuppressWarnings("finally")
    public static Boolean writeToFile(BufferedImage bufImg, String format, String saveImgFilePath) {
        Boolean bool = false;
        try {
            bool = ImageIO.write(bufImg, format, new File(saveImgFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return bool;
        }
    }

}

 

你可能感兴趣的:(JAVA学习笔记,javaweb)