java小白一名,看了自己动手写爬虫这本书,第一个例子完全按书中的代码敲下来的,编译无错误,但运行总是出现异常,下边是代码和错误内容,求大神帮忙
package zhuawang;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.*;
public class zhuawang {
private static HttpClient httpClient=new HttpClient();
//设置代理服务器
static{
//设置代理服务器的IP地址和端口
httpClient.getHostConfiguration().setProxy("172.27.35.1", 8080);
}
public static boolean downloadPage(String path) throws HttpException,IOException{
InputStream input=null;
OutputStream output=null;
//得到POST方法
PostMethod postMethod=new PostMethod(path);
//测试post方法的参数
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name","baidu");
postData[1] = new NameValuePair("pasword","123456");
postMethod.addParameters(postData);
//执行,返回状态码
int statusCode = httpClient.executeMethod(postMethod);
//针对状态码进行处理(简单起见,只处理返回值为200的状态码)
if(statusCode == HttpStatus.SC_OK)
{
input = postMethod.getResponseBodyAsStream();
//得到文件名
String filename = path.substring(path.lastIndexOf('/')+1);
//获得文件输出流
output = new FileOutputStream(filename);
//输出到文件
int tempByte = -1;
while((tempByte=input.read())>0)
{
output.write(tempByte);
}
//关闭输出流
if(input!=null)
input.close();
if(output!=null)
output.close();
return true;
}
return false;
}
//测试代码
public static void main(String[] args) {
//抓取百度首页
try{
zhuawang.downloadPage("http://www.baidu.com");
}catch (HttpException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
异常提示
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66)
at zhuawang.zhuawang.<clinit>(zhuawang.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more