Wps开放平台v5升级v7上传实体文件踩坑(Java使用restTemplate)

背景: 最近接到一个老项目需求,之前开发的WPS开放平台文件(商密集成)预览功能因为升级需要重新对接api,新的上传文件接口踩坑特意记录一下。

在这里插入图片描述
这里出问题的是第二步,请求文件上传信息
在这里插入图片描述
踩坑代码 调用后403 postman粘贴请求头和地址发起模拟调用成功

private String upload(StoreRequest storeRequest, File file) throws IOException, URISyntaxException {
        URI url = new URIBuilder(storeRequest.getUrl()).build();
        HttpHeaders headers = new HttpHeaders();
        List<Header> storeRequestHeaders = storeRequest.getHeaders();
        storeRequestHeaders.forEach(header -> headers.add(header.getName(), header.getValue()));
        byte[] fileBytes = Files.readAllBytes(file.toPath());
        HttpEntity<byte[]> requestEntity = new HttpEntity<>(fileBytes, headers);
        return restTemplate.postForObject(url,requestEntity, String.class).getBody();
    }

修改后:

private String upload(StoreRequest storeRequest, File file) throws IOException,  NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // 1. 读取文件为字节数组
        byte[] fileBytes = Files.readAllBytes(file.toPath());

        // 2. 创建信任所有证书的 HttpClient
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(null, (chain, authType) -> true)
                .build();

        try (CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLContext(sslContext)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .build()) {

            // 3. 创建 POST 请求
            HttpPost httpPost = new HttpPost(storeRequest.getUrl());

            // 4. 设置 Headers(保持原始 Content-Type)
            List<Header> storeRequestHeaders = storeRequest.getHeaders();
            storeRequestHeaders.forEach(header ->
                    httpPost.addHeader(header.getName(), header.getValue())
            );

            // 5. 设置请求体(二进制数据)
            ByteArrayEntity requestEntity = new ByteArrayEntity(fileBytes);
            httpPost.setEntity(requestEntity);

            // 6. 执行请求并获取响应
            HttpResponse response = httpClient.execute(httpPost);
            org.apache.http.Header[] headers = response.getHeaders("X-Wps3-Info-Token");

            // 7. 返回响应头中的 Token
            return headers[0].getValue();
        }
    }
问题原因

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用RestTemplate会自动根据上传二进制文件自动响应Content-Type为application/octet-stream
后与wps开发确认,此处的确是传空字符串。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值