Java 类org.apache.http.nio.client.methods.ZeroCopyConsumer 实例源码

项目:algorithmia-java    文件:HttpClient.java   
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());
    }
}
项目:yunpian-java-sdk    文件:ZeroCopyHttpExchange.java   
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");
}
项目:Android-Studio-Translate-Tool    文件:ZeroCopyHttpExchange.java   
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");
}