private void executeGetFile(HttpUriRequest request, File destination) throws APIException { // We don't use the BasicAsyncResponseConsumer because it barfs when the // content length is too long. try { ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(destination) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new APIException("Download failed: " + response.getStatusLine()); } return file; } }; execute(request, consumer); } catch (FileNotFoundException e) { throw new APIException("Could not find destination file: " + destination.getAbsolutePath()); } }
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try { httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try { httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process( final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }