import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.*;
public class downloadFileByUrl {
public static void main(String[] args)throws Exception
{
catchallseed("http://www.cc98.org/");
}
public static void catchallseed(String url)
{
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
byte[] responseBody = getMethod.getResponseBody();
FileOutputStream fos=new FileOutputStream("F://htmls/2.html");
fos.write(responseBody);
//System.out.println(new String(responseBody));
}
catch (HttpException e)
{
System.out.println("Please check your provided http address!");
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
} finally
{
getMethod.releaseConnection();
}
}
}
添加两个包:commons-httpclient;commons-codec
import java.io.IOException;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.*;
public class downloadFileByUrl {
public static void main(String[] args)throws Exception
{
catchallseed("http://www.cc98.org/");
}
public static void catchallseed(String url)
{
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
byte[] responseBody = getMethod.getResponseBody();
FileOutputStream fos=new FileOutputStream("F://htmls/2.html");
fos.write(responseBody);
//System.out.println(new String(responseBody));
}
catch (HttpException e)
{
System.out.println("Please check your provided http address!");
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
} finally
{
getMethod.releaseConnection();
}
}
}
添加两个包:commons-httpclient;commons-codec
转载于:https://blog.51cto.com/slddyb/408856