微信退款SpringBoot读取resource下的证书

近期做了一个微信支付退款接口,需要证书双向验证,测试的时候证书暂时放在resource下,上图

起初MyConfig中我是这样,在本机IDE中运行没有问题

import org.springframework.util.ResourceUtils; 
 
public MyConfig() throws Exception {
        File file = (ResourceUtils.getFile("classpath:config/apiclient_cert.p12"));
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

但到Linux服务器的docker中运行就IO异常了,查阅资料可能原因是内嵌web容器访问的是jar包,

解决方法1:

import org.apache.commons.io.IOUtils;
 
public InputStream getCertStream() {
        try {
            InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config/apiclient_cert.p12");
            this.certData = IOUtils.toByteArray(certStream);
            certStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

解决方法2:

org.springframework.core.io.ClassPathResource;
 
public InputStream getCertStream() {
          try {
            ClassPathResource classPathResource = new ClassPathResource("config/apiclient_cert.p12");
            //获取文件流
            InputStream certStream = classPathResource.getInputStream();
            this.certData = IOUtils.toByteArray(certStream);
            certStream.read(this.certData);
            certStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

 


来源于:

https://blog.csdn.net/xiaoxudong666/article/details/82192425

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小程序微信支付在Spring Boot中的实现可以参考以下步骤: 1. 开发前提:需要获取微信支付商户号、小程序的AppID以及商户密钥等信息。 2. 配置支付回调地址:支付回调地址是微信支付服务器返回给用户支付信息的地址,可以根据需要进行配置。 3. 签约微信支付产品:根据实际需求,可以选择签约不同的微信支付产品,例如JSAPI支付、Native支付、小程序支付、付款码支付、刷脸支付等。 4. 使用Spring Boot进行开发:可以使用Spring Boot的相关框架和工具,如Spring MVC、RestTemplate等,来实现与微信支付接口的交互。 5. 配置微信支付接口信息:在应用的配置文件中,配置微信支付接口的相关信息,包括商户号、AppID、商户密钥等。 6. 实现支付功能:根据微信支付接口文档,使用相关的API和参数,编写代码实现小程序微信支付功能。 以上是一个简单的步骤示例,具体的实现细节还需要根据具体的场景和需求来进行调整。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot 小程序微信支付](https://blog.csdn.net/weixin_40918145/article/details/128339151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring Boot中的微信支付(小程序)](https://blog.csdn.net/weixin_43248862/article/details/124921202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值