I'm trying to download a file over HTTP and store its contents in a String, as the title says. My approach is thus:
URL u = new URL("http://url/file.txt");
ByteArrayBuffer baf = new ByteArrayBuffer(32);
InputStream in = (InputStream) u.getContent();
BufferedInputStream bis = new BufferedInputStream(in);
int buffer;
while((buffer = bis.read()) != -1){
baf.append((byte)buffer);
}
bis.close();
in.close();
The code fails when it tries to read from the stream, reporting the stream is closed.
Now if you try to access the file through a browser, it won't be served as text, rather as a file to be downloaded.
I haven't gotten anywhere searching the web on this, so a little insight would be much appreciated!
Thanks.