springboot实现支付宝支付案例

做了一个测试案例,用的沙箱测试账号
可以去支付宝官网注册一个开发者账号,获取沙箱账号,地址: https://openhome.alipay.com/platform/appDaily.htm?tab=info
注册完之后会看到以下画面:

支付宝支付沙箱应用

支付宝支付沙箱账号

上面两个图中,我们可以看到有这个几个有用的信息:
1.APPID
2.支付宝网关
3.商家账号 登录密码
4.买家账号 登录密码 支付密码

接下来,我们在沙箱应用里要设置一下RSA2公钥
首先下载生成公钥和私钥的工具,地址:https://opendocs.alipay.com/open/291/105971#LDsXr

支付宝支付公钥生成工具

如上图,只有windows版和macOs版,用linux的心疼一秒钟,还好我有windows虚拟机。
支付宝支付公钥生成

如上图,安装之后,点击生成秘钥即可生成公钥和私钥,复制保存好这个公钥和私钥
接下来更改沙箱应用中RSA2的公钥,把刚刚生成的公钥复制到在线网站上保存。
支付宝支付保存公钥到在线网站

到现在为止,准备工作已经做完了。

在springboot上创建一个AlipayConfig类

package com.zhaohy.app.config;

import java.io.FileWriter;
import java.io.IOException;

public class AlipayConfig {
    // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
    public static String APP_ID = "";

    // 商户私钥,您的PKCS8格式RSA2私钥,这些就是我们刚才设置的
    public static String RSA_PRIVATE_KEY = "";

    // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm
    // 对应APPID下的支付宝公钥。,这些就是我们刚才设置的
    public static String ALIPAY_PUBLIC_KEY = "";

    // 异步通知,再这里我们设计自己的后台代码
    public static String notify_url = "http://www.zhaohy.org.cn";

    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    public static String return_url = "http://www.zhaohy.org.cn";

    // 签名方式
    public static String SIGN_TYPE = "RSA2";

    // 字符编码格式
    public static String CHARSET = "utf-8";

    // 支付宝网关 https://openapi.alipaydev.com/gateway.do https://openapi.alipay.com/gateway.do
    public static String GATEWAYURL = "https://openapi.alipaydev.com/gateway.do";

    // 支付宝网关
    public static String LOG_PATH = "/home/zhaohy/alipayLog/";
    
    public static String FORMAT = "json";

    // ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /**
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * 
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(LOG_PATH + "alipay_log_" + System.currentTimeMillis() + ".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

把之前的appid,公钥,私钥和网关地址都填在上面代码里

新建AlipayService类

package com.zhaohy.app.service;

import com.alipay.api.AlipayApiException;

public interface AlipayService {
    /**
     * web端订单支付
     * 
     * @param outTradeNo  订单编号(唯一)
     * @param totalAmount 订单价格
     * @param subject     商品名称
     */
    String webPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception;

    /**
     * app端订单支付
     * 
     * @param outTradeNo  订单编号
     * @param totalAmount 订单价格
     * @param subject     商品名称
     */
    String appPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception;

    /**
     * 退款
     * 
     * @param outTradeNo   订单编号
     * @param refundReason 退款原因
     * @param refundAmount 退款金额
     * @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
     */
    String refund(String outTradeNo, String refundReason, Integer refundAmount, String outRequestNo)
            throws AlipayApiException;

    /**
     * 交易查询
     * 
     * @param outTradeNo 订单编号(唯一)
     */
    String query(String outTradeNo) throws AlipayApiException;

    /**
     * 交易关闭
     * 
     * @param outTradeNo订单编号(唯一)
     */
    String close(String outTradeNo) throws AlipayApiException;

    /**
     * 退款查询
     * 
     * @param outTradeNo   订单编号(唯一)
     * @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
     */
    String refundQuery(String outTradeNo, String outRequestNo) throws AlipayApiException;
}

新建AlipayServiceImpl实现类

package com.zhaohy.app.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.zhaohy.app.config.AlipayConfig;
import com.zhaohy.app.service.AlipayService;
import com.zhaohy.app.utils.AppFrameworkUtil;
@Service("AlipayService")
public class AlipayServiceImpl implements AlipayService {

    /** 调取支付宝接口 web端支付 */
    DefaultAlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAYURL, AlipayConfig.APP_ID,
            AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
            AlipayConfig.SIGN_TYPE);

    /** 调取支付宝接口app端支付 */
    AlipayClient alipayClients = new DefaultAlipayClient(AlipayConfig.GATEWAYURL, AlipayConfig.APP_ID,
            AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
            AlipayConfig.SIGN_TYPE);

    /**
     * web端订单支付
     * 
     * @param outTradeNo  订单编号(唯一)
     * @param totalAmount 订单价格
     * @param subject     商品名称
     */
    @Override
    public String webPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception {

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        /** 同步通知,支付完成后,支付成功页面 */
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        /** 异步通知,支付完成后,需要进行的异步处理 */
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        Map bizContentMap = new HashMap();
        bizContentMap.put("out_trade_no", outTradeNo);
        bizContentMap.put("total_amount", totalAmount + "");
        bizContentMap.put("subject", subject);
        bizContentMap.put("body", "商品名称");
        bizContentMap.put("timeout_express", "90m");
        bizContentMap.put("product_code", "FAST_INSTANT_TRADE_PAY");
        String bizContentJson = AppFrameworkUtil.structureConfigParamsGroupJSONDataWithOutSimpleResultBean(bizContentMap);
        alipayRequest.setBizContent(bizContentJson);

        /** 转换格式 */
        String result = alipayClient.pageExecute(alipayRequest).getBody().replace('\"', '\'').replace('\n', ' ');
        return result;
    }

    /**
     * 退款
     * 
     * @param outTradeNo   订单编号
     * @param refundReason 退款原因
     * @param refundAmount 退款金额
     * @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
     */
    @Override
    public String refund(String outTradeNo, String refundReason, Integer refundAmount, String outRequestNo)
            throws AlipayApiException {
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        /** 调取接口 */
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"refund_amount\":\"" + refundAmount
                + "\"," + "\"refund_reason\":\"" + refundReason + "\"," + "\"out_request_no\":\"" + outRequestNo
                + "\"}");
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    /**
     * 交易查询
     * 
     * @param outTradeNo 订单编号(唯一)
     */
    @Override
    public String query(String outTradeNo) throws AlipayApiException {
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
        /** 请求接口 */
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"trade_no\":\"" + "" + "\"}");
        /** 转换格式 */
        String result = alipayClient.execute(alipayRequest).getBody();
        return result;
    }

    /**
     * 交易关闭
     * 
     * @param outTradeNo订单编号(唯一)
     */
    @Override
    public String close(String outTradeNo) throws AlipayApiException {
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"trade_no\":\"" + "" + "\"}");

        String result = alipayClient.execute(alipayRequest).getBody();

        return result;
    }

    /**
     * 退款查询
     * 
     * @param outTradeNo   订单编号(唯一)
     * @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
     */
    @Override
    public String refundQuery(String outTradeNo, String outRequestNo) throws AlipayApiException {
        AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();

        /** 请求接口 */
        alipayRequest.setBizContent(
                "{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"out_request_no\":\"" + outRequestNo + "\"}");

        /** 格式转换 */
        String result = alipayClient.execute(alipayRequest).getBody();

        return result;
    }

    /**
     * app端订单支付
     * 
     * @param outTradeNo  订单编号
     * @param totalAmount 订单价格
     * @param subject     商品名称
     */
    @Override
    public String appPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception {
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();

        /** 同步通知,支付完成后,支付成功页面 */
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        /** 异步通知,支付完成后,需要进行的异步处理 */
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        /** 销售产品码(固定) */
        String productCode = "QUICK_WAP_WAY";

        /** 进行赋值 */
        AlipayTradeWapPayModel wapPayModel = new AlipayTradeWapPayModel();
        wapPayModel.setOutTradeNo(outTradeNo);
        wapPayModel.setSubject(subject);
        wapPayModel.setTotalAmount(totalAmount.toString());
        wapPayModel.setBody("商品名称");
        wapPayModel.setTimeoutExpress("2m");
        wapPayModel.setProductCode(productCode);
        alipayRequest.setBizModel(wapPayModel);

        /** 格式转换 */
        String result = alipayClients.pageExecute(alipayRequest).getBody().replace('\"', '\'').replace('\n', ' ');
        return result;
    }

}

新建AlipayController

package com.zhaohy.app.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.zhaohy.app.service.AlipayService;
import com.zhaohy.app.utils.AppFrameworkUtil;
@Controller
public class AlipayController {
    @Autowired
    private AlipayService alipayService;
    
    @RequestMapping("/alipayTest.do")
    public String test(HttpServletRequest request) {
        request.setAttribute("ctx", AppFrameworkUtil.getWebRootUrl(request, ""));
        return "html/alipay";
    }

    /**
     * web 订单支付
     */
    @RequestMapping("/alipay/getPagePay")
    public void getPagePay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /** 模仿数据库,从后台调数据 */
        String outTradeNo = "19960310621211";
        Integer totalAmount = 1;
        String subject = "苹果28";

        String pay = alipayService.webPagePay(outTradeNo, totalAmount, subject);
        System.out.println(pay);

        Map resultMap = new HashMap<>();
        resultMap.put("data", pay);

        AppFrameworkUtil.renderJSON(response, AppFrameworkUtil.structureConfigParamsGroupJSONData(resultMap));
    }

    /**
     * app 订单支付
     */
    @RequestMapping("/alipay/getAppPagePay")
    public void getAppPagePay(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /** 模仿数据库,从后台调数据 */
        String outTradeNo = "1312332";
        Integer totalAmount = 1000;
        String subject = "天猫超市014";

        String pay = alipayService.appPagePay(outTradeNo, totalAmount, subject);

        System.out.println(pay);
        Map resultMap = new HashMap<>();
        resultMap.put("data", pay);
        AppFrameworkUtil.renderJSON(response, AppFrameworkUtil.structureConfigParamsGroupJSONData(resultMap));

    }

    /**
     * 交易查询
     */
    @RequestMapping("/alipay/aipayQuery")
    public void alipayQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /** 调取支付订单号 */
        String outTradeNo = "13123";

        String query = alipayService.query(outTradeNo);

        //Object json = JSONObject.toJSON(query);

        /*
         * JSONObject jObject = new JSONObject(); jObject.get(query);
         */
        AppFrameworkUtil.renderJSON(response, query);
    }

    /**
     * 退款
     * 
     * @throws AlipayApiException
     */
    @RequestMapping("/alipay/alipayRefund")
    public void alipayRefund(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException {
        String outTradeNo = request.getParameter("outTradeNo");
        String outRequestNo = request.getParameter("outRequestNo");
        String refundAmount = request.getParameter("refundAmount");
        /** 调取数据 */
        // String outTradeNo = "15382028806591197";
        String refundReason = "用户不想购买";
        // refundAmount = 1;
        // outRequestNo = "22";

        String refund = alipayService.refund(outTradeNo, refundReason, Integer.parseInt(refundAmount), outRequestNo);

        System.out.println(refund);

        AppFrameworkUtil.renderJSON(response, refund);
    }

    /**
     * 退款查询
     * 
     * @throws AlipayApiException
     */
    @RequestMapping("/alipay/refundQuery")
    public void refundQuery(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException {

        /** 调取数据 */
        String outTradeNo = "13123";
        String outRequestNo = "2";

        String refund = alipayService.refundQuery(outTradeNo, outRequestNo);

        AppFrameworkUtil.renderJSON(response, refund);

    }

    /**
     * 交易关闭
     * 
     * @throws AlipayApiException
     */
    @RequestMapping("/alipay/alipayclose")
    public void alipaycolse(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException {

        /** 调取数据 */
        String outTradeNo = "13123";

        String close = alipayService.close(outTradeNo);

        AppFrameworkUtil.renderJSON(response, close);
    }
}

controller里getAppPagePay是app订单支付,里面写死的是测试数据,如果正式用,可以当参数传进来。
演示app 订单支付,因为app订单支付会跳转到支付宝进行登录支付,如果没有监测到支付宝应用,则用网页版的支付宝进行支付,会比较方便。
调用上面的/alipayTest.do接口会跳转到html/alipay.html页面
alipay.html代码如下:





Insert title here






    

可以看到,一进来这个页面会请求app订单支付接口,那个接口会返回一个form表单,并提交给支付宝进行支付,那个form表单的html放到#alipay div中便会自动执行跳转.

启动项目,访问http://127.0.0.1:8082/alipayTest.do

支付宝支付跳转页面

如图,电脑访问跳转到了上图页面,点击继续浏览器付款,登录沙箱买家账号,输入支付密码即可
支付宝支付付款页面

点击确认付款,输入密码
image

几秒钟后会跳转回AlipayConfig里面设置的http:www.zhaohy.org.cn
支付宝支付跳转回页面

至此,整个支付过程已经完成~

=====2021年7月19日更新=====

补充一下AppFrameworkUtil工具类

package com.zhaohy.app.utils;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhaohy.app.service.UserService;

import flexjson.JSONSerializer;
import nl.bitwalker.useragentutils.UserAgent;
public class AppFrameworkUtil {
    /**
     * 四舍五入。保留num个小数点后的数字
     * 
     * @param bh
     * @param num
     * @return
     */
    public static String getNum(double bh, int num) {
        String numstr = "";
        numstr = new BigDecimal(bh).setScale(num, BigDecimal.ROUND_HALF_UP) + "";
        return numstr;
    }

    /**
     * 去替换横杠
     * 
     * @param str
     * @param t_str
     * @return
     */
    public static String getReplaseStr(String str) {
        String t_str = null;
        if (str != null) {
            t_str = str.replace("-", "");
        }
        return t_str;
    }

    /**
     * 四舍五入。保留num个小数点后的数字
     * 
     * @param bh
     * @param num
     * @return
     */
    public static String getNum(String bh, int num) {

        if (AppFrameworkUtil.isEmpty(bh) || "-".equals(bh))
            return "-";
        if ("0.0".equals(bh) || "0".equals(bh))
            return bh;
        String numstr = "";
        numstr = new BigDecimal(Double.parseDouble(bh)).setScale(num, BigDecimal.ROUND_HALF_UP) + "";
        return numstr;
    }

    /**
     * 字符串空判断
     * 
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        boolean flag = true;
        if (null != str && 0 != str.trim().length())
            flag = false;
        return flag;
    }

    /**
     * 添加符号
     * 
     * @param str
     */
    public static String addSign(String str) {
        if (!AppFrameworkUtil.isEmpty(str) && !str.contains("-")) {
            str = "+" + str;
        }
        return str;
    }

    /**
     * 添加千分位
     * 
     * @param str
     * @return
     */
    public static String format(String str) {
        if (!isEmpty(str) && !"-".equals(str)) {
            DecimalFormat df = new DecimalFormat("###,###");
            try {
                str = df.format(Long.parseLong(str));
            } catch (Exception e) {
                return str;
            }
        }
        return str;
    }

    /**
     * response JSON数据
     * 
     * @param response
     * @param context
     */
    public static void renderJSON(HttpServletResponse response, String context) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            out = response.getWriter();
            out.write(context);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    /**
     * 获取外网IP
     * 
     * @param request
     * @return
     */
    public static String getRemoteHost(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    public static String getSystemDate() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String curDate = s.format(calendar.getTime()); // 当前日期
        return curDate;
    }

    /**
     * 
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        return (str == null || str.trim().length() == 0 || str.equals("undefined") || str.equals("null"));
    }

    /**
     * 序列化集合成JSON字符
     * @param list
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String structureConfigParamsGroupJSONData(Object obj) {
        JSONSerializer serializer = new JSONSerializer();
        String json = "";
        @SuppressWarnings("rawtypes")
        SimpleResultBean result = new SimpleResultBean();
        result.setData(obj);
        json = serializer.exclude("*.class").deepSerialize(result).replaceAll(":\\s*null\\s*", ":\"\"");
        return json;
    }
    
    public static String structureConfigParamsGroupJSONDataWithOutSimpleResultBean(Object obj) {
        JSONSerializer serializer = new JSONSerializer();
        String json = "";
        json = serializer.exclude("*.class").deepSerialize(obj).replaceAll(":\\s*null\\s*", ":\"\"");
        return json;
    }

    /**
     * 返回jsonObj参数
     * 
     * @param request
     * @return
     */
    public static JSONObject getRequestParamsObj(HttpServletRequest request) {
        JSONObject paramsObj = new JSONObject();
        try {
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            paramsObj = JSONObject.parseObject(responseStrBuilder.toString());

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

    /**
     * 判断是否是ajax请求
     * 
     * @param request
     * @return
     */
    public static boolean isAjax(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 
     * 获取用户mac地址
     * 
     * @param ip
     * @return
     */
    public static String getMacAddrByIp(String ip) {
        String macAddr = null;
        try {
            String command = "nbtstat -a ";
            if (!System.getProperty("os.name").contains("Windows")) {
                command = "netstat -a ";
            }
            Process process = Runtime.getRuntime().exec(command + ip);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            Pattern pattern = Pattern.compile("([A-F0-9]{2}-){5}[A-F0-9]{2}");
            Matcher matcher;
            for (String strLine = br.readLine(); strLine != null; strLine = br.readLine()) {
                matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    macAddr = matcher.group();
                    break;
                }
            }
        } catch (IOException e) {
            // e.printStackTrace();
        }
        return macAddr;
    }

    /**
     * 获取系统os名称
     * 
     * @param request
     * @return
     */
    public static String getOSName(HttpServletRequest request) {
        String osName = null;
        try {
            String userAgentStr = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
            osName = userAgent.getOperatingSystem().getName();
        } catch (Exception e) {

        }

        return osName;
    }

    /**
     * 获取请求系统os名称
     * 
     * @param request
     * @return
     */
    public static Object getOSVersion(HttpServletRequest request) {
        Object osVersion = null;
        try {
            String userAgentStr = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
            osVersion = userAgent.getOperatingSystem().getId();
        } catch (Exception e) {

        }
        return osVersion;
    }

    /**
     * 获取请求浏览器名称
     * 
     * @param request
     * @return
     */
    public static String getBrowserName(HttpServletRequest request) {
        String name = null;
        try {
            String userAgentStr = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
            name = userAgent.getBrowser().getName();
        } catch (Exception e) {

        }
        return name;
    }

    /**
     * 获取请求浏览器版本
     * 
     * @param request
     * @return
     */
    public static String getBrowserVersion(HttpServletRequest request) {
        String version = null;
        try {
            String userAgentStr = request.getHeader("User-Agent");
            UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
            version = userAgent.getBrowserVersion().getVersion();
        } catch (Exception e) {

        }
        return version;
    }

    public static String getDevTypeById(String appType) {
        String name = "";
        if ("1".equals(appType)) {
            name = "IPAD";
        } else if ("5".equals(appType)) {
            name = "IPHONE";
        } else if ("6".equals(appType)) {
            name = "ANDROID";
        }
        return name;
    }

    public static String formatLanguageType(String languageType) {
        String value = "zh";
        if (!AppFrameworkUtil.isBlank(languageType)) {
            if ("ja".equals(languageType)) {
                value = "ja";
            }
        }
        return value;
    }

    /**
     * txt格式转String
     * 
     * @param txtPath
     * @return
     * @throws IOException
     */
    public static String txtToStr(String txtPath) throws IOException {
        StringBuffer buffer = new StringBuffer();
        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(txtPath), "UTF-8"));
        String str = null;
        while ((str = bf.readLine()) != null) {// 使用readLine方法,一次读一行
            buffer.append(new String(str.getBytes(), "UTF-8"));
        }

        String xml = buffer.toString();

        return xml;
    }

    public static boolean booleanUtf8(String txtPath) {
        boolean flag = false;
        // 原本这里的是gb2312,为了兼容更多,选择了gbk
        String code = "gbk";
        byte[] head = new byte[3];
        try {
            File file = new File(txtPath);
            InputStream inputStream = new FileInputStream(file);
            inputStream.read(head);
            if (head[0] == -1 && head[1] == -2) {
                code = "UTF-16";
            } else if (head[0] == -2 && head[1] == -1) {
                code = "Unicode";
            } else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
                code = "UTF-8";
            }
            inputStream.close();

            //System.out.println(code);
            if (code.equals("UTF-8")) {
                flag = true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean booleanUtf8(File file) {
        boolean flag = false;
        // 原本这里的是gb2312,为了兼容更多,选择了gbk
        String code = "gbk";
        byte[] head = new byte[3];
        try {
            InputStream inputStream = new FileInputStream(file);
            inputStream.read(head);
            if (head[0] == -1 && head[1] == -2) {
                code = "UTF-16";
            } else if (head[0] == -2 && head[1] == -1) {
                code = "Unicode";
            } else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
                code = "UTF-8";
            }
            inputStream.close();

            //System.out.println(code);
            if (code.equals("UTF-8")) {
                flag = true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static boolean upLoadFile(File file, String pdfPath) {
        boolean flag = false;
        File dirFile = new File(pdfPath);
        try {
            if (!dirFile.exists()) {
                dirFile.createNewFile();
            }
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        }

        InputStream input = null;
        FileOutputStream output = null;
        try {
            input = new FileInputStream(file);
            output = new FileOutputStream(dirFile);
            byte[] bt = new byte[1024 * 1024 * 200];// 最大200m
            int len = 0;
            while ((len = input.read(bt)) != -1) {
                output.write(bt, 0, len);
            }

            flag = true;
            input.close();
            output.flush();
            output.close();
        } catch (IOException e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 分页查询
     * 
     * @param request
     * @param paramsMap
     * @param list
     * @param sqlSession
     * @param string
     * @return
     */
    public static List> pagedQuery(HttpServletRequest request, Map paramsMap,
            List> list, SqlSession sqlSession, String queryPosition) {
        int pageIndex = Integer.parseInt(paramsMap.get("pageIndex").toString());// 当前页
        int pageIndexSize = Integer.parseInt(paramsMap.get("pageIndexSize").toString());// 一页条数
        int beginNum = pageIndex * pageIndexSize - pageIndexSize;
        int endNum = pageIndex * pageIndexSize;
        int totalNum = 0;// 总条数
        int pageCount = 0;// 总页数
        paramsMap.put("beginNum", beginNum);
        paramsMap.put("endNum", endNum);
        list = new ArrayList>();
        list = sqlSession.selectList(queryPosition, paramsMap);
        if (list.size() > 0) {
            totalNum = Integer.parseInt(list.get(0).get("TOTALNUM").toString());
            if (totalNum > pageIndexSize) {
                if (((double) totalNum / (double) pageIndexSize) > (double) (totalNum / pageIndexSize)) {
                    pageCount = totalNum / pageIndexSize + 1;
                } else {
                    pageCount = totalNum / pageIndexSize;
                }
            } else {
                pageCount = 1;
            }
        }
        paramsMap.put("pageCount", pageCount);// 总页数
        paramsMap.put("totalNum", totalNum);// 总条数
        return list;
    }

    public static void downloadFile(File file, HttpServletRequest request, HttpServletResponse response,
            String fileName, String fileType) {
        boolean success = false;
        /* 设置文件ContentType类型,这样设置,会自动判断下载文件类型 */
        response.setContentType("multipart/form-data");
        // response.setContentType("application/OCTET-STREAM;charset=UTF-8");
        /* 设置文件头:最后一个参数是设置下载文件名 */

        try {
            String formFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + formFileName + "." + fileType);

            InputStream fis = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024 * 1024 * 200];
            int len;
            while ((len = fis.read(b)) > 0) {
                os.write(b, 0, len);
            }
            os.flush();
            os.close();
            fis.close();

            success = true;
        } catch (Exception e) {
            success = false;
            e.printStackTrace();
        }

    }

    // 处理导出的excel文件中的空格变成+号的问题
    public static String encodeExcelName(String excelName) {
        try {
            excelName = URLEncoder.encode(excelName, "utf-8");
            excelName = excelName.replace("+", "%20");
        } catch (final IOException e) {
            e.printStackTrace();
            // throw e;
        } finally {
        }
        return excelName;
    }

    /**
     * 参数加引号处理
     * 
     * @param value
     * @return
     */
    public static String addQuotesToParameter(String ids) {
        if (!AppFrameworkUtil.isBlank(ids)) {
            String[] idsArr = ids.split(",");
            if (idsArr.length > 0) {
                StringBuffer idBuffer = new StringBuffer();
                for (int i = 0; i < idsArr.length; i++) {
                    idBuffer.append("'" + idsArr[i] + "',");
                }
                ids = idBuffer.toString().substring(0, idBuffer.toString().length() - 1);
            }
        }
        return ids;
    }

    /**
     * 把string字符串写入文件
     * @param html
     * @param htmlPath
     * @param string
     * @return
     */
    public static boolean writeStrToFile(String str, String path, String fileName) {
        boolean success = false;
        File dir = new File(path);
        if(!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(path + fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       // byte bytes[] = new byte[1024 * 1024 * 200];
       // bytes = str.getBytes();
       // int b = bytes.length; // 是字节的长度,不是字符串的长度
       // FileOutputStream fos;
     // 获取该文件的缓冲输出流
        BufferedWriter bufferedWriter = null;
        // 写入信息
        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bufferedWriter.write(str);
            bufferedWriter.flush();// 清空缓冲区
            bufferedWriter.close();// 关闭输出流
            success = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        /*
         * try {
         * fos = new FileOutputStream(file);
         * fos.write(bytes);
         * fos.flush();
         * fos.close();
         * success = true;
         * } catch (Exception e) {
         * // TODO Auto-generated catch block
         * e.printStackTrace();
         * }
         */
        return success;
    }

    /**
     * 前端访问路径
     * @param request
     * @param path
     * @return
     */
    public static String getCommonUrl(HttpServletRequest request, String path) {
//        String scheme = request.getScheme();//http
//        String serverName = request.getServerName();//localhost
//        int serverPort = request.getServerPort();//8080
//        String contextPath = request.getContextPath();//项目名
//        String commonUrl = scheme+"://"+serverName+":"+serverPort+contextPath + "/" + PropertiesUtils.getWEB_STATICFILE_URL() + "/" + path;
        String commonUrl = PropertiesUtils.getCOMMON_URL() + PropertiesUtils.getWEB_STATICFILE_URL() + "/" + path;
        return commonUrl;
    }
    
    public static String getWebRootUrl(HttpServletRequest request, String path) {
//        String scheme = request.getScheme();//http
//        String serverName = request.getServerName();//localhost
//        int serverPort = request.getServerPort();//8080
//        String contextPath = request.getContextPath();//项目名
//        String commonUrl = scheme+"://"+serverName+":"+serverPort+contextPath + "/" + path;
        String commonUrl = PropertiesUtils.getCOMMON_URL() + path;
        return commonUrl;
    }

    /**
     * 过滤文件名特殊字符
     * @param string
     * @return
     */
    public static String getFileNameFilterSpecialCharacters(String fileName) {
        if(!AppFrameworkUtil.isBlank(fileName)) {
            fileName = fileName.replace("\\", "").replace("/", "")
            .replace(":", "").replace("*", "").replace("?", "").replace("\"", "").replace("<", "")
            .replace(">", "").replace("|", "");
        }
        return fileName;
    }

    public static String formateDateType(HttpServletRequest request, String dateType) {
        String requestType = null == request.getParameter("requestType") ? "pc" : request.getParameter("requestType");
        if("app".equals(requestType)) {
            dateType = null == dateType ? "" : dateType;
        } else {
            dateType = null == dateType ? "4" : dateType;
        }
        return dateType;
    }

    /**
     * 店选中id勾选
     * @param request
     * @param paramsMap
     * @param checkIds
     * @param json 
     * @return
     */
    public static String getDealerBoxCheck(HttpServletRequest request, Map paramsMap, String checkIds, String json) {
        if(!AppFrameworkUtil.isBlank(checkIds)) {
            String[] checkIdsArr = checkIds.split(",");
            JSONObject jsonObj = JSONObject.parseObject(json);
            JSONObject dataObj = jsonObj.getJSONObject("data");
            JSONArray dataArr = dataObj.getJSONArray("data");
            
            for(int i = 0; i < dataArr.size(); i++) {
                JSONObject dataObj1 = dataArr.getJSONObject(i);
                JSONArray childrenArr = dataObj1.getJSONArray("children");
                for(int j = 0; j < childrenArr.size(); j++) {
                    JSONObject childrenObj = childrenArr.getJSONObject(j);
                    JSONArray childrenArr1 = childrenObj.getJSONArray("children");
                    for(int k = 0; k < childrenArr1.size(); k++) {
                        JSONObject childrenObj1 = childrenArr1.getJSONObject(k);
                        JSONArray childrenArr2 = childrenObj1.getJSONArray("children");
                        for(int l = 0; l < childrenArr2.size(); l++) {
                            JSONObject childrenObj2 = childrenArr2.getJSONObject(l);
                            childrenObj2.put("checked", false);
                        }
                        childrenObj1.put("checked", false);
                    }
                    childrenObj.put("checked", false);
                }
                dataObj1.put("checked", false);
            }
            
  first:for(int i = 0; i < dataArr.size(); i++) {
                JSONObject dataObj1 = dataArr.getJSONObject(i);
                for(int m = 0; m < checkIdsArr.length; m++) {
                    if(checkIdsArr[m].equals(dataObj1.getString("value"))) {
                        dataObj1.put("checked", true);
                        break first;
                    }
                }
                JSONArray childrenArr = dataObj1.getJSONArray("children");
                for(int j = 0; j < childrenArr.size(); j++) {
                    JSONObject childrenObj = childrenArr.getJSONObject(j);
                    for(int m = 0; m < checkIdsArr.length; m++) {
                        if(checkIdsArr[m].equals(childrenObj.getString("value"))) {
                            childrenObj.put("checked", true);
                            dataObj1.put("checked", true);
                            break first;
                        }
                    }
                    JSONArray childrenArr1 = childrenObj.getJSONArray("children");
                    for(int k = 0; k < childrenArr1.size(); k++) {
                        JSONObject childrenObj1 = childrenArr1.getJSONObject(k);
                        for(int m = 0; m < checkIdsArr.length; m++) {
                            if(checkIdsArr[m].equals(childrenObj1.getString("value"))) {
                                childrenObj1.put("checked", true);
                                childrenObj.put("checked", true);
                                dataObj1.put("checked", true);
                                break first;
                            }
                        }
                        JSONArray childrenArr2 = childrenObj1.getJSONArray("children");
                        for(int l = 0; l < childrenArr2.size(); l++) {
                            JSONObject childrenObj2 = childrenArr2.getJSONObject(l);
                            for(int m = 0; m < checkIdsArr.length; m++) {
                                if(checkIdsArr[m].equals(childrenObj2.getString("value"))) {
                                    childrenObj2.put("checked", true);
                                    childrenObj1.put("checked", true);
                                    childrenObj.put("checked", true);
                                    dataObj1.put("checked", true);
                                    break first;
                                }
                            }
                        }
                    }
                }
            }
            json = jsonObj.toJSONString();
        }
        return json;
    }

    public static String getUserId(HttpServletRequest request) {
        String userId = "";
        String token = AppFrameworkUtil.getTokenFromRequest(request);
        
        ServletContext context = request.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
        
        if(!AppFrameworkUtil.isBlank(redisTemplate.opsForValue().get(token))) {
            JSONObject userObj = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
            userId = userObj.get("USER_ID").toString();
        }
        return userId;
    }
    
    public static String getUserTypeId(HttpServletRequest request) {
        String userTypeId = "";
        String token = AppFrameworkUtil.getTokenFromRequest(request);
        
        ServletContext context = request.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
        
        if(!AppFrameworkUtil.isBlank(redisTemplate.opsForValue().get(token))) {
            JSONObject userObj = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
            userTypeId = userObj.get("USER_TYPE_ID").toString();
        }
        return userTypeId;
    }

    public static String getUserName(HttpServletRequest request) {
        String userName = "";
        String token = AppFrameworkUtil.getTokenFromRequest(request);
        
        ServletContext context = request.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
        
        if(!AppFrameworkUtil.isBlank(redisTemplate.opsForValue().get(token))) {
            JSONObject userObj = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
            userName = userObj.get("USER_NAME").toString();
        }
        return userName;
    }

    public static String getTokenFromRequest(HttpServletRequest request) {
        String token = null;
        if(null != request.getHeader("token")) {
            token = request.getHeader("token");
        }
        if(token == null) {
            token = request.getParameter("token");
        }
        
        ServletContext context = request.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
        StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
        
        if(!AppFrameworkUtil.isBlank(token)) {
            //去redis里找是否存在token
            if(AppFrameworkUtil.isBlank(redisTemplate.opsForValue().get(token))) {
                token = null;
            }
//          String userId = JWTUtil.verifyToken(token);
//          String value = null;
//          try {
//              value = PropertiesUtils.getPropertiesFromFile(AppFrameworkUtil.getLoginDateKey(userId));
//          } catch (IOException e) {
//              value = null;
//              e.printStackTrace();
//          }
//          userId = JWTUtil.verifyToken(token, value);
//          if(null == userId) {
//              return null;
//          }
//          ServletContext context = request.getServletContext();
//            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
//            UserService userService = ctx.getBean(UserService.class);
//            List> list = userService.getUserByUserId(request, userId);
//            if(list.size() > 0) {
//              request.getSession().setAttribute(PropertiesUtils.getUSERSTR(), list);
//            } else {
//              token = null;
//            }
        } else {//如果没传token进来,去看看redis里有没这个浏览器的sessionId
            String sessionId = request.getSession().getId();
            if(!AppFrameworkUtil.isBlank(redisTemplate.opsForValue().get(sessionId))) {
                token = redisTemplate.opsForValue().get(sessionId);
            }
        }
        if(AppFrameworkUtil.isBlank(token)) {
            token = "";
        }
        return token;
    }
    
    //public static void main(String[] args) {

        /*
         * List list = pdfPathToImagePaths("D://pdf/PowerPoint2013简介.pdf");
         * for (int i = 0; i < list.size(); i++) {
         * System.out.println("path==" + list.get(i));
         * }
         */

        /*
         * String userPwd = "&";
         * try {
         * userPwd =URLEncoder.encode(userPwd, "UTF-8");
         * } catch (UnsupportedEncodingException e) {
         * // TODO Auto-generated catch block
         * e.printStackTrace();
         * }
         * System.out.println(userPwd);
         */
    //}

    public static String getLoginDateKey(String userId) {
        if(!AppFrameworkUtil.isBlank(userId)) {
            return userId + "LOGIN_DATE";
        } else {
            return null;
        }
    }

    public static String getLoginDateValue(HttpServletRequest request, String userId) {
        if(!AppFrameworkUtil.isBlank(userId)) {
            return CookieUtil.getCookieByName(request, AppFrameworkUtil.getLoginDateKey(userId));
        }
        return null;
    }
}

你可能感兴趣的:(springboot实现支付宝支付案例)