no suitable HttpMessageConverter found for response type [class java.io.InputStream]

报错

使用springrestful 获取流的时候,报错

@Autowired
private RestTemplate restTemplate;

 restTemplate.getForObject(url, InputStream.class);

请求报错:

Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.InputStream] and content type [application/force-download]

方法1: 推荐使用

使用 org.springframework.core.io.Resource.class 来接收

org.springframework.core.io.Resource forObject = restTemplate.getForObject(url, org.springframework.core.io.Resource.class);
InputStream inputStream = Objects.requireNonNull(forObject).getInputStream();

方法2: 

直接用httpclient 请求,可以直接获取到流:

 public static InputStream sendGet(String url) {
        InputStream inputStream = null;
        try {
            // 创建URL对象
            URL connURL = new URL(url);
            // 打开URL连接
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection();
            // 设置通用属性
            httpConn.setRequestProperty("Accept", "*/*");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
            // 建立实际的连接
            httpConn.connect();

            inputStream = httpConn.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值