Reputation: 411
I am using the following code to download my alfresco HTML content of size 51kb
HttpGet httpget = new HttpGet(url);
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println("----------------------------------------");
if (entity != null) {
System.out.println("Response content type: " + entity.getContentType());
long contentLength = entity.getContentLength();
System.out.println("Response content length: "+ entity.getContentLength());
if (contentLength > 0) {
b = new byte[(int) contentLength];
entity.getContent().read(b);
content=new String(b);
content=content.replace("\n", "").replace("\r", "");
//content = StringEscapeUtils.escapeHtml(content);
System.out.println("Response content: " + content);
}
}
Only 30-40 % of the HTML content is downloaded and displayed. I am not able to get the full content.
I tried increasing the byte size of b. But nothing worked out for me.
Please help me in download alfresco content using java code. Any help is appreciated. Thanks in advance.
Upvotes: 0
Views: 1507
Reputation: 18569
Reading an InputStream doesn't always return the whole contents. You need to read from the InputStream
in a loop and write to the correct offset of the buffer, and increment the offset based on the return value of read()
. For example:
byte[] b = new byte[(int)contentLength];
int offset = 0;
while(offset < contentLength) {
offset += inputStream.read(b, offset, b.length - offset);
}
String content = new String(b); // Or specify encoding.
Or you can use a library like Apache Commons IO. Then it's:
IOUtils.toString(entity.getContent(), encoding);
Upvotes: 2