import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class HttpDownLoad { public static void main(String[] args) { //建立HttpCilent對象,--客戶端 HttpClient client=new DefaultHttpClient(); String path="http://rss.sina.com.cn/roll/sports/hot_roll.xml"; //建立請求。----Get:HttpGet HttpGet get=new HttpGet(path); //客戶端執行請求,獲得響應對象 try { HttpResponse response=client.execute(get); //獲得響應碼:響應碼和服務器端發送給客戶端的數據都封裝在HttpResponse裏。 int code=response.getStatusLine().getStatusCode(); if(code==200){ //if(code==HttpURLConnection.HTTP_OK) HttpEntity entity=response.getEntity(); byte[]a=EntityUtils.toByteArray(entity); OutputStream os=new FileOutputStream("e:\\xx.xml"); //將byte數組的數據寫到文件中。 os.write(a); os.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }