import org.apache.http.nio.client.methods.ZeroCopyPost;
public class ZeroCopyHttpExchange {
public static void main(String[] args) throws Exception {
HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
httpclient.start();
try {
File upload = new File(args[0]);
File download = new File(args[1]);
ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload,
ContentType.create("text/plain", null));
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.shutdown();
}
System.out.println("Done");
}