Apache camel Apache HttpComponents Asyn GET:实体内容太长
我正在Camel内部的bean中使用apachehttpcomponents,试图编写一个作业来下载苹果的元数据数据库文件。这是iTunes中每首歌曲的列表。所以,很明显,它很大。3.5+GB。我正在尝试使用ApacheHttpComponents发出异步get请求。但是,返回的文件似乎太大Apache camel Apache HttpComponents Asyn GET:实体内容太长,apache-camel,large-files,apache-httpcomponents,Apache Camel,Large Files,Apache Httpcomponents,我正在Camel内部的bean中使用apachehttpcomponents,试图编写一个作业来下载苹果的元数据数据库文件。这是iTunes中每首歌曲的列表。所以,很明显,它很大。3.5+GB。我正在尝试使用ApacheHttpComponents发出异步get请求。但是,返回的文件似乎太大 try { httpclient.start(); FileOutputStream fileOutputStream = new FileOutputStream(dow
try {
httpclient.start();
FileOutputStream fileOutputStream = new FileOutputStream(download);
//Grab the archive.
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("https");
uriBuilder.setHost("feeds.itunes.apple.com");
uriBuilder.setPath("/feeds/epf-flat/v1/full/usa/" + iTunesDate + "/song-usa-" + iTunesDate + ".tbz");
String endpoint = uriBuilder.build().toURL().toString();
HttpGet getCall = new HttpGet(endpoint);
String creds64 = new String(Base64.encodeBase64((user + ":" + password).getBytes()));
log.debug("Auth: " + "Basic " + creds64);
getCall.setHeader("Authorization", "Basic " + creds64);
log.debug("About to download file from Apple: " + endpoint);
Future<HttpResponse> future = httpclient.execute(getCall, null);
HttpResponse response = future.get();
fileOutputStream.write(EntityUtils.toByteArray(response.getEntity()));
fileOutputStream.close();
因此,文件的字节大小对于一个Java整数来说太大了,HttpComponents使用它来跟踪响应大小。我明白了,不知道除了放回一个层并直接调用Java Net库之外,还有什么解决办法。使用
它构建在http组件之上,支持零拷贝传输
请参见此处的示例:
或者简单地说,在你的情况下
CloseableHttpAsyncClient httpclient = HttpAsyncClientBuilder.create()....
ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(new 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("Connection to host failed: " + response.getStatusLine());
}
return file;
}
};
httpclient.execute(HttpAsyncMethods.createGet(endpoint), consumer, null, null).get();
CloseableHttpAsyncClient httpclient=HttpAsyncClientBuilder.create()。。。。
ZeroCopyConsumer=新的ZeroCopyConsumer(新文件(下载)){
@凌驾
受保护文件进程(
最终HttpResponse响应,
最终文件,
最终ContentType(ContentType)引发异常{
if(response.getStatusLine().getStatusCode()!=HttpStatus.SC\u OK){
抛出新的ClientProtocolException(“到主机的连接失败:+response.getStatusLine());
}
返回文件;
}
};
httpclient.execute(httpasynchmethods.createGet(endpoint),consumer,null,null.get();
响应的主体直接保存到文件中。文件系统给出了唯一的限制,这里是检查响应大小的代码。我认为这更像是HttpComponents的问题,而不是camel。
CloseableHttpAsyncClient httpclient = HttpAsyncClientBuilder.create()....
ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(new 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("Connection to host failed: " + response.getStatusLine());
}
return file;
}
};
httpclient.execute(HttpAsyncMethods.createGet(endpoint), consumer, null, null).get();