import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* URL链接,下载所访问的网页
*/
public class UrlConnection {
public static void main(String[] args) {
//URL地址数组
String[] urls = new String[]{"http://991690137.iteye.com/blog/1946378",
"http://991690137.iteye.com/blog/1946173","http://991690137.iteye.com/blog/1946149",
"http://991690137.iteye.com/blog/1946149","http://991690137.iteye.com/blog/1946119",
"http://991690137.iteye.com/blog/1946123","http://991690137.iteye.com/blog/1946133",
"http://991690137.iteye.com/blog/1944394","http://991690137.iteye.com/blog/1944245",
"http://991690137.iteye.com/blog/1942772","http://991690137.iteye.com/blog/1942767",
"http://991690137.iteye.com/blog/1942736","http://991690137.iteye.com/blog/1942736",
"http://991690137.iteye.com/blog/1942717"};
//遍历数组
for(int i=0;i<urls.length;i++){
//循环访问100次
for(int j=1;j<100;j++){
System.out.println("beging...");
//将访问的网页下载并保存在L:/html/indexn.html
DownLoadPages(urls[i],"L:/html/index"+i+".html");
System.out.println("end.");
try {
//每次访问休息1.5s,目的是减轻所访问网页所在服务器的压力,再者访问过快会导致很多意想不到的问题
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 下载网页 或 文件
* @param urlStr 网页地址 比如: http://www.163.com
* @param outPath 文件输出路径
*/
public static void DownLoadPages(String urlStr, String outPath)
{
/** 读入的输入流是字节流
* chByte是读入的每个字节所转成int类型的表示
* */
int chByte = 0;
/** 网络的url地址 */
URL url = null;
/** http连接 */
HttpURLConnection httpConn = null;
/** 输入流 */
InputStream in = null;
/** 文件输出流 */
FileOutputStream out = null;
try
{
url = new URL(urlStr);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
in = httpConn.getInputStream();
out = new FileOutputStream(new File(outPath));
chByte = in.read();
while (chByte != -1)
{
out.write(chByte);
chByte = in.read();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();
in.close();
httpConn.disconnect();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}