|
以下是代码片段:
<PRE class=csharp name="code">package com.core.crawl;
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
import com.core.http.Http;
public class WebSpider implements Runnable{ private Http http = new Http();
private String webAddress = ""; private String destFile = ""; public void setWebAddress(String webAddress){ this.webAddress = webAddress; } public void setDestFile (String destFile){ this.destFile = destFile; } public boolean download() throws IOException, InterruptedException {
HttpURLConnection httpConn = null;
try { URL url = new URL(webAddress); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestMethod("GET"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"); InputStream in = httpConn.getInputStream(); String fileType = http.fileType(httpConn.getContentType()); System.out.println(fileType); FileOutputStream out = new FileOutputStream(new File(destFile + fileType)); int chByte = in.read(); while (chByte != -1) { out.write(chByte); //System.out.println(chByte); chByte = in.read(); } } catch (Exception ex) { System.out.println(ex.toString()); } finally { httpConn.disconnect(); } return true; }
public void run() { try { //System.out.println(Thread.currentThread().getName()); download(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } </PRE> |