每天仅在连接互联网时查看 运行 一次
Check to run only once a day and when internet is connected
我有一个 java 代码,它必须每天保存一次来自 URL 的图像。每次 windows 启动和连接到互联网时,我想将 windows 启动文件夹中的可执行 jar 文件放入 运行;但是,windows 每天可能启动不止一次。所以,我希望我的代码检查是否已经 运行 并在今天保存图像,它不会再次 运行 (保存图像的名称是墙纸,我不想更改它的名称) .我怎样才能做到这一点?谢谢。
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);
}
我有一个 java 代码,它必须每天保存一次来自 URL 的图像。每次 windows 启动和连接到互联网时,我想将 windows 启动文件夹中的可执行 jar 文件放入 运行;但是,windows 每天可能启动不止一次。所以,我希望我的代码检查是否已经 运行 并在今天保存图像,它不会再次 运行 (保存图像的名称是墙纸,我不想更改它的名称) .我怎样才能做到这一点?谢谢。
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);
}