Original: use Java to set Bing daily wallpaper to Win7 wallpaper
Source code: Http://www.zuidaima.com/share/1550463716592640.htm
Previously published "Using Java to set the Bing daily wallpaper to Win7 wallpaper", read the comments and decided to make this idea on the Win7.
In fact, the implementation on the Win7 and on the implementation of Ubuntu is not very big difference, the previous parsing XML and download the image is the same, the difference is to set the wallpaper behind.
Win7 the code to set up the wallpaper is a bit cumbersome and not as good as the Linux direct call command. Later, when the boot entry was set, it was found that the sequence of running the jar file was preceded by the start of the network service, so I used a do...while to solve the problem (see the code for the main function).
Compiling this program requires the use of the Jna.jar package and the Jna-platform.jar package.
It is recommended to package its compilation into an executable jar file, then click on the Start menu----" All Programs "-----" Start "Right-click" Open "to drag the jar file into the open folder, so that the wallpaper will be automatically switched on every day ~ ~
Package Com.zuidaima;import Java.io.ioexception;import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Org.w3c.dom.document;import Org.xml.sax.saxexception;import Java.io.bufferedreader;import Java.io.DataInputStream ; Import Java.io.file;import java.io.fileoutputstream;import java.io.inputstream;import java.io.InputStreamReader; Import Java.net.url;import Java.util.hashmap;import Com.sun.jna.native;import Com.sun.jna.platform.win32.windef.uint_ptr;import com.sun.jna.win32.*;/*** @author Www.zuidaima.com**/public class mywallpaper {public static void main (string[] argc) throws Parserconfigurationexception, Saxexception, ioexception{ Mywallpaper wallpaper = new Mywallpaper ();d o{string path = Wallpaper.getthepath (); wallpaper.downloadwallpaper (path); Wallpaper.settingwallpaper ();} while (Wallpaper.isconnect ()!=true);} Public interface SPI extends Stdcalllibrary {long spi_setdeskwallpaper = 20; Long spif_updateinifile = 0x01; Long spif_sendwininichange = 0x02; SPI INSTANCE = (SPI) native.loadlibrary ("User32", Spi.class, New Hashmap<object, object> () {{PU T (Option_type_mapper, W32apitypemapper.unicode); Put (Option_function_mapper, W32apifunctionmapper.unicode); } }); Boolean SystemParametersInfo (Uint_ptr uiaction, Uint_ptr Uiparam, String pvparam, UIN T_ptr Fwinini); }public Boolean Isconnect () throws Ioexception{runtime Runtime = Runtime.getruntime (); Process process = Runtime.exec ("ping www.baidu.com"); InputStream is = Process.getinputstream (); InputStreamReader ISR = New InputStreamReader (IS); BufferedReader br = new BufferedReader (ISR), if (Br.readline () ==null) {//system.out.println ("The Network is wrong!"); return false;} ELSE{//SYSTEM.OUT.PRINTLN ("The Network is well"); return true;}} Public String Getthepath () throws Parserconfigurationexception, Saxexception, IoexCeption{//getting the path of the Bing jpg picture via analysis xmldocumentbuilderfactory factory = Documentbuilderfactory . newinstance (); Documentbuilder builder = Factory.newdocumentbuilder ();D ocument Document = Builder.parse ("http://www.bing.com/ Hpimagearchive.aspx?format=xml&idx=0&n=8 ");d ocument.normalize (); String relativepath =document.getelementsbytagname ("url"). Item (0). Gettextcontent (); String Path = "http://www.bing.com/" +relativepath;return Path;} public void Downloadwallpaper (String path) throws ioexception{//download the jpg fileURL url = new URL (path);D Atainputstre Am dis = new DataInputStream (Url.openstream ()); FileOutputStream fos = new FileOutputStream (New File ("c:\\wallpaper.jpg")); byte[] buffer = new Byte[1024];int length; while ((Length=dis.read (buffer)) >0) {fos.write (buffer,0,length);} Dis.close (); Fos.close ();} public void Settingwallpaper () {String localpath = "C:\\wallpaper.jpg"; Spi. INSTANCE. SystemParametersInfo (New Uint_ptr (SPI. Spi_setdeskwallpaper), new Uint_ptr (0), LocalPath, New Uint_ptr (SPI. Spif_updateinifile | Spi. Spif_sendwininichange));}}
Use Java to set Bing daily wallpaper to Win7 wallpaper