如果已更新文件,如何下载文件

时间:2011-09-24 06:49:24

标签: android httpclient httpresponse head

我正在从SD卡下载互联网上的图像。首先检查文件是否存在于SD卡中如果没有,则下载文件。如果文件已经在SD卡中,则检查现有文件的上次修改日期并将其与服务器文件进行比较。如果服务器映像文件的日期是最新的SD卡文件,则在返回文件时以其他方式下载文件。

我已经做到了。文件被下载到SD卡中,我必须检查它,这是一种写入方式来完成这项任务,还是他们以任何其他方式完成同样的任务。

所以你可以帮我解决同样的问题。 这是我的代码:

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)
    {
//       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 个答案:

没有答案