標籤:bing 每日壁紙 設定 win7壁紙 java
原文:使用java將bing的每日壁紙設定為ubuntu壁紙
原始碼:http://www.zuidaima.com/share/1550463714806784.htm
早上起來瀏覽bing的時候突然有了想要把bing的每日壁紙設為ubuntu的案頭壁紙的想法,中午從機房回來後就開始寫代碼。先是解析xml,擷取壁紙的,然後是下載壁紙,最後調用ubuntu的命令設定壁紙。
在我的ubuntu13.04上運行成功。建議將這個java檔案編譯然後打包成jar,修改/etc/rc.local 在exit 0 前添加該jar檔案的運行命令,比如我的寫成 java -jar /home/kongkongyzt/wallpaper.jar
這樣每天開機就會自動換壁紙了。
代碼量很小,不規範的地方很多,希望大家指出~~
package com.zuidaima.swing.demo;/***@author www.zuidaima.com**/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.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.net.URL;public class wallpaper {public static void main(String[] argc) throws ParserConfigurationException, SAXException, IOException{//getting the path of the bing jpg picture via analysis xmlDocumentBuilderFactory 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;//download the jpg fileURL url = new URL(path);DataInputStream dis = new DataInputStream(url.openStream());FileOutputStream fos = new FileOutputStream(new File("/tmp/wallpaper.jpg"));byte[] buffer = new byte[1024];int length;while((length=dis.read(buffer))>0){fos.write(buffer,0,length);}dis.close();fos.close();Process process = Runtime.getRuntime().exec("gsettings set org.gnome.desktop.background picture-uri file:///tmp/wallpaper.jpg");}}
使用java將bing的每日壁紙設定為ubuntu壁紙