使用java将bing的每日壁纸设置为ubuntu壁纸

原文:使用java将bing的每日壁纸设置为ubuntu壁纸

源代码下载地址:

早上起来浏览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 **/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("?format=xml&idx=0&n=8");document.normalize(); String relativePath =document.getElementsByTagName("url").item(0).getTextContent();String path =""+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壁纸

相关文章:

你感兴趣的文章:

标签云: