In this example
Scenario is that Rest web service is returning different files stored in database in binary stream.these files could be .pdf,.jPG or any other formats.
So by using below code you can read binary stream easily.
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(URI, HttpMethod.GET, entity, byte[].class, "1");
if(response.getStatusCode().equals(HttpStatus.OK))
{
FileOutputStream output = new FileOutputStream(new File("filename.jar"));
IOUtils.write(response.getBody(), output);
}
Below is another example using same method for reading JPG file from output,here i have taken example for JPG file but you can read any file from response using this code just change file type
In below example I am calling rest webservice
http://abcd.com/Rest/retrive
and passing input parameter as filename
package service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class DownloadRestClient {
private Logger logger = Logger.getLogger(DownloadRestClient.class);
private String uri = "http://abcd.com/Rest/retrive";
private String name;
public DownloadRestClient(String name) {
super();
this.name = name;
}
/**
* @param args
* @throws IOException
*/
public String run(String filename) throws IOException {
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("filename", filename);
logger.info("Created multipart request: " + multipartMap);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
template.getMessageConverters()
.add(new ByteArrayHttpMessageConverter());
HttpEntity request = new HttpEntity(multipartMap,headers);logger.info("Posting request to: " + uri);ResponseEntity<byte[]> httpResponse = template.exchange(uri,HttpMethod.POST, request, byte[].class);if (!httpResponse.getStatusCode().equals(HttpStatus.OK)) {logger.error("Problems with the request. Http status: "+ httpResponse.getStatusCode());} else {System.out.println("Got successfull message "+ httpResponse.getHeaders().CONTENT_DISPOSITION);FileOutputStream fileOutputStream = new FileOutputStream("E:\\Sagar\\LunaWorkspace\\src\\resource\\output.jpg");org.apache.commons.io.IOUtils.write(httpResponse.getBody(),fileOutputStream);}return httpResponse.toString();}}
Scenario is that Rest web service is returning different files stored in database in binary stream.these files could be .pdf,.jPG or any other formats.
So by using below code you can read binary stream easily.
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(URI, HttpMethod.GET, entity, byte[].class, "1");
if(response.getStatusCode().equals(HttpStatus.OK))
{
FileOutputStream output = new FileOutputStream(new File("filename.jar"));
IOUtils.write(response.getBody(), output);
}
Below is another example using same method for reading JPG file from output,here i have taken example for JPG file but you can read any file from response using this code just change file type
In below example I am calling rest webservice
http://abcd.com/Rest/retrive
and passing input parameter as filename
package service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class DownloadRestClient {
private Logger logger = Logger.getLogger(DownloadRestClient.class);
private String uri = "http://abcd.com/Rest/retrive";
private String name;
public DownloadRestClient(String name) {
super();
this.name = name;
}
/**
* @param args
* @throws IOException
*/
public String run(String filename) throws IOException {
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("filename", filename);
logger.info("Created multipart request: " + multipartMap);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
template.getMessageConverters()
.add(new ByteArrayHttpMessageConverter());
HttpEntity request = new HttpEntity(multipartMap,headers);logger.info("Posting request to: " + uri);ResponseEntity<byte[]> httpResponse = template.exchange(uri,HttpMethod.POST, request, byte[].class);if (!httpResponse.getStatusCode().equals(HttpStatus.OK)) {logger.error("Problems with the request. Http status: "+ httpResponse.getStatusCode());} else {System.out.println("Got successfull message "+ httpResponse.getHeaders().CONTENT_DISPOSITION);FileOutputStream fileOutputStream = new FileOutputStream("E:\\Sagar\\LunaWorkspace\\src\\resource\\output.jpg");org.apache.commons.io.IOUtils.write(httpResponse.getBody(),fileOutputStream);}return httpResponse.toString();}}
No comments:
Post a Comment