Java internet连接更改时回调
我有一个java代码,可以将图片从web下载到硬盘。我想在启动时运行它,当互联网连接时,它会下载图片,否则会等待互联网连接。一种方法是随时检查互联网连接,但我在寻找更好的方法吗? 我希望它支持windows操作系统。 这是我的密码:Java internet连接更改时回调,java,internet-connection,Java,Internet Connection,我有一个java代码,可以将图片从web下载到硬盘。我想在启动时运行它,当互联网连接时,它会下载图片,否则会等待互联网连接。一种方法是随时检查互联网连接,但我在寻找更好的方法吗? 我希望它支持windows操作系统。 这是我的密码: import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.UnknownHostException;
public class SaveImageFromUrl {
public static void main(String[] args) throws Exception {
String imageUrl = "http://imgs.yooz.ir/yooz/walls/yooz-950616.jpg";
//the imageUrl changes every few days.
String destinationFile = "C:\\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();
}
}
}
你能添加下载的代码吗?你想支持所有操作系统吗?看看。你可以在连接到Internet时触发你的程序。添加我的代码以下载@sinclair。我希望它支持windows OS@sidgate。你可以添加代码以下载吗?是否希望支持所有操作系统?请查看。你可以在连接到Internet时触发你的程序。添加我的代码,下载@sinclair。我希望它支持windows OS@sidgate。