when I am trying to download files from server, it misses some bytes! and shows as a corrupted file. below the code i tried to download from URL. It gives no exception while running. I am using this in a service. these are the sample results from my tries:
file 1: actual size = 73.2 kb downloaded size = 68.7 kb
file 2: actual size = 147 kb downloaded size = 137 kb
file 3: actual size = 125 kb downloaded size = 116.8 kb
please help me to find the correction needed to my code. thanks,
InputStream stream = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
fos = new FileOutputStream(output.getPath());
int next = -1;
while ((next = reader.read()) != -1) {
fos.write(next);
}
// Successful finished
Log.d("reaching", "reaching : DOWNLOAD FINISHED SUCCESSFULLY");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}