I suggest using ResponseExtractor.
you can call execute method of RestTemplate such as below.
File file = (File) restTemplate.execute(rootUrl.concat("/vocasets/{vocasetId}/{version}"), HttpMethod.GET, requestCallabck,
responseExtractor, uriVariables);
ResponseExtractor has extractData method. you can take body inputstream from extractData method through getBody() of response.
Extend ResponseExtractor for canceling your request.
Good luck.
In my case, I've used Listener way.
static class FileResponseExtractor implements ResponseExtractor<File> {
...
public void setListener(ReceivingListener listener) {
this.listener = listener;
}
@Override
public File extractData(ClientHttpResponse response) throws IOException {
InputStream is = response.getBody();
long contentLength = response.getHeaders().getContentLength();
long availableSpace = AvailableSpaceHandler.getExternalAvailableSpaceInMB();
long availableBytes = AvailableSpaceHandler.getExternalAvailableSpaceInBytes();
Log.d(TAG, "available space: " + availableSpace + " MB");
long spareSize = 1024 * 1024 * 100;
if(availableBytes < contentLength + spareSize) {
throw new NotEnoughWritableMemoryException(availableSpace);
}
File f = new File(temporaryFileName);
if (f.exists())
f.delete();
f.createNewFile();
OutputStream o = new FileOutputStream(f);
listener.onStart(contentLength, null);
boolean cancel = false;
try {
byte buf[] = new byte[bufferSize];
int len;
long sum = 0;
while ((len = is.read(buf)) > 0) {
o.write(buf, 0, len);
sum += len;
listener.onReceiving(sum, len, null);
cancel = !listener.onContinue();
if(cancel) {
Log.d(TAG, "Cancelled!!!");
throw new CancellationException();
}
}
} finally {
o.close();
is.close();
listener.onFinish(null);
if(cancel) {
f.delete();
}
}
return f;
}
}