美文网首页
项目根路径读取写入

项目根路径读取写入

作者: bd8915df25f2 | 来源:发表于2019-11-02 08:35 被阅读0次

前段时间做的功能,有些技术点记录下,

  1. //获取类加载的根路径
    String rootPath=this.getClass().getResource("/").getPath();
    根路径即画圈部分.png
    2.httpclient模拟表单上传附件
    需要实现的目标格式.png
    具体实现:
                HttpPost httpPost = new HttpPost(reqUrl);
                httpPost.setConfig(config);
                ContentType contentTypeUpload = ContentType.create("multipart/form-data");

                MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                multipartEntityBuilder.setBoundary("boundary--");
                multipartEntityBuilder.setContentType(contentTypeUpload);

                ContentType contentTypeMeta = ContentType.create("application/json");
                multipartEntityBuilder.addTextBody("meta",params,contentTypeMeta);
                //判断类型
                ContentType contentTypeImage = ContentType.create("image/jpeg");
                if (StringUtils.isNotBlank(fileName)){
                    String filename=fileName;
                    if (filename.endsWith(".png")) {
                        contentTypeImage = ContentType.create("image/png");
                    }else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".jpe")) {
                        contentTypeImage = ContentType.create("image/jpeg");
                    }else if (filename.endsWith(".gif")) {
                        contentTypeImage = ContentType.create("image/gif");
                    }else if (filename.endsWith(".ico")) {
                        contentTypeImage = ContentType.create("image/image/x-icon");
                    }
                }

                if (StringUtils.isNotBlank(fileName)){
                    multipartEntityBuilder.addBinaryBody("file",bytes,contentTypeImage,fileName);
                }

                HttpEntity httpEntity=multipartEntityBuilder.build();
                httpPost.addHeader("Authorization",token);
                httpPost.setHeader("Accept", "*/*");

                httpPost.setEntity(httpEntity);
                CloseableHttpResponse response = httpClient.execute(httpPost);

注:之前为了看到上传数据的结果,不小心打印了entity,只能上传很小的图片,稍大一点的上传图片结果出现了Content length is too long,找了老长时间原因。后来在源码中找到了报错原因,去掉调用此方法即可解决。


若打印entity内容有可能会抛异常.png

3.加载配置文件

   public static  PropertiesConfiguration config;
   //PostConstruct在容器启动的时候加载,且仅加载一次
   @PostConstruct
   public static void init() {
       try {
           log.info("加载配置信息--开始");
           config = new PropertiesConfiguration("bank/certificate/XXXX/certificate.properties");
           log.info("加载配置信息--完成");
       } catch (Exception e) {
           log.error("初始化基础信息失败!:",e);
       }
   }

配置文件:

#平台证书序号
plantcer_XXXXX=bank/certificate/XXXXX/wenjianjia/zhengshuwenjian.pem

在静态方法中获取本地路径

            //平台证书路径
            String relativeFilePath= WeChatCertificateConfigUtils.config.getString("plantcer_"+merchantId);
            // 读取本机存放的PKCS8证书文件
            String currentPath = Thread.currentThread().getContextClassLoader().getResource(cerPath).getPath();
            X509Certificate cert = PemUtil.loadCertificate(new FileInputStream(currentPath));

载入证书方法:

public static X509Certificate loadCertificate(InputStream inputStream) {
    try {
      log.info("---loadCertificate-----");
      CertificateFactory cf = CertificateFactory.getInstance("X509");
      X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
      cert.checkValidity();
      return cert;
    } catch (CertificateExpiredException e) {
      throw new RuntimeException("证书已过期", e);
    } catch (CertificateNotYetValidException e) {
      throw new RuntimeException("证书尚未生效", e);
    } catch (CertificateException e) {
      throw new RuntimeException("无效的证书", e);
    }
  }
获取内容的位置.png

4.将证书保存在本地

private void saveCertificate(List<PlainCertificateItem> cert,String outputFilePath) throws IOException {
        //获取类加载的根路径
        String rootPath=this.getClass().getResource("/").getPath();
        String pkfp=StringUtils.join(rootPath,outputFilePath);
        File file = new File(pkfp);
        file.mkdirs();

        for (PlainCertificateItem item : cert) {
            String outputAbsoluteFilename = file.getAbsolutePath() + File.separator + "wechatpay_" + item.getSerialNo() + ".pem";
            try (BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(outputAbsoluteFilename), StandardCharsets.UTF_8))) {
                writer.write(item.getPlainCertificate());
            }

            log.info("save cert file absolute path = {}" , outputAbsoluteFilename);
        }
    }

相关文章

  • 项目根路径读取写入

    前段时间做的功能,有些技术点记录下, //获取类加载的根路径String rootPath=this.getCla...

  • python 文件操作

    fp=open("文件路径","方式") 文件读取 文件写入 文件关闭 文件读取写入方式

  • Android 读写设备节点

    定义节点路径: 写入节点: 读取节点:

  • java项目根路径读取properties

    //ToolUtil是当前类名称Properties prop = new Properties();try {p...

  • pyspark读写csv文件

    读取csv文件 写入csv文件 option支持参数 path: csv文件的路径。支持通配符; header: ...

  • JDBC学习笔记(4) -- 类路径读取jdbcUtil的配置文

    应使用类路径的读取 . 代表java命令运行的目录 1.在java项目下,. java命令的运行目录从项目的根目...

  • iOS野狗云 分页查询数据

    在消息写入部分,我们在路径中写入的数据都是按写入数据的时间戳顺序排布的。那么问题来了,我们读取数据的时候是需要逆序...

  • 关于文件File的存取

    将文件读取并写入一个文件夹里面的文件中 ①生成一个文件路径(选择D盘) ②创建文件的路径和名称 ③创建一个“写入文...

  • laravel开启定时

    crontab -e写入:* * * * * [php可执行文件路径]/php [项目路径]/artisan sc...

  • fs文件系统操作

    基础写入文件 简单写入文件 流式文件写入 简单文件读取 流式文件读取 流式文件拷贝(读取 + 写入) 复制文件 f...

网友评论

      本文标题:项目根路径读取写入

      本文链接:https://www.haomeiwen.com/subject/kxalsctx.html