跨境汇款-文件预上传接口

接口描述

商户通过本接口将订单关联的文件信息上送至合利宝-跨境系统,获取真实的文件上传地址。
商户使用返回的文件上传地址将文件上送至合利宝服务器。

流程图

基本信息

测试请求地址 https://cbtrxtest.helipay.com/cbgateway/api/pay/preUpload
请求地址 https://cbptrx.helipay.com/cbgateway/api/pay/preUpload
提交方式 采用POST方法提交,requestbody传值 Content-Type=[application/json;charset=UTF-8]
签名算法 UTF-8字符编码 SHA256签名 AES加密

请求参数

名称 参数 类型 必填 示例值 说明
产品编码 productCode String(20) 需要上传文件订单对应的产品编码
ONLINEB2B:网银B2B
ONLINEB2C:网银B2C
QUICKPAY:快捷支付
ALIPAYSCAN:支付宝主扫
WXPAYSCAN:微信主扫
商户订单号 orderNo String(64) p_20170302185347 关联网关购汇订单
商户编号 merchantNo String(10) Me10000002 合利宝-跨境系统分配的商户编号
文件类型 fileType String(15) zip 需要上传的文件类型
支持 PDF、Word、zip、rar、JPG、BMP
文件大小 fileSize Integer 2097152 单位为字节
文件大小不能超过10M, 即 10485760

响应参数

名称 参数 类型 必填 示例值 说明
产品编码 productCode String(20) ALIPAYSCAN
商户订单号 orderNo String(64) p_20170302185347
商户编号 merchantNo String(10) Me10000002 合利宝-跨境系统分配的商户编号
响应码 errorCode String(10) 0000 0000 代表请求成功
响应信息 errorMessage String(200) 成功 响应信息 错误信息
响应时间 current String(20) 2018-04-01 12:00:00 格式yyyy-MM-dd HH:mm:ss
文件真实上传路径 uploadUrl String 只支持put格式,不支持浏览器直接打开;使用方式参考demo;
上传地址有效期为30分钟,超过三十分钟后失效;过期后,可调用预生成接口重新生成。
文件上传token token String 上传时指定 http请求头。
例:put.addHeader("x-oss-meta-token", token);
上传文件时必须指定请求头,否则会报 403

代码示例

private RemoteService remoteService = new RemoteServiceImpl();

    @Test
    public void uploadByHttpClient() throws IOException {
        String filePath = "/Users/chronos/Downloads/1541558673220.jpg";
        Path path = Paths.get(filePath);

        GatewayApiFilePreUploadRequest request = new GatewayApiFilePreUploadRequest();
        request.setMerchantNo("Me10000018");
        request.setProductCode("ONLINEB2C");
        request.setOrderNo("Online20180401hlp");
        request.setFileSize(Files.size(path));
        int typeIndex = path.toString().lastIndexOf(".") + 1;
        if (typeIndex == 0) {
            throw new IllegalArgumentException("不支持的文件类型");
        }
        request.setFileType(path.toString().substring(typeIndex));
        HeliRequest param = HandleDataUtils.encodeAndSign(request, request.getProductCode(), request.getMerchantNo());
        HeliRequest heliRequest = remoteService.postRemoteInvoke(LoadPropertiesUtils.getProperty("gatewayFilePreUpload"), JSONObject.toJSONString(param), HeliRequest.class);
        GatewayApiFilePreUploadResponse response = HandleDataUtils.decode(heliRequest, GatewayApiFilePreUploadResponse.class, heliRequest.getProductCode(), heliRequest.getMerchantNo());
        logger.info("文件预生成响应结果:" + response.toString());

        if ("0000".equals(response.getErrorCode())) {
            logger.info("根据预生成响应信息,上传文件");
            testUploadByHttpClient(response.getUploadUrl(), response.getToken(), path);
        } else {
            logger.error("文件预生成接口响应异常,请求检查!" + response);
            throw new IllegalArgumentException("预生成文件失败,请求检查");
        }
    }

    public void testUploadByHttpClient(String url, String token, Path path) {
        HttpPut put = new HttpPut(url);
        put.addHeader("x-oss-meta-token", token);

        HttpEntity entity = new FileEntity(path.toFile());
        put.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = HttpClients.createDefault().execute(put);
        } catch (IOException e) {
            logger.error("上传文件失败!", e);
            return;
        }
        if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
            logger.info("文件上传成功!");
            return;
        }
        logger.info("文件上传失败!返回码:" + response.getStatusLine().getStatusCode());
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            logger.info("文件上传失败!返回信息:" + sb);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

                                
private RemoteService remoteService = new RemoteServiceImpl();

    @Test
    public void uploadByHttpConnection() throws IOException, KeyManagementException, NoSuchAlgorithmException {
        String filePath = "/Users/chronos/Downloads/1541558673220.jpg";
        Path path = Paths.get(filePath);

        GatewayApiFilePreUploadRequest request = new GatewayApiFilePreUploadRequest();
        request.setMerchantNo("Me10000018");
        request.setProductCode("ONLINEB2C");
        request.setOrderNo("Online20180401hlp");
        request.setFileSize(Files.size(path));
        int typeIndex = path.toString().lastIndexOf(".") + 1;
        if (typeIndex == 0) {
            throw new IllegalArgumentException("不支持的文件类型");
        }
        request.setFileType(path.toString().substring(typeIndex));
        HeliRequest param = HandleDataUtils.encodeAndSign(request, request.getProductCode(), request.getMerchantNo());
        HeliRequest heliRequest = remoteService.postRemoteInvoke(LoadPropertiesUtils.getProperty("gatewayFilePreUpload"), JSONObject.toJSONString(param), HeliRequest.class);
        GatewayApiFilePreUploadResponse response = HandleDataUtils.decode(heliRequest, GatewayApiFilePreUploadResponse.class, heliRequest.getProductCode(), heliRequest.getMerchantNo());
        logger.info("文件预生成响应结果:" + response.toString());

        if ("0000".equals(response.getErrorCode())) {
            logger.info("根据预生成响应信息,上传文件");
            testWithHttpsUrlConnection(response.getUploadUrl(), response.getToken(), path);
        } else {
            logger.error("文件预生成接口响应异常,请求检查!" + response);
            throw new IllegalArgumentException("预生成文件失败,请求检查");
        }
    }

    public void testWithHttpsUrlConnection(String uploadUrl, String token, Path path) throws NoSuchAlgorithmException, KeyManagementException, IOException {
        URL url = null;
        try {
            url = URI.create(uploadUrl).toURL();

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

        SSLContext sc = initSSLContext();

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setSSLSocketFactory(sc.getSocketFactory());
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("x-oss-meta-token", token);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.connect();

        DataInputStream in = new DataInputStream(new FileInputStream(path.toFile()));
        int bytes;
        byte[] bufferOut = new byte[1024];
        OutputStream outputStream = conn.getOutputStream();
        try {
            TimeUnit.SECONDS.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while ((bytes = in.read(bufferOut)) != -1) {
            outputStream.write(bufferOut, 0, bytes);
        }
        in.close();
        outputStream.flush();
        outputStream.close();


        if (HttpStatus.SC_OK == conn.getResponseCode()) {
            logger.info("文件上传成功!");
            return;
        }
        logger.info("文件上传失败!" + conn.getResponseCode() + "," + conn.getResponseMessage());
        InputStream inputStream = conn.getErrorStream();

        // 读取返回数据
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        reader.close();
        conn.disconnect();

        logger.info("返回值是:{}", sb.toString());
    }

    private SSLContext initSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sc = SSLContext.getInstance("TLSv1.2");
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    @Override
                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                    @Override
                    public void checkServerTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        };
        // Init the SSLContext with a TrustManager[] and SecureRandom()
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        return sc;
    }
                                

                                

                                

错误码

错误码 错误码描述 解决方案