Is this a convenient implementation? NOTE: it uses Apache HttpComponents Client and Apache Commons IO
public class RunApp {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpGet httpget = new HttpGet("http://localhost/");
System.out.println("Executing request " + httpget.getRequestLine());
// Create a custom response handler
ResponseHandler<File> responseHandler = new ResponseHandler<File>() {
public File handleResponse( final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
File file = new File("Desired-filename");
IOUtils.copy(entity.getContent(), new FileOutputStream(file));
return file;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
File file = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(file.getAbsolutePath());
} finally {
httpclient.close();
}
}
}