您的位置:首页 > 编程语言 > Java开发

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

2015-04-02 10:36 337 查看
原文:使用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 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;
		
		//download the jpg file
		URL 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");
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: