httpclient4.3下载远程图片,设置user-agent和伪装成google的refer, 并设置timeout
首先maven中加入jar依赖关系
org.apache.httpcomponents
httpclient
4.3.6
org.slf4j
slf4j-api
1.7.10
org.apache.commons
commons-io
1.3.2
Java代码import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ImageDownloader {
private static final String USER_AGENT = "Mozilla/5.0 Firefox/26.0";
private static Logger logger = LoggerFactory.getLogger(ImageDownloader.class);
private static final int TIMEOUT_SECONDS = 120;
private static final int POOL_SIZE = 120;
private static CloseableHttpClient httpclient;
public static void main(String[] args) throws ClientProtocolException, IOException {
ImageDownloader p_w_picpathDownloader = new ImageDownloader();
p_w_picpathDownloader.initApacheHttpClient();
String p_w_picpathUrl = "http://bagua.40407.com/uploads/allimg/110926/3808-110926112412.jpg";
p_w_picpathDownloader.fetchContent(p_w_picpathUrl);
p_w_picpathDownloader.destroyApacheHttpClient();
}
public void initApacheHttpClient() {
// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(TIMEOUT_SECONDS * 1000)
.setConnectTimeout(TIMEOUT_SECONDS * 1000).build();
// Create an HttpClient with the given custom dependencies and
// configuration.
httpclient = HttpClients.custom().setUserAgent(USER_AGENT).setMaxConnTotal(POOL_SIZE)
.setMaxConnPerRoute(POOL_SIZE).setDefaultRequestConfig(defaultRequestConfig).build();
}
private void destroyApacheHttpClient() {
try {
httpclient.close();
} catch (IOException e) {
logger.error("httpclient close fail", e);
}
}
public void fetchContent(String p_w_picpathUrl) throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet(p_w_picpathUrl);
httpget.setHeader("Referer", "http://www.google.com");
System.out.println("executing request " + httpget.getURI());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() >= 400) {
throw new IOException("Got bad response, error code = " + response.getStatusLine().getStatusCode()
+ " p_w_picpathUrl: " + p_w_picpathUrl);
}
if (entity != null) {
InputStream input = entity.getContent();
OutputStream output = new FileOutputStream(new File("D:\\website\\1.gif"));
IOUtils.copy(input, output);
output.flush();
}
} finally {
response.close();
}
}
}
转载出处:http://laravel.iteye.com/blog/2180948