天天看點

RestTemplate post json downloadRestTemplate post json download

RestTemplate post json download

/**
     * @param requestJson The requested JSON string
     * @param url httpUrl
     * @param filePath The full path of the file, including the name of the file
     * @throws IOException
     */
    public void sendPostJsonDownFile(String requestJson,String url,String filePath) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        List<MediaType> list = new ArrayList<MediaType>();
        list.add(MediaType.valueOf("application/octet-stream"));
        headers.setAccept(list);
        ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<String>(requestJson,headers),byte[].class);
        InputStream inputStream = new ByteArrayInputStream(response.getBody());
        OutputStream outputStream = new FileOutputStream(filePath);
        IOUtils.copy(inputStream, outputStream);
    }