使用java將bing的每日壁紙設定為ubuntu壁紙

來源:互聯網
上載者:User

標籤: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壁紙

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.