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;
    }

你可能感兴趣的:(错误处理,java,restful,httpclient)