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

本文提供了一个简单的Java程序,用于解析Bing每日壁纸的XML数据,下载图片并设置为Ubuntu的桌面壁纸。程序通过修改/etc/rc.local实现开机自动更换壁纸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:使用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");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值