开发者

How To Download a File Only If It Has Been Updated

I am downloading image from the internet in SD card. First to Check the file is exist in SD Card if no then download the file. if file is already in the SD-card the check the last modified date of existing file and compare it with server file . If server image file date is latest than SD-card file then download the file otherwise as it return the file.

I Have did it.The file gets downloaded in SD-card, I have to checked it, is this a write way to achieve this task or is their any other way to do the same task.

So could you help me regarding same. here is my code:

package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
*/
import android.app.Activity;
import android.graphics.Bitmap;

public class FileChache {


    File sdDir;

    public FileChache(Activity  activity)
    {
开发者_StackOverflow中文版//       Find the directry to save cache image
                    //Gets the Android external storage directory.---> getExternalStorageState()
                    //MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access.
        String sdState = android.os.Environment.getExternalStorageState();

         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
         {
             sdDir=new File(android.os.Environment.getExternalStorageDirectory(),"sa");
         }
        else
                 sdDir=activity.getCacheDir();

                if(!sdDir.exists())
                    sdDir.mkdirs();      
        }


    public File getFile(String url)
    {
        //identify images by hashcode
        String filename= String.valueOf(url.hashCode());


        System.out.println("The File name is:"+filename);

//      Check whether the file is exist in SD Card

        File f= new File(sdDir, filename);
        Date fileDate = new Date(f.lastModified());
        System.out.println("The file date is:"+fileDate.toGMTString());
//      Check whether the file is exist in SD Card      
        if(!f.exists())
        {               
                return f;
        }

        else
        {
                //Check the last update of file
            File file= new File(filename);

            Date date= new Date(f.lastModified());          

            System.out.println("The last Modified of file is:"+date.toGMTString());

            /*HttpClient httpclient = new DefaultHttpClient();
             HttpGet httpget = new HttpGet(url);*/
            try
            {
                URL urll = new URL(url);

                String ss= String.valueOf(urll.hashCode());

                URLConnection conn= urll.openConnection();

                Map map = conn.getHeaderFields();
                Set set = map.entrySet();

                Iterator iterator = set.iterator();
                while (iterator.hasNext()) {
                    System.out.println("The Data is :"+iterator.next());                


                }
                    String header=conn.getHeaderField("Last-Modified");
                    System.out.println("The header is:"+header);
                    long header1=conn.getIfModifiedSince();

                    Date aaa= new Date(header1);
                    System.out.println("The getIFLastmodifiedSince is:----"+aaa);

                    Date ServerDate_timeStamp = new Date(header);
                    System.out.println("The headerDate(The serverDate_Timestamp) is:"+ServerDate_timeStamp);

                    long filemodifiedDate= f.lastModified();
                    System.out.println("The last(Long) modified is:"+filemodifiedDate);

                    Date SD_cardFileModifiedDate = new Date(filemodifiedDate);
                    System.out.println("File file Modified Date Date is:"+ SD_cardFileModifiedDate);

                    if(SD_cardFileModifiedDate.before(ServerDate_timeStamp))
                    {
                        File ff= new File(sdDir,ss);
                        return ff;
                    }
                    else
                    {
                        return f;
                    }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
    }   

    return null;

}


    public void writeFile(Bitmap bmp, File f) {
        FileOutputStream out = null;

        try {
            out = new FileOutputStream(f);
            bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally { 
            try { if (out != null )
                out.close(); }
            catch(Exception ex) {} 
        }
    }

}


/*File f= new File(sdDir,filename);

System.out.println("***The last modified time of file is:****"+f.lastModified());

Date d= new Date(f.lastModified());
System.out.println("The lst modified Date is:"+d.toGMTString());

Calendar calendar = Calendar.getInstance();
Date mNew_timeStamp = calendar.getTime();       
System.out.println("*****new Time_Stamp:*****"+mNew_timeStamp.toGMTString());


if(f.exists())
{
    System.out.println("The file is exist");
}
else
{
        System.out.println("It does not exist");    
}*/

//if(header)

//  System.out.println(conn.getHeaderFieldKey(j)+":"+ header);


/*HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜