如何仅下载已更新的文件
我正在从互联网上将图像下载到 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);*/
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)
{
// 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);*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论