栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot获得文件转发给外部接口

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot获得文件转发给外部接口

上一篇文章“SpringBoot中接收form-data请求参数处理后请求外部接口”的有个会在本地创建文件夹的缺点,在本文中会得到解决。

本文背景是通过一个链接请求得到一份文件,再将文件转给外部的一个接口,请求的参数就是得到文件的请求地址。 重点在得到文件后的操作。

 

 @Autowired
    private GatewayParams gatewayParams;


    private static final String QTKEN = "x-qys-accesstoken";
    private static final String QTIME = "x-qys-timestamp";
    private static final String QNonCE = "x-qys-nonce";
    private static final String QSINA = "x-qys-signature";


    
    @Override
    public String createbyfile (@RequestBody DianziFileReq dianziFileReq){
        //获得得到文件的请求地址
        String fileUrl = dianziFileReq.getFileUrl();
        String title = dianziFileReq.getTitle();
        String fileType = dianziFileReq.getFileType();

        HttpURLConnection conn = null;

        try{
            URL path = new URL(fileUrl);
            conn = (HttpURLConnection) path.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10 * 1000);
            // 通过输入流获取数据
            InputStream fis = conn.getInputStream();
            byte[] buffer = readInputStream(fis);

            ByteArrayResource byteArrayResource = new ByteArrayResource(buffer){
                @Override
                public String getFilename() {
                    //重写文件名称
                    return title;
                }
            };
            MultiValueMap dataMap = new linkedMultiValueMap<>();
            dataMap.add("file",byteArrayResource);
            dataMap.add("title",title);
            dataMap.add("fileType",fileType);
            //外部接口请求参数
            String appToken = gatewayParams.getGateDianZiToken();
            String appSecret = gatewayParams.getGateDianZiSecret();
            Date nowDate = new Date();
            Long timestamp = nowDate.getTime();
            double nonce = Math.random();
            String signature= MD5Utils.stringToMD5(appToken+appSecret+timestamp.toString()+nonce);

            HttpHeaders headers = new HttpHeaders();
            headers.set(QTKEN,appToken);
            headers.set(QTIME,timestamp.toString());
            headers.set(QNONCE,nonce+"");
            headers.set(QSINA,signature);
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            //外部接口的请求地址
            String url =gatewayParams.getGateDianZiUrl() + "/v2/document/createbyfile";

            HttpEntity httpEntity = new HttpEntity<>(dataMap,headers);

            RestTemplate restTemplate =new RestTemplate();
            //请求外部接口地址
            ResponseEntity response =restTemplate.postForEntity(url,httpEntity, JSONObject.class);
            Object result =response.getBody();
            if(result != null){
                Map resultMap = (Map) result;
                Map resultMap1=(Map)resultMap.get("result");
                //合同ID
                return (String) resultMap1.get("documentId");
            }else {
                return "";
            }

        }catch (IOException ex){
            ex.printStackTrace();
            return ex.getMessage();
        }



    }
MD5Utils是一个MD5加密的方法,是外部接口请求头约定需要的。
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String stringToMD5(String plainText) {
        byte[] secretBytes = null;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes()
            );
        }
        catch (NoSuchAlgorithmException e){
            throw  new RuntimeException("没有这个md5算法!");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for(int i = 0; i < 32 - md5code.length(); i++){
            md5code = "0" + md5code;
        }
        return md5code;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396942.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号