restTemplate 微服务远程传输文件流

添加Bean
可以在启动类添加,也可以单独写一个配置文件

 public static void main(String[] args)
    {
        SpringApplication.run(RuoYiSystemApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  系统模块启动成功   ლ(´ڡ`ლ)゙  \n");
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    @Bean
    //添加 @LoadBalanced注解 可以使用服务名访问 进行负载均衡轮询
    @LoadBalanced 
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

文件是获取ftp文件服务器的文件代码

/**
 * 获取文件流
 * @param ftpClient 连接
 * @param filename 文件地址
 * @return
 */
public InputStreamResource ftpInputStreamResource(FTPClient ftpClient, String filename) {
        InputStream in = null;
        InputStreamResource fileResource = null;
        try {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding("GBK");
            String newPath = new String(filename.getBytes("GBK"), "ISO-8859-1");
            FTPFile[] ftpFile = ftpClient.listFiles(newPath);
            in = ftpClient.retrieveFileStream(newPath);
            fileResource = new InputStreamResource(in){
                @Override
                public long contentLength() {
                    return ftpFile[0].getSize();
                }

                @Override
                public String getFilename() {
                    return ftpFile[0].getName();
                }
            };

        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileResource;
    }

发送请求的代码

 /**
     * 获取ftp服务器 发送文件 远程发送请求
     * @param ykYcRoute
     */
    public void post(YkYcRoute ykYcRoute) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        //                                              multipart/form-data
        MediaType mediaType = MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
        headers.setContentType(mediaType);
        //请求体为入参map
        LinkedMultiValueMap<String, Object> request = new LinkedMultiValueMap<>();
        //获取连接
        FTPClient ftpClient = ftpUtils.initFtpClient();
        InputStreamResource file = ftpUtils.ftpInputStreamResource(ftpClient, ykYcRoute.getFilePath());
        request.add("file", file);
        request.add("fileName", ykYcRoute.getFileName());
        request.add("fileType", "yc_xlsx");
        HashMap<String, String> keySign = JsonUtils.toMap(ykYcRoute.getSign());
        request.add("keySign", keySign);
        //用httpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity(request, headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(routeUrl, entity, String.class);
        String body = stringResponseEntity.getBody();

        System.err.println(body);
        ftpUtils.dropFtpClient(ftpClient);
    }

其中用到了其他工具方法自己封装的比如JsonUtils.toMap

/**
     * 字符串 转 map
     * @param json
     * @return
     */
    public static HashMap toMap(String json) {
        json = json.replace("[", "").replace("]", "");
        String[] maxJson = json.split("},");
        return Arrays.stream(maxJson)
                .map((e) -> {
                    return e.indexOf("}") == -1 ? e + "}" : e;
                })
                .collect(Collectors.toMap(
                        (e) -> {
                            try {
                                return mapper.readValue(e, Map.class).get("key").toString();
                            } catch (JsonProcessingException jsonProcessingException) {
                                jsonProcessingException.printStackTrace();
                            }
                            return null;
                        },
                        (e) -> {
                            try {
                                return mapper.readValue(e, Map.class).get("value").toString();
                            } catch (JsonProcessingException jsonProcessingException) {
                                jsonProcessingException.printStackTrace();
                            }
                            return null;
                        },
                        (e1, e2) -> e2,
                        HashMap::new
                ));
    }

还有其他ftp的工具类可以访问我之前写的ftp工具类文件都有
https://blog.csdn.net/qq_43898141/article/details/123921156

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值