package com.guet.crawlerbyself;
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.PostMethod;
public class RetrivePage
{
private static HttpClient httpClient = new HttpClient();
//set a proxy server
static
{
//set IP Address and the port of the proxy server
httpClient.getHostConfiguration().setProxy("localhost",8080);
}
public static boolean downloadPage(String path) throws HttpException, IOException
{
InputStream input = null;
OutputStream output = null;
//get post method
PostMethod postMethod = new PostMethod("www.baidu.com");
//set parameters of post method
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name","baidu");
postData[1] = new NameValuePair("password","123");
//Adds an array of parameters to be used in the POST request body.
postMethod.addParameters(postData);
//execute and return status code.
int statusCode = httpClient.executeMethod(postMethod);
//this place,only process the status 200.
if(statusCode == HttpStatus.SC_OK)
{
input = postMethod.getResponseBodyAsStream();
String filename = path.substring(path.lastIndexOf('/')+1);
output = new FileOutputStream(filename);
//output to the file
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)
{
System.out.println("execute");
try{
RetrivePage.downloadPage("http://www.baidu.com/");
}catch(HttpException e){
System.out.println("httpexeption");
e.printStackTrace();
}catch(IOException e){
System.out.println("ioexeption");
e.printStackTrace();
}
}
}
运行时弹出对话框显示:could not find the main class. program will exit
console中显示:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.commons.httpclient.HttpClient.(HttpClient.java:66)
at com.guet.crawlerbyself.RetrivePage.(RetrivePage.java:17)
Exception in thread "main"
所需的apache的开源包我已经导入了,代码写完后也没报错,运行的时候就报错了.
我在网上google了一下,有的说把jdk改成1.4的之类的答案,我试过了,还是老问题没什么效果.希望之前碰到这个问题的朋友帮忙解答一下.