声明:文章内容全都是自己的学习总结,如有不对的地方请大家帮忙指出。有需要沟通交流的可加我QQ群:425120333
主要是通过配置文件加载Proxy信息,为后面的获取获取网页提供帮助。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ProxyServerUtil {
private static final Log logger = LogFactory.getLog(ProxyServerUtil.class);
private static String userAgent = BrowserUtil.getUserAgent();
private volatile static List<Proxy> proxyList = ProxyServerUtil.loadProxy();
private static int index = -1;
private static List<Proxy> loadProxy() {
List<Proxy> list = new ArrayList<Proxy>();
BufferedReader br = null;
Proxy proxy = null;
try {
FileInputStream inputStream = new FileInputStream(new File(StringUtil.getClassPath() + "proxy.txt"));
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
br = new BufferedReader(inputStreamReader);
String line = null;
while ((line = br.readLine()) != null) {
String[] s = line.split(":");
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(s[0].trim(), Integer.parseInt(s[1].trim())));
if (proxy != null && !list.contains(proxy)) {
list.add(proxy);
}
}
} catch (FileNotFoundException e) {
logger.error(ProxyServerUtil.class, e);
} catch (IOException e) {
logger.error(ProxyServerUtil.class, e);
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
logger.error(ProxyServerUtil.class, e);
}
}
logger.info("proxy list size :" + list.size());
return list;
}
public synchronized static Proxy getProxy() {
index++;
if (index >= proxyList.size()) {
userAgent = BrowserUtil.getUserAgent();
index = -1;
}
return proxyList.get(index);
}
public static String getUserAgent() {
return userAgent;
}
}
代码中用到的文件proxy.txt里面存放的内容如下:
115.238.240.136:138
27.188.17.72:9797
222.186.58.27:138
222.163.198.30:8888
115.231.28.67:8888
123.160.166.193:81
211.102.219.131:80
121.40.108.76:80
111.1.23.189:80
180.97.183.9:8888
122.228.86.8:8888
124.74.138.154:80
122.189.58.25:8998
61.167.245.92:8998
211.103.154.216:8080
222.73.5.138:8080
210.14.143.188:80
58.247.45.250:80
122.241.62.181:8998
36.40.214.218:8998
58.247.142.50:80
117.34.47.5:8888
121.207.41.168:808
14.215.89.83:8888
118.178.142.98:138
101.4.138.72:8888
118.244.189.240:80
202.110.80.10:8888
211.90.28.131:8888
218.92.220.59:8080
可自行从网上获取代理ip地址;