java 讀取 rss(gzip格式)

[b]用java自帶的gzip解壓就可以了!這種只限於gzip格式[/b]

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();
}

[b]普通的xml格式rss可以這樣做[/b]

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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章