On Sat, 2007-08-11 at 17:34 -0700, nadeem ghani wrote:
> Thanks Roland.
>
> But your suggestion doesn't seem to be working. Invoking
> Entity.getContentLength() returns, correctly, 80MB of data to be read, but
> when I read from the InputStream, I only get a few kB.
>
> Also, if I use curl I do get 80MB, so the data should be there.
>
> The code I'm using is below. If you could point out errors, or suggest
> anything else to try, I'd appreciate it.
>
> Nadeem
>
> public static void writeToFile(HttpResponse resp, String pathToFile) {
> HttpEntity entity = resp.getEntity();
> InputStream instream;
> try {
> instream = entity.getContent();
> if (instream == null) {
> System.out.println("instream == null");
> }
> System.out.println("entity content length: " +
> entity.getContentLength());
>
> long bytesRead = 0;
> FileOutputStream output = new FileOutputStream(new
> File(pathToFile));
>
> try {
> byte[] tmp = new byte[1024];
> int l;
> while((l = instream.read(tmp)) != -1) {
> bytesRead += l;
> output.write(tmp, 0, l);
> }
> System.out.println("bytes read: " + bytesRead);
> } finally {
> instream.close();
> }
> } catch (IllegalStateException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
>
Nadeem,
I do not see any obvious problem with your code. At the same time I am
unable to reproduce the problem locally (see code below). It all seems
to work for me as advertised.
Could you please try to put together a complete test case that we could
use to reproduce the problem
==================================================
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
BasicHttpProcessor httpproc = new BasicHttpProcessor();
// Required protocol interceptors
httpproc.addInterceptor(new RequestContent());
httpproc.addInterceptor(new RequestTargetHost());
// Recommended protocol interceptors
httpproc.addInterceptor(new RequestConnControl());
httpproc.addInterceptor(new RequestUserAgent());
httpproc.addInterceptor(new RequestExpectContinue());
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("localhost", 80);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new
DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", "/blob");
System.out.println(">> Request URI: " +
request.getRequestLine().getUri());
context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn,
context);
httpexecutor.postProcess(response, httpproc, context);
System.out.println("<< Response: " + response.getStatusLine());
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("<< Content length: " +
entity.getContentLength());
InputStream instream = entity.getContent();
long bytesRead = 0;
byte[] tmp = new byte[1024];
int l;
while((l = instream.read(tmp)) != -1) {
bytesRead += l;
}
System.out.println("<< Bytes read: " + bytesRead);
}
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
} finally {
conn.close();
}
==================================================
>> Request URI: /blob
<< Response: HTTP/1.1 200 OK
<< Content length: 42752304
<< Bytes read: 42752304
Connection kept alive...
==================================================
Oleg
>
>
>
>
> ____________________________________________________________________________________
> Be a better Globetrotter. Get better travel answers from someone who knows.
> Yahoo! Answers - Check it out.
> http://answers.yahoo.com/dir/?link=list&sid=396545469
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]