??xml version="1.0" encoding="utf-8" standalone="yes"?>国自在线精品视频,久久综合九色综合欧美98,精品中文字幕在线http://www.aygfsteel.com/cenly60/category/21836.html光荣在于qxE...艰巨在于漫长...zh-cnSat, 05 Sep 2009 10:25:51 GMTSat, 05 Sep 2009 10:25:51 GMT60[原创]用HttpUrlConnection抓取|页内容http://www.aygfsteel.com/cenly60/archive/2009/09/05/293979.html6060Sat, 05 Sep 2009 02:58:00 GMThttp://www.aygfsteel.com/cenly60/archive/2009/09/05/293979.htmlhttp://www.aygfsteel.com/cenly60/comments/293979.htmlhttp://www.aygfsteel.com/cenly60/archive/2009/09/05/293979.html#Feedback0http://www.aygfsteel.com/cenly60/comments/commentRss/293979.htmlhttp://www.aygfsteel.com/cenly60/services/trackbacks/293979.html׃目需求,需要采集一些网站的内容。在JAVA领域中,已经存在很多良好的框架用于该应用Q如httpclientQwebharvestq有更多强大的框架。但׃本n目比较单纯Q且Z良好的自我控Ӟ决定直接用JDK中的HttpUrlConnectioncL抓取|页内容。ƈ不是说那些框架不好,而只是不适用本项目?/p>

说明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.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.setUseCaches(false);//不要用cacheQ用了也没有什么用Q因为我们不会经常对一个链接频J访问。(针对E序Q?br>        urlConn.setConnectTimeout(6 * 1000);
        urlConn.setReadTimeout(6*1000);
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);

有什么疑问的话,可以查看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) {
        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());                   
                }
            }

            if (null == in) {
                in = urlConn.getInputStream();
            }
            if (null != in) {
                br = new BufferedReader(new InputStreamReader(in, charset));
                String line = "";
                while ((line = br.readLine()) != null) {
                    content.append(line);
                }
            }

        } catch (UnsupportedEncodingException e) {
            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();
    }

x一个简单的采集工具c诞生了Q他的优在于,代码,不用引入M包。纯JDKQ一栯q许多事。有时不希望把本来简单的事情搞得复杂化。虽然不要创造重复的轮子Q但我们不能U拿来主义,像q样一个简单的功能Q不需要搞得太复杂。只要不DRY可以了Q这栯׃能一点点地进步?

以上有什么错误,q请包涵。。。。。。该文章来自:乐乐日志(C每个快乐的日??

60 2009-09-05 10:58 发表评论
]]>
Java性能优化http://www.aygfsteel.com/cenly60/archive/2009/06/17/282859.html6060Wed, 17 Jun 2009 07:23:00 GMThttp://www.aygfsteel.com/cenly60/archive/2009/06/17/282859.htmlhttp://www.aygfsteel.com/cenly60/comments/282859.htmlhttp://www.aygfsteel.com/cenly60/archive/2009/06/17/282859.html#Feedback0http://www.aygfsteel.com/cenly60/comments/commentRss/282859.htmlhttp://www.aygfsteel.com/cenly60/services/trackbacks/282859.html 原文地址Q?nbsp;http://blogs.oracle.com/olaf/2009/04/java_performance_the_return_of.html

60 2009-06-17 15:23 发表评论
]]>
Java Map 集合cȝ?/title><link>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282854.html</link><dc:creator>60</dc:creator><author>60</author><pubDate>Wed, 17 Jun 2009 06:53:00 GMT</pubDate><guid>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282854.html</guid><wfw:comment>http://www.aygfsteel.com/cenly60/comments/282854.html</wfw:comment><comments>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282854.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/cenly60/comments/commentRss/282854.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/cenly60/services/trackbacks/282854.html</trackback:ping><description><![CDATA[<p><span id="wmqeeuq" class="bodycopy">java.util 中的集合cd?Java 中某些最常用的类?最常用的集合类?List ?Map?List 的具体实现包?ArrayList ?VectorQ它们是可变大小的列表,比较适合构徏、存储和操作Mcd对象元素列表?List 适用于按数值烦引访问元素的情Ş?/span> </p> <p><span id="wmqeeuq" class="bodycopy">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> </p> <br /> 原文地址Q?a >http://www.oracle.com/technology/global/cn/pub/articles/maps1.html</a> <img src ="http://www.aygfsteel.com/cenly60/aggbug/282854.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/cenly60/" target="_blank">60</a> 2009-06-17 14:53 <a href="http://www.aygfsteel.com/cenly60/archive/2009/06/17/282854.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java的内存泄?/title><link>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282802.html</link><dc:creator>60</dc:creator><author>60</author><pubDate>Wed, 17 Jun 2009 03:30:00 GMT</pubDate><guid>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282802.html</guid><wfw:comment>http://www.aygfsteel.com/cenly60/comments/282802.html</wfw:comment><comments>http://www.aygfsteel.com/cenly60/archive/2009/06/17/282802.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/cenly60/comments/commentRss/282802.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/cenly60/services/trackbacks/282802.html</trackback:ping><description><![CDATA[Java的一个重要优点就是通过垃圾攉?Garbage CollectionQGC)自动理内存的回ӞE序员不需要通过调用函数来释攑ֆ存。因此,很多E序员认为Java不存在内存泄漏问题,或者认为即使有内存泄漏也不是程序的责QQ而是GC或JVM的问题。其实,q种x是不正确的,因ؓJava也存在内存泄Ԍ但它的表CC++不同?br /> <br /> 见原文:<a >http://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/</a> <img src ="http://www.aygfsteel.com/cenly60/aggbug/282802.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/cenly60/" target="_blank">60</a> 2009-06-17 11:30 <a href="http://www.aygfsteel.com/cenly60/archive/2009/06/17/282802.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>?JSF ?Managed Bean 中获得request session对象http://www.aygfsteel.com/cenly60/archive/2007/12/25/170416.html6060Tue, 25 Dec 2007 14:56:00 GMThttp://www.aygfsteel.com/cenly60/archive/2007/12/25/170416.htmlhttp://www.aygfsteel.com/cenly60/comments/170416.htmlhttp://www.aygfsteel.com/cenly60/archive/2007/12/25/170416.html#Feedback0http://www.aygfsteel.com/cenly60/comments/commentRss/170416.htmlhttp://www.aygfsteel.com/cenly60/services/trackbacks/170416.html ------------------------------------------------------------------------
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ec = context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();


session:
------------------------------------------------------------------------
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ec = context.getExternalContext();
HttpSession session = (HttpSession) ec.getSession(true);


------------------------------------------------------------------------
获得session后,便可以利用session来获得一些其它有用的东东了。比如:
session.getServletContext().getRealPath("/"); // 盘路径

60 2007-12-25 22:56 发表评论
]]>
Something About Finalhttp://www.aygfsteel.com/cenly60/archive/2007/06/26/126247.html6060Tue, 26 Jun 2007 01:15:00 GMThttp://www.aygfsteel.com/cenly60/archive/2007/06/26/126247.htmlhttp://www.aygfsteel.com/cenly60/comments/126247.htmlhttp://www.aygfsteel.com/cenly60/archive/2007/06/26/126247.html#Feedback0http://www.aygfsteel.com/cenly60/comments/commentRss/126247.htmlhttp://www.aygfsteel.com/cenly60/services/trackbacks/126247.htmlFinal

when using final object references rather than primitives the meaning gets a bit confusing.With a primitive, final makes the value a constant, but with an object refercence,final makes the reference a constant.

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.
  



60 2007-06-26 09:15 发表评论
]]>
[导入]谈JNI技术在嵌入式Y件开发中的应?http://www.aygfsteel.com/cenly60/archive/2007/04/21/112399.html6060Sat, 21 Apr 2007 02:38:00 GMThttp://www.aygfsteel.com/cenly60/archive/2007/04/21/112399.htmlhttp://www.aygfsteel.com/cenly60/comments/112399.htmlhttp://www.aygfsteel.com/cenly60/archive/2007/04/21/112399.html#Feedback0http://www.aygfsteel.com/cenly60/comments/commentRss/112399.htmlhttp://www.aygfsteel.com/cenly60/services/trackbacks/112399.html
文章来源:http://blog.csdn.net/CenLY60/archive/2006/07/04/875591.aspx

60 2007-04-21 10:38 发表评论
]]>
վ֩ģ壺 ̩| | | ̨| | µ| ӥ̶| ʱ| | | | ϲ| ƺ| Դ| ʡ| ̫| º| ˳ƽ| | | | º| ̨| | ǫ| | Ȩ| | | ˮ| | ͩ| | | | | | ͼ| żҴ| | |