Httpclient 下载资源

HttpClient 下载网络资源

所需commons-httpclient-3.0.1.jar及其依赖包

package cn.test.softcrawltool.utils.net;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import cn.yicha.softcrawltool.utils.CommonUtils;
import static cn.test.softcrawltool.utils.consts.SoftCrawlConst.*;

/**
* 功能: 软件资源下载
*
* @author liaoyq
*
*/
public class DownloadUtil {

private static final Logger LOG = Logger.getLogger(DownloadUtil.class);

private static final int BUFFER_SIZE = 10240; //10K

public static boolean downloadBinaryResource(String url, String referer, String rootpath) {
File rootDir = CommonUtils.createFileDirectory(rootpath);

HttpClient httpClient = getHttpClient();
GetMethod getMethod = getMethod(url, referer);

InputStream in = null;
DataOutputStream dos = null;
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
String newurl = getMethod.getResponseHeader("Content-Location").getValue();
String filename = getFilename(newurl);
in = getMethod.getResponseBodyAsStream();
if (in != null) {
dos = new DataOutputStream(new FileOutputStream(new File(rootDir, filename)));
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
while ((bytes = in.read(buffer)) != -1)
dos.write(buffer, 0, bytes);
return true;
}
} else {
LOG.error("Method failed: " + getMethod.getStatusLine());
}
} catch (Exception e) {
LOG.error("download soft resource failed!" + url, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
}
}
getMethod.releaseConnection();
}
return false;
}

public static String downloadPage(String url, String pageEncoding) {
StringBuffer content = new StringBuffer(10240);
HttpClient httpClient = getHttpClient();
GetMethod getMethod = getMethod(url, null);

InputStream in = null;
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode == HttpStatus.SC_OK) {
in = getMethod.getResponseBodyAsStream();
if (in != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(in, pageEncoding));
String line = null;
while ((line = br.readLine()) != null) {
content.append(line + "\n");
}
return content.toString();
}
}
} catch (Exception e) {
LOG.error("download page content failed!" + url, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
getMethod.releaseConnection();
}
return null;
}

private static HttpClient getHttpClient() {
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpClient httpClient = new HttpClient(connectionManager);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
ConnectionTimeout);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(
SoTimeout);
return httpClient;
}

private static GetMethod getMethod(String url, String refererUrl) {
GetMethod getMethod = new GetMethod(url);
getMethod.setFollowRedirects(true);// 自动重定向
if (!StringUtils.isEmpty(refererUrl))
getMethod.setRequestHeader(Referer, refererUrl);// 解决不能下载问题
getMethod.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
getMethod.setRequestHeader(COOKIE, COOKIE_VALUE);
getMethod.setRequestHeader(USER_AGENT, USER_AGENT_VALUE);
getMethod.setRequestHeader(Connection, Connection_VALUE);
return getMethod;
}

private static String getFilename(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
}
// JBuilder API Decompiler stub source generated from class file // 2010-1-15 // -- implementation of methods is not available package org.apache.commons.httpclient; // Imports import java.io.IOException; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.logging.Log; public class HttpClient { // Fields private static final Log LOG; private HttpConnectionManager httpConnectionManager; private HttpState state; private HttpClientParams params; private HostConfiguration hostConfiguration; // Constructors public HttpClient() { } public HttpClient(HttpClientParams params) { } public HttpClient(HttpClientParams params, HttpConnectionManager httpConnectionManager) { } public HttpClient(HttpConnectionManager httpConnectionManager) { } // Methods public synchronized HttpState getState() { return null;} public synchronized void setState(HttpState state) { } public synchronized void setStrictMode(boolean strictMode) { } public synchronized boolean isStrictMode() { return false;} public synchronized void setTimeout(int newTimeoutInMilliseconds) { } public synchronized void setHttpConnectionFactoryTimeout(long timeout) { } public synchronized void setConnectionTimeout(int newTimeoutInMilliseconds) { } public int executeMethod(HttpMethod method) throws IOException, HttpException { return 0;} public int executeMethod(HostConfiguration hostConfiguration, HttpMethod method) throws IOException, HttpException { return 0;} public int executeMethod(HostConfiguration hostconfig, HttpMethod method, HttpState state) throws IOException, HttpException { return 0;} public String getHost() { return null;} public int getPort() { return 0;} public synchronized HostConfiguration getHostConfiguration() { return null;} public synchronized void setHostConfiguration(HostConfiguration hostConfiguration) { } public synchronized HttpConnectionManager getHttpConnectionManager() { return null;} public synchronized void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) { } public HttpClientParams getParams() { return null;} public void setParams(HttpClientParams params) { } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值