java 读取 rss(gzip格式)

用java自带的gzip解压就可以了!这种只限于gzip格式
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;
public class ConnectTester {

	public static void requestInfo() {
		URL url = null;
		try {
			url = new URL("http://blog.sina.com.cn/rss/xujinglei.xml");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();		
			 GZIPInputStream gzin=new GZIPInputStream(conn.getInputStream()); 
			 FileOutputStream fout=new FileOutputStream("E:\\test.xml"); 
			 byte[] buf=new byte[1024];
			   int num;
			   while ((num=gzin.read(buf,0,buf.length))!=-1) { 
			    fout.write(buf,0,num); 
			   }
			   gzin.close();
			   fout.close();  

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String args[]) {
		requestInfo();
	}

普通的xml格式rss可以这样做
public static void requestInfo() {

		URL url;
		try {
			url = new URL("http://www.people.com.cn/rss/politics.xml");

			URLConnection conn;
			conn = url.openConnection();

			BufferedReader bufferedreader = new BufferedReader(
					new InputStreamReader(conn.getInputStream()));
			String line = null;
			while ((line = bufferedreader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String args[]) {
		requestInfo();
	}

你可能感兴趣的:(java,.net,xml,Blog,sun)