downloading file in java
i write a code to download a zip file from internet and extract .
my original file size is 3029478 but i get 3029512 in download and this
corrupt my zip file to extract .
my code is :
URL url = new URL(downloadURL);
URLConnection connection = url.openConnection();
connection.connect();
long fileLenght = connection.getContentLength();
File file = new File(zipFilePath);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output;
if (file.exists())
output = new FileOutputStream(file, true);
else
output = new FileOutputStream(file);
if (file.length() < fileLenght)
{
input.skip(file.length());
setMax((int) fileLenght);
byte data[] = new byte[bufferSize];
long total = file.length();
int count;
while ((count = input.read(data)) != -1)
{
if (running)
{
output.write(data, 0, count);
total += count;
publishProgress((int) (total));
}
else
{
input.close();
output.flush();
output.close();
return false;
}
}
}
i download my file from ubuntu from this url :
http://ubuntuone.com/5PGyI16s0JkRJKUonWFPj1
i download successfully but get a little more byte to download and corrupt
my file.
No comments:
Post a Comment