Java 检查每天仅运行一次,并在连接internet时运行
我有一个java代码,必须每天保存一次URL中的图像。我想把可执行jar文件放在windows启动文件夹中,以便在每次windows启动和连接到internet时运行;但是,windows每天可能会启动不止一次。因此,我希望我的代码检查是否已经运行并保存了今天的图像,它不会再次运行(保存的图像的名称是墙纸,我不想更改其名称)。我该怎么做?多谢各位Java 检查每天仅运行一次,并在连接internet时运行,java,windows,Java,Windows,我有一个java代码,必须每天保存一次URL中的图像。我想把可执行jar文件放在windows启动文件夹中,以便在每次windows启动和连接到internet时运行;但是,windows每天可能会启动不止一次。因此,我希望我的代码检查是否已经运行并保存了今天的图像,它不会再次运行(保存的图像的名称是墙纸,我不想更改其名称)。我该怎么做?多谢各位 public static void main(String[] args) throws Exception { String imageU
public static void main(String[] args) throws Exception {
String imageUrl ="http://imgs.yooz.ir/fc/m/medium-news/0170220/656760513-0.jpg";
String destinationFile = "E:\\Picture\\Wallpaper.jpg";
saveImage(imageUrl, destinationFile);
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException {
URL url = new URL(imageUrl);
byte[] b = new byte[2048];
int length;
try {
InputStream is=url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}catch (UnknownHostException e){
e.printStackTrace();
}
}
仅当当前时间大于目标文件上次修改时间后的24小时时,您才能下载图像。谢谢httPants。但我是java的初学者;如何查找文件的修改时间?
final long dayMilliSec=24*60*60*1000;
final long diffMilliSec=(3*60+30)*60*1000;
File file=new File(location);
long modDay=(file.lastModified()+diffMilliSec)/dayMilliSec;
long currDay=(new Date().getTime()+diffMilliSec)/dayMilliSec;
//int a=(int) Math.ceil(b);
if (currDay==modDay){
System.exit(0);
}