//here we are checking the directory already exists or not
File directory = new File("file directory name/full path");
if(!directory.exists()){
directory.mkdirs();
}
//for loop to download the multiple files.
for(int i=0; i<presentationsList.size();i++){
File file=new File(directory, filename);
//here we checking the if file already exists or not. and even if the file size is zero we delete the file and recreate the file.
if(file.exists() && file.length()==0){
file.delete();
}
//here is the actual stuff to download the file.
if(!file.exists()){
try
{
URLConnection ucon = new URL(proved the url here).openConnection();
InputStream is = ucon.getInputStream();
fsize=ucon.getContentLength();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(ucon.getContentLength());
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
//if the downloaded file size is less than the actual file size then we remove the file
if(file.length()<fsize){
if(file.exists())
file.delete();
}
}
catch (Exception e)
{
if(file.length()<fsize){
if(file.exists())
file.delete();
}
e.printStackTrace();
}
}
}
No comments:
Post a Comment