??xml version="1.0" encoding="utf-8" standalone="yes"?> 说明Q本博的一切内容均可{载,但必L意出处?a target="_blank">http://www.lelelog.com(乐乐日志) 一、HttpUrlConnectionq接之模拟浏览器 在诸多的|站中,特别是大型的|站Q设|了必须是浏览器的请求才会回应。之所以这栯|,是Z防止我们q种目l他产生无意义的hQ往往q种h都是大批量,对其服务器生负P。那Z解决q个问题Q我们需要在httph中,d属性?/p> HttpURLConnection urlConn; urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); q样p|好了,你可以随意设|你的操作系l|览器|版本Q只要正就OK了。这样就可以正常有效地访问其|站了。他可不知道你是不是览器?font color="#ff00ff">你即使是条狗Q他也不知道?/font> 二、完整的讄HttpUrlConnection的属性?/p> HttpURLConnection urlConn; urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); urlConn.setUseCaches(false);//不要用cacheQ用了也没有什么用Q因为我们不会经常对一个链接频J访问。(针对E序Q?br> urlConn.setConnectTimeout(6 * 1000); 有什么疑问的话,可以查看JDK的API文Q这个可以实时看。至于ؓ什么要讄 gzipQ而又不设|deflateQ原因如下,有些|站他不你能接受什么压~格式,l统也会压羃|页内容传给你。当然IEQFF能处理好q些内容。所以我们通过览器查看的时候完全正常。一般gzip的压~可以将一?3K的文件压~成7KQ这样会节约不少带宽Q但服务器的负荷q没有减轻,因ؓ他要压羃文g呀。至于ؓ什么不用deflateQ是׃l大多数|站的压~方式是用gzipQ而在有些|站中,明明是用的gzip却返回deflate的压~标识。这有什么意义呢Q所以干脆就告诉服务器,我不接受deflateQ因Z太丑了,又长Q哪像gzipq么潮呀。呵呵,对于览量大的静态网|务器Q这样做很是必要?00M的独享服务器Q他也只?00M呀?/p> 三、开始采集某个网늚内容 该方法就是传入一个HttpUrlConnection的链接,和该文g的字W集~码Q就可以q回其网内容了?/p> public static String getContentFromIn(HttpURLConnection urlConn, String charset) { if (null == in) { } catch (UnsupportedEncodingException e) { x一个简单的采集工具c诞生了Q他的优在于,代码,不用引入M包。纯JDKQ一栯q许多事。有时不希望把本来简单的事情搞得复杂化。虽然不要创造重复的轮子Q但我们不能U拿来主义,像q样一个简单的功能Q不需要搞得太复杂。只要不DRY可以了Q这栯׃能一点点地进步? 以上有什么错误,q请包涵。。。。。。该文章来自:乐乐日志(C每个快乐的日??
urlConn.setRequestProperty("Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
urlConn.setRequestProperty("Accept-Language", "zh-cn");
urlConn.setRequestProperty("UA-CPU", "x86");
urlConn.setRequestProperty("Accept-Encoding", "gzip");//Z么没有deflate?br> urlConn.setRequestProperty("Content-type", "text/html");
urlConn.setRequestProperty("Connection", "close"); //keep-AliveQ有什么用呢,你不是在讉K|站Q你是在采集。嘿ѝ减d人的压力Q也是减轻自己?
urlConn.setReadTimeout(6*1000);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
BufferedReader br = null;
StringBuilder content = new StringBuilder(200);
InputStream in = null;
try {
if(null == urlConn){
return "";
}
if (StringTools.isNotEmpty(urlConn.getContentEncoding())) {
String encode = urlConn.getContentEncoding().toLowerCase();
if (StringTools.isNotEmpty(encode) && encode.indexOf("gzip") >= 0) {
in = new GZIPInputStream(urlConn.getInputStream());
}
}
in = urlConn.getInputStream();
}
if (null != in) {
br = new BufferedReader(new InputStreamReader(in, charset));
String line = "";
while ((line = br.readLine()) != null) {
content.append(line);
}
}
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
}
return content.toString();
}
]]>
Map 提供了一个更通用的元素存储方法?Map 集合cȝ于存储元素对Q称?#8220;?#8221;?#8220;?#8221;Q,其中每个键映到一个倹{?从概念上而言Q您可以?List 看作是具有数值键?Map?而实际上Q除?List ?Map 都在定义 java.util 中外Q两者ƈ没有直接的联pR本文将着重介l核?Java 发行套g中附带的 MapQ同时还介l如何采用或实现更适用于您应用E序特定数据的专?Map?/span>
1. blank finals
java allows the creation of blank finals, which are fields that are declared as final but are not given an initialization value. in all cases, the blank final must be initialized before it is used, and the compiler ensures this.However, blank finals provied much more flexibility in the use of the final keyword since.the blank final should be initialized in the constructor.
2. Final methods
Tow reasons for final methods. The first is to put a "lock" on the method to prevent any inheriting class from changing its meaning. This is done for design reasons when you want to make sure that a method's behavior is retained during inheritance and cannot be overidden.
The second reason for final methods is efficiency. If u make a method final,you are allowing the compiler to turn any calls to that method into inline calls.
However, it's better to not trust that the compiler is able to do this and make a method final only if it's quite small or if you want to explicitly prevent overriding.
3.Final class
Defining the class as final simply prevents inheritance nothing more.