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.*;

public class MyWallpaper {
	public static void main(String[] argc) throws ParserConfigurationException, SAXException, IOException
	{
		MyWallpaper wallpaper = new MyWallpaper();
		
		do{
		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>() {
	         {
	            put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
	            put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
	         }
	      });

	      boolean SystemParametersInfo(
	          UINT_PTR uiAction,
	          UINT_PTR uiParam,
	          String pvParam,
	          UINT_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 xml
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse("http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=8");
		document.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 file
		URL url = new URL(path);
		DataInputStream 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));
	}
}
最近下载更多
zhushuai001  LV1 2022年4月29日
sadfadf  LV1 2021年12月9日
Naptie  LV1 2020年11月22日
eihqnh  LV1 2020年8月23日
jialan75  LV1 2019年11月11日
c879244324  LV8 2019年9月4日
bg6ejh  LV1 2018年11月6日
Timberwolves  LV1 2018年10月8日
胡海龙  LV1 2018年9月20日
justmemoryl  LV3 2018年8月29日
最近浏览更多
heqian  LV16 2023年1月10日
zhushuai001  LV1 2022年4月29日
allen平凡之路  LV12 2021年10月18日
hollejianren 2021年10月18日
暂无贡献等级
Naptie  LV1 2020年11月22日
wei112233  LV15 2020年5月20日
yn2333  LV8 2020年2月2日
jialan75  LV1 2019年11月11日
c879244324  LV8 2019年9月4日
youwuzuichen  LV10 2019年8月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友