如何仅下载已上传到服务器的文件

发布于 2024-12-11 01:26:21 字数 2705 浏览 0 评论 0原文

下面是我完成的代码,但我不确定它是否正确?任何从事此工作的人都请帮忙。实际上,我想检查服务器中文件的日期以及 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 org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

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(),"sash");
}
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);
System.out.println("The file date is:"+f.lastModified());
// 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);
URLConnection conn= urll.openConnection();
String header=conn.getHeaderField("Last-Modified");
System.out.println("The header is:"+header);
Date ServerDate_timeStamp = new Date(header);
System.out.println("The headerDate 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,filename);
return ff;
}
else
{
return f;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return null;
}

Below is the code i have done but i am not sure whether is it right way or not? anybody working on it kindly help please.Actually i want to check the date of the file from the server and the file saved in my SD card and if date of server's file is updated then i have to download it and display it else it will just check and display the file present in SD card.
Thanks in advance .

package com.test;

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

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

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(),"sash");
}
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);
System.out.println("The file date is:"+f.lastModified());
// 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);
URLConnection conn= urll.openConnection();
String header=conn.getHeaderField("Last-Modified");
System.out.println("The header is:"+header);
Date ServerDate_timeStamp = new Date(header);
System.out.println("The headerDate 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,filename);
return ff;
}
else
{
return f;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文