如何获取远程文件上次修改日期 [英] How to get Remote File Last-Modification Date
本文介绍了如何获取远程文件上次修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取远程文件的文件修改日期。
显然, 属性
返回 NULL :
- (BOOL)fileHasBeenModifiedSinceLastLaunch
{
NSError * err = nil;
NSDictionary * attributes = [FILEMANAGER attributesOfItemAtPath:[NSURL URLWithString:@http://www.wrightscs.com/some_file.txt] error:& err];
NSString * modified = [NSString stringWithFormat:@%@,[attributes objectForKey:@NSFileModificationDate]];
NSString * savedmod = [[NSUserDefaults standardUserDefaults] objectForKey:@kFeedLastModified];
NSLog(@Error:%@,[err localizedDescription]);
NSLog(@Saved Modified Date:%@,savedmod);
NSLog(@上次修改日期:%@,修改);
[[NSUserDefaults standardUserDefaults] setObject:modified forKey:@kFeedLastModified];
[[NSUserDefaults standardUserDefaults] synchronize];
if([savedmod isEqualToString:modified])
{
NSLog(@文件尚未修改。
return NO;
} else
{
NSLog(@文件已修改。
return YES;
}
}
输出:
b $ b
2011-08-01 01:25:51.088 WrightsCS [2858:903]错误:无法打开文件some_file.txt,因为没有文件。
2011-08-01 01:25:51.092 WrightsCS [2858:903]保存的修改日期:(null)
2011-08-01 01:25:51.093 WrightsCS [2858:903]上次修改日期:(null)
2011-08-01 01:25:51.095 WrightsCS [2858:903] Feed没有被修改。
解决方案
p>
您可以使用 NSURLConnection
和 NSURLRequest
的文件,那么您可以查找 Last-Modified 键:
- (void) sendRequestForLastModifiedHeaders
{
/ *发送文件修改日期请求* /
NSURLRequest * modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:@http://www.wrightscs.com/some_file .txt]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[[NSURLConnection alloc] initWithRequest:modReq delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
/ *将响应转换为NSHTTPURLResponse,
allHeaderFields属性,然后获取
Last-Modified键。
* /
NSString * last_modified = [NSString stringWithFormat:@%@,
[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@Last-Modified]
NSLog(@Last-Modified:%@,last_modified);
}
I need to get the file modification date of a remote file.
Obviously, attributes
in this example returns NULL:
- (BOOL) fileHasBeenModifiedSinceLastLaunch
{
NSError * err = nil;
NSDictionary * attributes = [FILEMANAGER attributesOfItemAtPath:[NSURL URLWithString:@"http://www.wrightscs.com/some_file.txt"] error:&err];
NSString * modified = [NSString stringWithFormat:@"%@",[attributes objectForKey:@"NSFileModificationDate"]];
NSString * savedmod = [[NSUserDefaults standardUserDefaults] objectForKey:@"kFeedLastModified"];
NSLog(@"Error: %@",[err localizedDescription]);
NSLog(@"Saved Modified Date: %@", savedmod);
NSLog(@"Last Modified Date: %@", modified);
[[NSUserDefaults standardUserDefaults] setObject:modified forKey:@"kFeedLastModified"];
[[NSUserDefaults standardUserDefaults] synchronize];
if ( [savedmod isEqualToString:modified] )
{
NSLog(@"File has not been modified.");
return NO;
} else
{
NSLog(@"File has been modified.");
return YES;
}
}
Output:
2011-08-01 01:25:51.088 WrightsCS[2858:903] Error: The file "some_file.txt" couldn’t be opened because there is no such file.
2011-08-01 01:25:51.092 WrightsCS[2858:903] Saved Modified Date: (null)
2011-08-01 01:25:51.093 WrightsCS[2858:903] Last Modified Date: (null)
2011-08-01 01:25:51.095 WrightsCS[2858:903] Feed has not been modified.
解决方案
So I was able to figure it out.
You can use NSURLConnection
and NSURLRequest
to get the http headers of the file, then you can look for the key Last-Modified:
- (void) sendRequestForLastModifiedHeaders
{
/* send a request for file modification date */
NSURLRequest *modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.wrightscs.com/some_file.txt"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[[NSURLConnection alloc] initWithRequest:modReq delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
/* convert response into an NSHTTPURLResponse,
call the allHeaderFields property, then get the
Last-Modified key.
*/
NSString * last_modified = [NSString stringWithFormat:@"%@",
[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Last-Modified"]
NSLog(@"Last-Modified: %@", last_modified );
}
这篇关于如何获取远程文件上次修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文