[英]Check to run only once a day and when internet is connected
I have a java code that must save an image from a URL, once a day. 我有一个Java代码,必须每天一次从URL保存图像。 I want to put the executable jar file in windows startup folder to run every time the windows starts and when connects to internet;
我想将可执行的jar文件放在Windows启动文件夹中,以便在每次Windows启动以及连接到Internet时运行; but, the windows may be start more than one time every day.
但是,窗户每天可能会启动多次。 So, I want my code checks if has been ran and saved the image today, it don't run again (the name of saved image is Wallpaper and i don't want to change its name).
因此,我希望我的代码检查今天是否已运行并保存了图像,因此不再运行(保存的图像名称为Wallpaper,并且我不想更改其名称)。 How can I do this?
我怎样才能做到这一点? Thank you.
谢谢。
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小时以上时,才可以下载映像。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.