﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-sblig</title><link>http://www.blogjava.net/sblig/</link><description /><language>zh-cn</language><lastBuildDate>Sat, 18 Apr 2026 13:12:33 GMT</lastBuildDate><pubDate>Sat, 18 Apr 2026 13:12:33 GMT</pubDate><ttl>60</ttl><item><title>跟我学Spring3 学习笔记七 初始化与销毁</title><link>http://www.blogjava.net/sblig/archive/2012/10/18/390644.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Thu, 18 Oct 2012 08:45:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/10/18/390644.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390644.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/10/18/390644.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390644.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390644.html</trackback:ping><description><![CDATA[
              <p><span style="color: #0000ff;"><strong>最后 遗留一个问题，继续探索中....</strong></span></p>
<p> </p>
<p> </p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><a href="/blog/1585027" style="color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记一</span></a></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="font-size: x-small;"><a href="/blog/1585027" style="color: #108ac6;"></a><a href="/blog/1586139" style="color: #108ac6;">跟我学Spring3 学习笔记二</a></span></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><a href="/blog/1586553" style="color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记三</span></a></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="color: #108ac6;"><a href="/blog/1586584" style="color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记四</span></a></span></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="font-size: x-small;"><span style="color: #108ac6;"><a href="/blog/1586584" style="color: #108ac6;"></a></span><a href="/blog/1591360" style="color: #108ac6; line-height: 1.5em;">跟我学Spring3 学习笔记五 注入</a></span></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="font-size: x-small;"><a href="/blog/1591360" style="color: #108ac6; line-height: 1.5em;"></a></span><a href="/blog/1700958" style="line-height: 1.5em; color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记六 注入</span></a></p>
<p> </p>
<p>统一接口：</p>
<p> </p>
<pre name="code" class="java">public interface HelloApi {
	public void sayHello();  
}
</pre>
<p> </p>
<p> </p>
<p>一、延迟初始化：</p>
<p> </p>
<pre name="code" class="java">/**
 * 延迟初始化Bean
 *     延迟初始化也叫做惰性初始化，指不提前初始化Bean，而是只有在真正使用时才创建及初始化Bean。
 *     配置方式很简单只需在&lt;bean&gt;标签上指定 “lazy-init” 属性值为“true”即可延迟初始化Bean。
 */
public class DiLazyInit implements HelloApi{

	public void sayHello() {
		System.out.println("say DiInitDestory");
	}
	
	public DiLazyInit(){
		System.out.println("初始化 DiInitDestory");
	}
}</pre>
<p> </p>
<p> </p>
<p>配置延迟初始化：</p>
<p> </p>
<p> </p>
<pre name="code" class="xml">&lt;!-- 延迟初始化Bean 
	     延迟初始化也叫做惰性初始化，指不提前初始化Bean，而是只有在真正使用时才创建及初始化Bean。
	     配置方式很简单只需在&lt;bean&gt;标签上指定 “lazy-init” 属性值为“true”即可延迟初始化Bean。 --&gt;
	&lt;bean id="lazyinitDi" class="com.diinit.DiLazyInit"
		lazy-init="true"&gt;
	&lt;/bean&gt;</pre>
<p> </p>
<p> junit 进行测试：</p>
<p> </p>
<pre name="code" class="java">@Test
	public void testLazyInit(){
		ApplicationContext context = new ClassPathXmlApplicationContext("initdepends.xml");
		HelloApi lazyInit = context.getBean("lazyinitDi",HelloApi.class);
		lazyInit.sayHello();
		System.out.println("");
	}</pre>
 
<p> </p>
<p>注意这个时候的输出结果：</p>
<p> </p>
<p> </p>
<p><span style="color: #ff0000;"><strong>初始化 DiLazyInit</strong></span></p>
<p><span style="color: #ff0000;"><strong>say DiLazyInit</strong></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>二、 可以指定初始化和销毁的顺序</p>
<p> </p>
<p> </p>
<pre name="code" class="java">/* 使用depends-on 是指 指定Bean初始化及销毁时的顺序，使用depends-on属性指定的Bean要先初始化完毕
*     后才初始化当前Bean，由于只有“singleton”Bean能被Spring管理销毁，所以当指定的Bean都是“singleton”
*     时，使用depends-on属性指定的Bean要在指定的Bean之后销毁。
*     “decorator”指定了“depends-on”属性为“lazyinitDi”，所以在“decorator”Bean初始化之前要先初
*     始化“lazyinitDi”，而在销毁“lazyinitDi”之前先要销毁“decorator”，大家注意一下销毁顺序，与文档上的不符。
*     “depends-on”属性可以指定多个Bean，若指定多个Bean可以用“;”、“,”、空格分割。
*     
*  那“depends-on”有什么好处呢？
*     主要是给出明确的初始化及销毁顺序，比如要初始化“decorator”时要确保“lazyinitDi”Bean的资源准备好了，
*     否则使用“decorator”时会看不到准备的资源；而在销毁时要先在“decorator”Bean的把对“helloApi”资源的引用释
*     放掉才能销毁“lazyinitDi”，否则可能销毁 “lazyinitDi”时而“decorator”还保持着资源访问，造成资源不能释放或释放错误。
*/
public class ApiDecorator implements HelloApi{

	private HelloApi helloApi;
	
	public ApiDecorator(){
		System.out.println("初始化 ApiDecorator");
	}
	
	public void sayHello() {
		System.out.println("say ApiDecorator");
		helloApi.sayHello();
		
	}

	public HelloApi getHelloApi() {
		return helloApi;
	}

	public void setHelloApi(HelloApi helloApi) {
		this.helloApi = helloApi;
	}
}</pre>
 
<p> </p>
<p>配置xml指定初始化和销毁顺序：</p>
<p> </p>
<pre name="code" class="xml">&lt;!-- 初始化及销毁时的顺序    
	     “decorator”指定了“depends-on”属性为“lazyinitDi”，所以在“decorator”Bean初始化之前
	     要先初始化“lazyinitDi”，而在销毁“lazyinitDi”之前先要销毁“decorator”，大家注意一下销毁顺序 --&gt;
	&lt;bean id="decorator" class="com.diinit.ApiDecorator"
		depends-on="lazyinitDi"&gt;
		&lt;property name="helloApi"&gt;
			&lt;ref bean="lazyinitDi" /&gt;
		&lt;/property&gt;
	&lt;/bean&gt;</pre>
 
<p> </p>
<p> </p>
<p> junit 进行测试：</p>
<p> </p>
<pre name="code" class="java">@Test
	public void testLazyInit(){
		ApplicationContext context = new ClassPathXmlApplicationContext("initdepends.xml");
		HelloApi lazyInit = context.getBean("lazyinitDi",HelloApi.class);
		lazyInit.sayHello();
		System.out.println("");
	}
	
	@Test
	public void testDependsOn(){
		ApplicationContext context= new ClassPathXmlApplicationContext("initdepends.xml");
		HelloApi depends = context.getBean("decorator",HelloApi.class);
		depends.sayHello();
	}</pre>
 
<p> </p>
<p>注意这个时候的输出结果：</p>
<p> </p>
<p> </p>
<p><span style="color: #ff0000;"><strong>初始化 DiLazyInit</strong></span></p>
<p><span style="color: #0000ff;"><strong>初始化 ApiDecorator            //也是上面同样的测试函数 testLazyInit()，同样的配置  这句是多打印出来的</strong></span></p>
<p><span style="color: #ff0000;"><strong>say DiLazyInit</strong></span></p>
<p> </p>
<p>初始化 DiLazyInit</p>
<p>初始化 ApiDecorator</p>
<p>say ApiDecorator</p>
<p>say DiLazyInit</p>
<div><br></div>
<p> </p>
<p> </p>
<p> </p>
<p>这突然多出来的打印结果，说明进行了ApiDecorator的对象的创建，</p>
<p>但是在第一个配置中也没涉及到 ApiDecorator 类的加载，注入  。</p>
<p> </p>
<p>什么原因造成的呢？<span style="color: #0000ff;"><strong>是一种隐藏的注入</strong></span>？ 继续探索中....</p>
<p> </p>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1701110#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390644.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-10-18 16:45 <a href="http://www.blogjava.net/sblig/archive/2012/10/18/390644.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>跟我学Spring3 学习笔记六 注入</title><link>http://www.blogjava.net/sblig/archive/2012/10/18/390645.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Thu, 18 Oct 2012 06:32:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/10/18/390645.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390645.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/10/18/390645.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390645.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390645.html</trackback:ping><description><![CDATA[
              <p>
</p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><a href="/blog/1585027" style="color: #108ac6;"><span style="font-size: x-small;"><br>跟我学Spring3 学习笔记一</span></a></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="font-size: x-small;"><a href="/blog/1585027" style="color: #108ac6;"></a><a href="/blog/1586139" style="color: #108ac6;">跟我学Spring3 学习笔记二</a></span></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><a href="/blog/1586553" style="color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记三</span></a></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="color: #108ac6;"><a href="/blog/1586584" style="color: #108ac6;"><span style="font-size: x-small;">跟我学Spring3 学习笔记四</span></a></span></p>
<p style="line-height: 25px; text-align: left; padding: 0px;"><span style="font-size: x-small;"><span style="color: #108ac6;"><a href="/blog/1586584" style="color: #108ac6;"></a></span><a href="/blog/1591360" style="line-height: 1.5em; color: #108ac6;">跟我学Spring3 学习笔记五 注入</a></span></p>
<p style="font-size: 14px; line-height: 25px; text-align: left; padding: 0px;"> </p>
<p style="font-size: 14px; line-height: 25px; text-align: left; padding: 0px;"> </p>
<h3 style="font-size: 1.2em; line-height: 1.5em; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding: 0px;">引用其它Bean</h3>
<p style="padding: 0px;"> </p>
<p style="line-height: 1.5em; padding: 0px;"><strong style="font-weight: bold; line-height: 1.5em; padding: 0px; margin: 0px;">一、构造器注入方式：</strong></p>
<p style="line-height: 1.5em; padding: 0px;">（1）通过” &lt;constructor-arg&gt;”标签的ref属性来引用其他Bean</p>
<p style="line-height: 1.5em; padding: 0px;"> </p>
<p style="line-height: 1.5em; padding: 0px;">（2）通过” &lt;constructor-arg&gt;”标签的子&lt;ref&gt;标签来引用其他Bean，使用bean属性来指定引用的Bean</p>
<p style="line-height: 1.5em; padding: 0px;"><strong style="font-weight: bold; line-height: 1.5em; padding: 0px; margin: 0px;">二、setter</strong><strong style="font-weight: bold; line-height: 1.5em; padding: 0px; margin: 0px;">注入方式：</strong></p>
<p style="line-height: 1.5em; padding: 0px;">（1）通过” &lt;property&gt;”标签的ref属性来引用其他Bean</p>
<p style="line-height: 1.5em; padding: 0px;">（2）通过” &lt;property&gt;”标签的子&lt;ref&gt;标签来引用其他Bean，使用bean属性来指定引用的Bean</p>
<p style="line-height: 1.5em; padding: 0px;"> </p>
<p style="line-height: 1.5em; padding: 0px;"> </p>
<pre name="code" class="java">public class HelloDiBean implements HelloApi{

	private HelloApi helloApi;
	private HelloApi helloApi2;
	

	public HelloDiBean(HelloApi helloApi){
		this.helloApi = helloApi;
	}
	
	public void sayHello() {
		helloApi.sayHello();
		helloApi2.sayHello();
	}
	

	public HelloApi getHelloApi2() {
		return helloApi2;
	}

	public void setHelloApi2(HelloApi helloApi2) {
		this.helloApi2 = helloApi2;
	}
}
</pre>
<p style="line-height: 1.5em; padding: 0px;"> 配置注入引用其他的bean</p>
<p style="line-height: 1.5em; padding: 0px;"> </p>
<pre name="code" class="xml">&lt;!-- 引用其他的bean进行注入 --&gt;
	&lt;bean id="helloBean" class="com.dilist.HelloDiBean"&gt;
		&lt;constructor-arg index="0" ref="mapBean" /&gt;
		&lt;property name="helloApi2"&gt;
			&lt;ref bean="properBean" /&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	</pre>
 
<p style="line-height: 1.5em; padding: 0px;">其他引用bean 的高级用法：</p>
<p style="line-height: 1.5em; padding: 0px;"> </p>
<pre name="code" class="java">/**
 * Spring还提供了另外两种更高级的配置方式，&lt;ref local=””/&gt;和&lt;ref parent=””/&gt;：
 * （1）&lt;ref local=””/&gt;配置方式：用于引用通过&lt;bean id=”beanName”&gt;方式中通过id属性指定的Bean，
 * 		它能利用XML解析器的验证功能在读取配置文件时来验证引用的Bean是否存在。
 * 		因此如果在当前配置文件中有相互引用的Bean可以采用&lt;ref local&gt;方式从而如果配置错误能在开发调试时就发现错误。
 * （2）&lt;ref parent=””/&gt;配置方式：用于引用父容器中的Bean，不会引用当前容器中的Bean，
 *       当然父容器中的Bean和当前容器的Bean是可以重名的，获取顺序是直接到父容器找。
 */
public class HelloHigh implements HelloApi{
	
	private HelloApi helloApi;
	private HelloApi helloApi2;
	

	public HelloHigh(HelloApi helloApi){
		this.helloApi = helloApi;
	}
	
	public void sayHello() {
		helloApi.sayHello();
		System.out.println("");
		helloApi2.sayHello();
	}
	

	public HelloApi getHelloApi2() {
		return helloApi2;
	}

	public void setHelloApi2(HelloApi helloApi2) {
		this.helloApi2 = helloApi2;
	}

}</pre>
 
<p style="padding: 0px;"><span style="line-height: 18px;">helloworld.xml：</span></p>
<p style="padding: 0px;"> </p>
<pre name="code" class="xml">&lt;!-- 注入properties类型 --&gt;
	&lt;bean id="properBean" class="com.dilist.HelloDiProperties"&gt;
		&lt;property name="properties"&gt;
			&lt;props value-type="int" merge="default"&gt;&lt;!-- 虽然指定value-type,但是不起作用 --&gt;
				&lt;prop key="1"&gt;1sss&lt;/prop&gt;           &lt;!-- Properties 建和值都是String类型 --&gt;
				&lt;prop key="2"&gt;2&lt;/prop&gt;
			&lt;/props&gt;
		&lt;/property&gt;
		&lt;property name="properties2"&gt;
			&lt;value&gt; &lt;!-- 分隔符可以是 “换行”、“；”、“，” 不建议该方式，优先选择第一种方式 --&gt;
				1=11
				2=22;&lt;!-- 这样的分隔符好像没用 --&gt;
			    3=33,
				4=44
			&lt;/value&gt;
		&lt;/property&gt;
	&lt;/bean&gt;

	&lt;!-- Spring还提供了另外两种更高级的配置方式，&lt;ref local=””/&gt;和&lt;ref parent=””/&gt; --&gt;
	&lt;bean id="helloHigh" class="com.dilist.HelloHigh"&gt;
		&lt;constructor-arg index="0"&gt;&lt;ref local="properBean" /&gt;&lt;/constructor-arg&gt;
		&lt;property name="helloApi2"&gt;&lt;ref parent="properBean" /&gt;&lt;/property&gt;	
	&lt;/bean&gt;</pre>
 
<p style="padding: 0px;"> </p>
<p style="padding: 0px;"><span style="line-height: 18px;">helloworldParent.xml：</span></p>
<p style="padding: 0px;"> </p>
<pre name="code" class="xml">&lt;!-- 注入properties类型 --&gt;
	&lt;bean id="properBean" class="com.dilist.HelloDiProperties"&gt;
		&lt;property name="properties"&gt;
			&lt;props value-type="int" merge="default"&gt;&lt;!-- 虽然指定value-type,但是不起作用 --&gt;
				&lt;prop key="1"&gt;2dss&lt;/prop&gt;           &lt;!-- Properties 建和值都是String类型 --&gt;
				&lt;prop key="2"&gt;3aas&lt;/prop&gt;
			&lt;/props&gt;
		&lt;/property&gt;
		&lt;property name="properties2"&gt;
			&lt;value&gt; &lt;!-- 分隔符可以是 “换行”、“；”、“，” 不建议该方式，优先选择第一种方式 --&gt;
				1=111
				2=222;&lt;!-- 这样的分隔符好像没用 --&gt;
			    3=333,
				4=444
			&lt;/value&gt;
		&lt;/property&gt;
	&lt;/bean&gt;</pre>
 
<p style="padding: 0px;">调用处 利用加载父容器的方式，注入父容器中的Bean：</p>
<p style="padding: 0px;"> </p>
<p style="padding: 0px;"> </p>
<pre name="code" class="java">@Test
	public void testDiBeanHigh() {
		// 以classes为根目录算起
		// 读取配置文件实例化一个Ioc容器

		// 初始化父容器
		ApplicationContext parentContext = new ClassPathXmlApplicationContext(
				"helloworldParent.xml");

		// 初始化当前容器
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "helloworld.xml" }, parentContext);

		// 构造 + setter注入 引用其他的bean注入
		HelloApi helloApi = context.getBean("helloHigh", HelloApi.class);
		helloApi.sayHello();

	}</pre>

              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1700958#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390645.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-10-18 14:32 <a href="http://www.blogjava.net/sblig/archive/2012/10/18/390645.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JavaScript 学习笔记 汇总</title><link>http://www.blogjava.net/sblig/archive/2012/10/17/390646.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Wed, 17 Oct 2012 08:14:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/10/17/390646.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390646.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/10/17/390646.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390646.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390646.html</trackback:ping><description><![CDATA[
              <p>
</p>
<ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; line-height: 18px; text-align: left; padding: 0px;">
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1477338" target="_blank" style="color: #108ac6;">1.1 JavaScript 学习笔记 一 动态性</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1477359" target="_blank" style="color: #108ac6;">1.2 JavaScript 学习笔记 二 对象的访问</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1477389" target="_blank" style="color: #108ac6;">1.3 JavaScript 学习笔记 三 原型(prototype)</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1477477" target="_blank" style="color: #108ac6;">1.4 JavaScript 学习笔记 四 this指针</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1487563" target="_blank" style="color: #108ac6;">1.5 JavaScript 学习笔记 五 函数</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1487767" target="_blank" style="color: #108ac6;">1.6 JavaScript 学习笔记 五 函数作用域</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1487823" target="_blank" style="color: #108ac6;">1.7 JavaScript 学习笔记 五 数组</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1497066" target="_blank" style="color: #108ac6;">1.8 JavaScript 学习笔记 六 正则表达式《一》</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1501721" target="_blank" style="color: #108ac6;">1.9 JavaScript 学习笔记 六 正则表达式《二》</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1501725" target="_blank" style="color: #108ac6;">1.10 JavaScript 学习笔记 六 正则表达式《三》</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1501978" target="_blank" style="color: #108ac6;">1.11 JavaScript 学习笔记七 闭包</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1502468" target="_blank" style="color: #108ac6;">1.12 JavaScript 学习笔记 跑马灯</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1503172" target="_blank" style="color: #108ac6;">1.13 JavaScript 学习笔记七 闭包二</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1503741" target="_blank" style="color: #108ac6;">1.14 JavaScript 学习笔记八 继承与引用</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1507456" target="_blank" style="color: #108ac6;">1.15 JavaScript 学习笔记九 new和apply，call</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1522064" target="_blank" style="color: #108ac6;">1.16 JavaScript 学习笔记十 练习任务系统</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1537550" target="_blank" style="color: #108ac6;">1.17 JavaScript 学习笔记十一 函数高级应用</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1537864" target="_blank" style="color: #108ac6;">1.18 JavaScript 学习笔记十二 函数式编程风格</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1541340" target="_blank" style="color: #108ac6;">1.19 JavaScript 学习笔记十三 面向对象？</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1542896" target="_blank" style="color: #108ac6;">1.20 JavaScript 学习笔记十四 this特性，静态方法 和实例方法，prototype</a></li>
<li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a href="http://sblig.iteye.com/blog/1559182" target="_blank" style="color: #108ac6;">1.21 JavaScript 学习笔记十五 规范的编码</a></li>
</ul>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1700376#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390646.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-10-17 16:14 <a href="http://www.blogjava.net/sblig/archive/2012/10/17/390646.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> 动态生成class</title><link>http://www.blogjava.net/sblig/archive/2012/10/16/390647.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Tue, 16 Oct 2012 03:18:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/10/16/390647.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390647.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/10/16/390647.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390647.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390647.html</trackback:ping><description><![CDATA[
              <pre name="code" class="java">ASM 进行动态生成class</pre>
<pre name="code" class="java">import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class HelloWorld extends ClassLoader implements Opcodes{
	public static void main(String[] args) {
		ClassWriter cw = new ClassWriter(0);
		cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
		MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "&lt;init&gt;", "()V", null, null);
		mw.visitVarInsn(ALOAD, 0);
		mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "&lt;init&gt;", "()V");
		mw.visitInsn(RETURN);
		mw.visitMaxs(1, 1);
		mw.visitEnd();
		
		mw = cw.visitMethod(ACC_PUBLIC+ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
		mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
		mw.visitLdcInsn("Hello World!");
		mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
		mw.visitInsn(RETURN);
		mw.visitMaxs(2, 2);
		mw.visitEnd(); 
		
		byte[] code = cw.toByteArray();
		FileOutputStream fos;
		try {
			fos = new FileOutputStream("Example.class");
			fos.write(code);
			fos.close();
			
			HelloWorld loader = new HelloWorld();   
		     Class exampleClass = loader   
		         .defineClass("Example", code, 0, code.length);  
				exampleClass.getMethods()[0].invoke(null, new Object[] { null });
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}</pre>
<p> </p>
<p>
</p>
<pre name="code" class="java">cglib 动态生成class 并进行拦截</pre>

<p> </p>
<p>
</p>
<pre name="code" class="java">public class MyClass {
	public void print() {
		System.out.println("I'm in MyClass.print!");
	}
}


import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class Main {

	public static void main(String[] args) {

		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(MyClass.class);
		enhancer.setCallback((Callback) new MethodInterceptorImpl());
		MyClass my = (MyClass) enhancer.create();
		my.print();
	}

	private static class MethodInterceptorImpl implements MethodInterceptor {

		public Object intercept(Object obj, Method method, Object[] args,
				MethodProxy proxy) throws Throwable {
			// log something
			System.out.println(method + " intercepted!");

			proxy.invokeSuper(obj, args);
			return null;
		}

	}
}</pre>
 
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1699047#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390647.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-10-16 11:18 <a href="http://www.blogjava.net/sblig/archive/2012/10/16/390647.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>FtpUtil  ftp工具类 过滤文件名</title><link>http://www.blogjava.net/sblig/archive/2012/10/10/390648.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Wed, 10 Oct 2012 08:26:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/10/10/390648.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390648.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/10/10/390648.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390648.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390648.html</trackback:ping><description><![CDATA[
              <p>工具类：</p>
<p> </p>
<p> </p>
<pre name="code" class="java">import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;

public class FtpUtil {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FtpUtil ftp = new FtpUtil();
		ftp.connect("10.16.12.75", 21, "ftpusr", "ftpusr");
		try {
			// 上传目录下文件 并可以递归到子目录
			// ftp.upPathFile(new File("D:\\ALSC"), "ALSC/");
			// 下载目录下多个文件 并可以递归到子目录
			//ftp.downPathFile("/opt/ftp/outgoing/cs/", "D:/outgoing/csvc");
			
			// 切换目录
			ftp.setPath("/opt/ftp/book");
			System.out.println(ftp.getDir());
			for (String file : ftp.getFileNameList()) {
				System.out.println(file);
			}

			// 切换到父级目录
			ftp.up();
			
			// 创建多目录
			// ftp.createDir("aa/bb/cc");
			ftp.setPath("ftpTest");
			
			// 删除文件
			ftp.deleteFile("bbb.bmp");
			System.out.println(ftp.getDir());
			for (String file : ftp.getFileNameList()) {
				System.out.println(file);
			}
			// 上传 下载单个文件
			ftp.uploadFile("c:/aaa.bmp", "bbb.bmp");
			ftp.downloadFile("bbb.bmp", "c:/bbb");

			List&lt;String&gt; list = ftp.getFileList();
			for (String file : list) {
				System.out.println(file);
			}

			ftp.setPath("/opt/ftp/outgoing/cs");
			String patternStr = "^CSGET_[0-9]{4}_0085_"+"20120926"+"_[0-9]{3}";
			// 过滤，获取目录下的文件列表
			list = ftp.getFileList(new CSFilter(patternStr));
			for (String file : list) {
				System.out.println(file);
			}
			
			//下载 过滤后的文件
			ftp.downPathFile("/opt/ftp/outgoing/cs/", "D:/outgoing/csvc",new CSFilter(patternStr));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ftp.close();
	}
	
	private FtpClient ftpClient = null;

	/**
	 * 打开连接
	 * 
	 * @param hostname
	 * @param port
	 * @param username
	 * @param passwd
	 * @return
	 */
	public void connect(String hostname, int port, String username,
			String passwd) {
		String msg = "";
		try {
			ftpClient = new FtpClient(hostname, port);
			ftpClient.login(username, passwd);
			ftpClient.binary();
			msg = "连接成功！";
		} catch (FtpLoginException e) {
			msg = "登录主机失败，可能是用户名密码错误！";
			e.printStackTrace();
		} catch (IOException e) {
			msg = "登录主机失败，请检验端品是否正确！";
			e.printStackTrace();
		} catch (SecurityException e) {
			msg = "无权连接主机，主确认是否有权限连接主机！";
			e.printStackTrace();
		}
		System.out.println(msg);
	}

	/**
	 * 关闭连接
	 */
	public void close() {
		if (ftpClient == null) {
			return;
		}
		try {
			ftpClient.closeServer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 重命名
	 * 
	 * @param oldName
	 * @param newName
	 * @return
	 */
	public boolean renameFile(String oldName, String newName) {
		boolean result = false;
		try {
			this.ftpClient.rename(oldName, newName);
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 取得相对于当前连接目录的某个目录下所有文件列表
	 * 
	 * @param path
	 * @return
	 */
	public List getFileList(String path) {
		List list = new ArrayList();
		DataInputStream dis;
		try {
			dis = new DataInputStream(ftpClient.nameList(this.getDir() + path));
			String filename = "";
			while ((filename = dis.readLine()) != null) {
				list.add(filename);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 读取文件列表
	 * 
	 * @return
	 * @throws IOException
	 */
	public List&lt;String&gt; getFileList() throws IOException {
		List&lt;String&gt; fileList = new ArrayList&lt;String&gt;();
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(this.ftpClient.list());
			br = new BufferedReader(isr);
			String fileName = "";
			while (true) {
				fileName = br.readLine();
				if (fileName == null) {
					break;
				}
				fileList.add(fileName);
			}
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return fileList;
	}

	/**
	 * 读取文件列表
	 * 
	 * @return
	 * @throws IOException
	 */
	public List&lt;String&gt; getFileList(FileFilter filter) throws IOException {
		List&lt;String&gt; fileList = new ArrayList&lt;String&gt;();
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(this.ftpClient.list());
			br = new BufferedReader(isr);
			String fileName = "";
			while (true) {
				fileName = br.readLine();
				if (fileName == null) {
					break;
				}
				if ((filter == null) || filter.accept(new File(fileName)))
					fileList.add(fileName);
			}
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return fileList;
	}

	/**
	 * 获取文件名列表
	 * 
	 * @return
	 * @throws IOException
	 */
	public List&lt;String&gt; getFileNameList() throws IOException {
		List&lt;String&gt; fileNameList = new ArrayList&lt;String&gt;();
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(this.ftpClient.nameList(this.getDir()));
			br = new BufferedReader(isr);

			String fileName = "";
			while (true) {
				fileName = br.readLine();
				if (fileName == null) {
					break;
				}
				fileNameList.add(fileName);
			}
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return fileNameList;

	}

	/**
	 * 设置路径 切换目录
	 * 
	 * @param path
	 * @return
	 */
	public boolean setPath(String path) {
		if (this.ftpClient != null) {
			try {
				ftpClient.cd(path);
				return true;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 判断是否为目录
	 * 
	 * @param line
	 * @return
	 */
	public boolean isDir(String line) {
		return line.startsWith("d");
	}

	/**
	 * 检查文件夹在当前目录下是否存在
	 * 
	 * @param dir
	 * @return
	 */
	public boolean isDirExist(String dir) {
		String pwd = "";
		try {
			pwd = ftpClient.pwd();
			ftpClient.cd(dir);
			ftpClient.cd(pwd);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 获取当前路径
	 * 
	 * @return
	 * @throws IOException
	 */
	public String getDir() throws IOException {
		return this.ftpClient.pwd();
	}

	/**
	 * 向上 切换到父级目录
	 * 
	 * @throws IOException
	 */
	public void up() throws IOException {
		if ("/".equals(ftpClient.pwd()) || "//".equals(ftpClient.pwd())) {
			return;
		}
		this.ftpClient.cdUp();
	}

	/**
	 * 删除文件
	 * 
	 * @param fileName
	 * @return
	 */
	public void deleteFile(String fileName) {
		ftpClient.sendServer("dele " + fileName + "\r\n");// 这个地方一定要注意 加上 \r\n
		try {
			if (ftpClient.readServerResponse() != 250)
				System.out.println("删除异常");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 在当前目录下创建文件夹
	 * 
	 * @param dir
	 * @return
	 * @throws Exception
	 */
	public boolean createDir(String dir) {
		try {
			ftpClient.ascii();
			StringTokenizer s = new StringTokenizer(dir, "/"); // sign
			s.countTokens();
			String pathName = ftpClient.pwd();
			while (s.hasMoreElements()) {
				pathName = pathName + "/" + (String) s.nextElement();
				try {
					ftpClient.sendServer("MKD " + pathName + "\r\n");
				} catch (Exception e) {
					e = null;
					return false;
				}
				ftpClient.readServerResponse();
			}
			ftpClient.binary();
			return true;
		} catch (IOException e1) {
			e1.printStackTrace();
			return false;
		}
	}

	/**
	 * 上传文件
	 * 
	 * @param localFile
	 * @param targetFileName
	 * @return
	 */
	public boolean uploadFile(String localFile, String targetFileName) {
		boolean result = false;
		if (this.ftpClient == null) {
			return false;
		}
		TelnetOutputStream tos = null;
		RandomAccessFile sendFile = null;
		DataOutputStream dos = null;
		try {
			File file = new File(localFile);
			sendFile = new RandomAccessFile(file, "r");
			sendFile.seek(0);
			tos = this.ftpClient.put(targetFileName);
			dos = new DataOutputStream(tos);
			int ch = 0;
			while (sendFile.getFilePointer() &lt; sendFile.length()) {
				ch = sendFile.read();
				dos.write(ch);
			}
			result = true;
		} catch (Exception ex) {
			result = false;
		} finally {
			if (tos != null) {
				try {
					tos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (sendFile != null) {
				try {
					sendFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 上传文件
	 * 
	 * @param localFile
	 * @param targetFileName
	 * @return
	 */
	public boolean uploadFile(File localFile, String targetFileName) {
		boolean result = false;
		if (this.ftpClient == null) {
			return false;
		}
		TelnetOutputStream tos = null;
		RandomAccessFile sendFile = null;
		DataOutputStream dos = null;
		try {
			sendFile = new RandomAccessFile(localFile, "r");
			sendFile.seek(0);
			tos = this.ftpClient.put(targetFileName);
			dos = new DataOutputStream(tos);
			int ch = 0;
			while (sendFile.getFilePointer() &lt; sendFile.length()) {
				ch = sendFile.read();
				dos.write(ch);
			}
			result = true;
		} catch (Exception ex) {
			result = false;
		} finally {
			if (tos != null) {
				try {
					tos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (sendFile != null) {
				try {
					sendFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 上传本地目录下的所有文件到服务器上
	 * 
	 * @param srcPath
	 * @param tagPath
	 * @param level
	 *            递归的级别
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public boolean upPathFile(File srcPathFile, String tagPath) {
		buildList(tagPath.substring(0, tagPath.lastIndexOf("/")));
		boolean result = true;

		try {
			File temp[] = srcPathFile.listFiles();
			for (int i = 0; i &lt; temp.length; i++) {
				if (temp[i].isDirectory()) {
					if (tagPath.lastIndexOf('/') &gt; 0) {
						result = upPathFile(temp[i], tagPath
								+ temp[i].getName() + "/");
					} else {
						result = upPathFile(temp[i], tagPath + "/"
								+ temp[i].getName() + "/");
					}
				} else {
					if (tagPath.lastIndexOf('/') &gt; 0) {
						result = uploadFile(temp[i], tagPath
								+ temp[i].getName());
					} else {
						result = uploadFile(temp[i], tagPath + "/"
								+ temp[i].getName());
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 下载文件
	 * 
	 * @param srcFileName
	 * @param targetFileName
	 * @return
	 */
	public boolean downloadFile(String srcFileName, String targetFileName) {
		if (this.ftpClient == null) {
			return false;
		}
		TelnetInputStream tis = null;
		RandomAccessFile getFile = null;
		boolean result = true;
		try {
			File file = new File(targetFileName);
			getFile = new RandomAccessFile(file, "rw");
			getFile.seek(0);
			tis = this.ftpClient.get(srcFileName);
			DataInputStream dis = new DataInputStream(tis);
			int ch = 0;
			while (true) {
				ch = dis.read();
				if (ch &lt; 0) {
					break;
				}
				getFile.write(ch);
			}
			getFile.close();
		} catch (IOException e) {
			result = false;
		} finally {
			if (getFile != null) {
				try {
					getFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (tis != null) {
				try {
					tis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 下载文件
	 * 
	 * @param srcFileName
	 * @param targetFileName
	 * @return
	 */
	public boolean downloadFile(String srcFileName, File targetFileName) {
		if (this.ftpClient == null) {
			return false;
		}
		TelnetInputStream tis = null;
		RandomAccessFile getFile = null;
		boolean result = true;
		try {
			getFile = new RandomAccessFile(targetFileName, "rw");
			getFile.seek(0);
			tis = this.ftpClient.get(srcFileName);
			DataInputStream dis = new DataInputStream(tis);
			int ch = 0;
			while (true) {
				ch = dis.read();
				if (ch &lt; 0) {
					break;
				}
				getFile.write(ch);
			}
			getFile.close();
		} catch (IOException e) {
			result = false;
		} finally {
			if (getFile != null) {
				try {
					getFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (tis != null) {
				try {
					tis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	/**
	 * 下载远程目录下的所有文件到本地
	 * 
	 * @param srcPathFile
	 *            远程目录文件
	 * @param tagPath
	 *            本地存放目录
	 * @return
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	public boolean downPathFile(String srcPath, String tagPath)
			throws IOException {

		boolean result = true;

		File tagFile = new File(tagPath);
		tagFile.mkdirs();

		setPath(srcPath);
		String tempPath = "";
		List&lt;String&gt; list = getFileList();
		for (int i = 0; i &lt; list.size(); i++) {
			String currPath = list.get(i);
			String fileName = getFileName(currPath);

			String currPathFul = getDir() + "/" + fileName;

			if (tagPath.lastIndexOf('/') &gt; 0) {
				tempPath = tagPath
						+ currPathFul.substring(currPathFul.lastIndexOf("/"),
								currPathFul.length());
			} else {
				tempPath = tagPath
						+ "/"
						+ currPathFul.substring(currPathFul.lastIndexOf("/"),
								currPathFul.length());
			}

			if (isDir(currPath)) {
				srcPath = currPathFul + "/";
				downPathFile(srcPath, tempPath);
			} else {
				srcPath = currPathFul;
				downloadFile(srcPath, tempPath);
			}
		}

		return result;
	}

	/**
	 * 下载远程目录下的所有文件到本地，过滤规则
	 * 
	 * @param srcPathFile
	 *            远程目录文件
	 * @param tagPath
	 *            本地存放目录
	 * @param fileFilter
	 * 			  下载过滤文件
	 * @return
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	public boolean downPathFile(String srcPath, String tagPath,
			FileFilter fileFilter) throws IOException {

		boolean result = true;

		File tagFile = new File(tagPath);
		tagFile.mkdirs();

		setPath(srcPath);
		String tempPath = "";
		List&lt;String&gt; list = getFileList(fileFilter);
		for (int i = 0; i &lt; list.size(); i++) {
			String currPath = list.get(i);
			String fileName = getFileName(currPath);

			String currPathFul = getDir() + "/" + fileName;

			if (tagPath.lastIndexOf('/') &gt; 0) {
				tempPath = tagPath
						+ currPathFul.substring(currPathFul.lastIndexOf("/"),
								currPathFul.length());
			} else {
				tempPath = tagPath
						+ "/"
						+ currPathFul.substring(currPathFul.lastIndexOf("/"),
								currPathFul.length());
			}

			if (isDir(currPath)) {
				srcPath = currPathFul + "/";
				downPathFile(srcPath, tempPath, fileFilter);
			} else {
				srcPath = currPathFul;
				downloadFile(srcPath, tempPath);
			}
		}

		return result;
	}

	public String getFileName(String line) {
		int i;
		String filename = (String) parseLine(line).get(8);
		for (i = 9; i &lt; parseLine(line).size(); i++) {
			filename = filename + " " + ((String) parseLine(line).get(i));
		}
		return filename;
	}

	// 处理getFileList取得的行信息
	private ArrayList parseLine(String line) {
		ArrayList s1 = new ArrayList();
		StringTokenizer st = new StringTokenizer(line, " ");
		while (st.hasMoreTokens()) {
			s1.add(st.nextToken());
		}
		return s1;
	}

	/**
	 * 从FTP文件服务器上下载文件SourceFileName，到本地destinationFileName 所有的文件名中都要求包括完整的路径名在内
	 * 
	 * @param SourceFileName
	 *            String
	 * @param destinationFileName
	 *            String
	 * @throws Exception
	 */
	public void downFile(String SourceFileName, String destinationFileName)
			throws Exception {
		ftpClient.binary(); // 一定要使用二进制模式
		TelnetInputStream ftpIn = ftpClient.get(SourceFileName);
		byte[] buf = new byte[204800];
		int bufsize = 0;
		FileOutputStream ftpOut = new FileOutputStream(destinationFileName);
		while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
			ftpOut.write(buf, 0, bufsize);
		}
		ftpOut.close();
		ftpIn.close();
	}

	/**
	 * 从FTP文件服务器上下载文件，输出到字节数组中
	 * 
	 * @param SourceFileName
	 *            String
	 * @return byte[]
	 * @throws Exception
	 */
	public byte[] downFile(String SourceFileName) throws Exception {
		ftpClient.binary(); // 一定要使用二进制模式
		TelnetInputStream ftpIn = ftpClient.get(SourceFileName);
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		byte[] buf = new byte[204800];
		int bufsize = 0;

		while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
			byteOut.write(buf, 0, bufsize);
		}
		byte[] return_arraybyte = byteOut.toByteArray();
		byteOut.close();
		ftpIn.close();
		return return_arraybyte;
	}

	/**
	 * 上传文件到FTP服务器,destination路径以FTP服务器的"/"开始，带文件名、 上传文件只能使用二进制模式，
	 * 当文件存在时再次上传则会覆盖
	 * 
	 * @param source
	 *            String
	 * @param destination
	 *            String
	 * @throws Exception
	 */
	public void upFile(String source, String destination) throws Exception {
		buildList(destination.substring(0, destination.lastIndexOf("/")));
		ftpClient.binary(); // 此行代码必须放在buildList之后
		TelnetOutputStream ftpOut = ftpClient.put(destination);
		TelnetInputStream ftpIn = new TelnetInputStream(new FileInputStream(
				source), true);
		byte[] buf = new byte[204800];
		int bufsize = 0;
		while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
			ftpOut.write(buf, 0, bufsize);
		}
		ftpIn.close();
		ftpOut.close();
	}

	/**
	 * JSP中的流上传到FTP服务器, 上传文件只能使用二进制模式，当文件存在时再次上传则会覆盖 字节数组做为文件的输入流,
	 * 此方法适用于JSP中通过request输入流来直接上传文件在RequestUpload类中调用了此方法，
	 * destination路径以FTP服务器的"/"开始，带文件名
	 * 
	 * @param sourceData
	 *            byte[]
	 * @param destination
	 *            String
	 * @throws Exception
	 */
	public void upFile(byte[] sourceData, String destination) throws Exception {
		buildList(destination.substring(0, destination.lastIndexOf("/")));
		ftpClient.binary(); // 此行代码必须放在buildList之后
		TelnetOutputStream ftpOut = ftpClient.put(destination);
		ftpOut.write(sourceData, 0, sourceData.length);
		// ftpOut.flush();
		ftpOut.close();
	}

	/**
	 * 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
	 * 上传文件时保证目录的存在目录格式必须以"/"根目录开头
	 * 
	 * @param pathList
	 *            String
	 * @throws Exception
	 */
	public void buildList(String pathList) {
		try {
			ftpClient.ascii();

			StringTokenizer s = new StringTokenizer(pathList, "/"); // sign
			int count = s.countTokens();
			String pathName = "";
			while (s.hasMoreElements()) {
				pathName = pathName + (String) s.nextElement();
				try {
					ftpClient.sendServer("XMKD " + pathName + "\r\n");
				} catch (Exception e) {
					e = null;
				}
				int reply = ftpClient.readServerResponse();
				pathName = pathName + "/";
			}
			ftpClient.binary();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}</pre>
<p> 过滤：</p>
<p> </p>
<pre name="code" class="java">import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class CSFilter implements FileFilter {

	private String patternStr ;
	
	//"^CSGET_[0-9]{4}_0085_"+"20120926"+"_[0-9]{3}"
	private Pattern pattern ;

	public CSVCFilter(String str){
		this.patternStr = str;
		this.pattern = Pattern.compile(patternStr);
	}
	public boolean accept(File pathname) {
		String strName = pathname.getName();
		if (!isDir(strName)) {
			strName = getFileName(strName);
			System.out.println(strName);
			return pattern.matcher(strName).matches();
		}
		return true;
	}

	public boolean isDir(String strName) {
		return ((String) parseLine(strName).get(0)).indexOf("d") != -1;
	}

	public String getFileName(String line) {
		int i;
		String filename = (String) parseLine(line).get(8);
		for (i = 9; i &lt; parseLine(line).size(); i++) {
			filename = filename + " " + ((String) parseLine(line).get(i));
		}
		return filename;
	}

	// 处理getFileList取得的行信息
	private ArrayList parseLine(String line) {
		ArrayList s1 = new ArrayList();
		StringTokenizer st = new StringTokenizer(line, " ");
		while (st.hasMoreTokens()) {
			s1.add(st.nextToken());
		}
		return s1;
	}

	public String getFileSize(String line) {
		return (String) parseLine(line).get(4);
	}

	public String getFileDate(String line) {
		ArrayList a = parseLine(line);
		return (String) a.get(5) + " " + (String) a.get(6) + " "
				+ (String) a.get(7);
	}

}</pre>
<p> </p>
<p><strong><span style="color: #ff6600;">下载速度提升</span></strong></p>
<p> </p>
<p>
</p>
<pre name="code" class="java">public boolean downloadFile(String srcFileName, File targetFileName)
{
//.....

//下载速度太慢，用如下方式进行提升
                        byte[] recvbuf = new byte[1024];
			while((ch = dis.read(recvbuf)) &gt; 0){
				getFile.write(recvbuf,0,ch);
			}

//			while (true) {
//				ch = dis.read();
//				if (ch &lt; 0) {
//					break;
//				}
//				getFile.write(ch);
//			}



//...

}</pre>
 
<p> </p>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1695166#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390648.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-10-10 16:26 <a href="http://www.blogjava.net/sblig/archive/2012/10/10/390648.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 高性能网络编程  NIO</title><link>http://www.blogjava.net/sblig/archive/2012/09/28/390649.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Fri, 28 Sep 2012 08:31:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/09/28/390649.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390649.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/09/28/390649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390649.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390649.html</trackback:ping><description><![CDATA[
              <p> 服务器端：</p>
<p> </p>
<p> </p>
<pre name="code" class="java"> // 1. 分配一个 ServerSocketChannel 文件描述符
            serverChannel = ServerSocketChannel.open();

            // 2. 从 ServerSocketChannel里获取一个对于的 socket
            serverSocket = serverChannel.socket();

            // 3. 生成一个 Selector
            selector = Selector.open();

            // 4. 把 socket 绑定到端口上
            serverSocket.bind(new InetSocketAddress(iport));

            // 5. serverChannel 未非bolck
            serverChannel.configureBlocking(false);

            // 6. 通过Selector注册ServerSocketChannel: 只能注册 accept
            // 而SocketChannel可以注册CONNENCT,READ,WRITE ; register -&gt; validOps
            // 在各个子类实现不同
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);
            while (true) {
			try {
				// 获得IO准备就绪的channel数量
				int n = selector.select();

				// 没有channel准备就绪,继续执行
				if (n == 0) {
					continue;
				}

				// 用一个iterator返回Selector的selectedkeys
				Iterator it = selector.selectedKeys().iterator();

				// 处理每一个SelectionKey
				while (it.hasNext()) {
					SelectionKey key = (SelectionKey) it.next();

					// 判断是否有新的连接到达
					if (key.isAcceptable()) {
						
						// 返回SelectionKey的ServerSocketChannel
						ServerSocketChannel server = (ServerSocketChannel) key
								.channel();
						System.out.println("有连接");
						SocketChannel channel = server.accept();
						
						registerChannel(selector, channel, SelectionKey.OP_READ);
						
						doWork(channel);
					}

					// 判断是否有数据在此channel里需要读取
					if (key.isReadable()) {
						processData(key);
					}
				}

				// 删除 selectedkeys
				it.remove();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}</pre>
<p> </p>
<p> </p>
<p> </p>
<p> 客户端：</p>
<p> </p>
<p> </p>
<pre name="code" class="java">  //打开socket通道
		SocketChannel socketChannel = SocketChannel.open();
		//设置非阻塞方式
		socketChannel.configureBlocking(false);
		//打开选择器
		Selector selector = Selector.open();
		//注册连接到服务器socket动作
		socketChannel.register(selector, SelectionKey.OP_CONNECT);
		//连接
		socketChannel.connect( new InetSocketAddress("localhost",9988));
		
		Set&lt;SelectionKey&gt; selectkeySets;
		SelectionKey selectionKey;
		Iterator&lt;SelectionKey&gt; iterator;
		
		//与服务器通信通道
		SocketChannel clientChannel ;

	       while(true){
			//选择一组建，其相应的通道已为I/O操作准备就绪
			//此方法执行处于阻塞模式的选择操作
			selector.select(TIME_OUT);
			
			//返回此选择器的已选择键集。
			selectkeySets = selector.selectedKeys();
			iterator = selectkeySets.iterator();
			
			
			while(iterator.hasNext()){
				selectionKey = iterator.next();
				
				if (selectionKey.isConnectable()) {
                                  clientChannel = (SocketChannel)selectionKey.channel();
					// 判断此通道上是否正在进行连接操作。  
                                  // 完成套接字通道的连接过程。  
					if (clientChannel.isConnectionPending()) {//判断此通道上是否正在进行连接操作
						clientChannel.finishConnect();  //完成套接字通道的连接过程
                                   
                                  }
                                  clientChannel.register(selector, SelectionKey.OP_WRITE);
                            }else if (selectionKey.isReadable()) {
					clientChannel = (SocketChannel)selectionKey.channel();
					//将缓冲区清空
					receiveBuffer.clear();
					//读取服务器发送来的数据库到缓冲区
					count = clientChannel.read(receiveBuffer);//count 读取到的字节数
					if (count &gt; 0) {
						clientChannel.register(selector, SelectionKey.OP_WRITE);
					}
                            }else if (selectionKey.isWritable()) {
					sendBuffer.clear();
					clientChannel = (SocketChannel)selectionKey.channel();
					clientChannel.write(sendBuffer);
					System.out.println("客户端向服务器发送数据:"+sendText);
					clientChannel.register(selector, SelectionKey.OP_READ);
                            }
                     }
                 }

</pre>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1688123#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-09-28 16:31 <a href="http://www.blogjava.net/sblig/archive/2012/09/28/390649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 高性能网络编程  mina</title><link>http://www.blogjava.net/sblig/archive/2012/09/28/390650.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Fri, 28 Sep 2012 02:52:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/09/28/390650.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390650.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/09/28/390650.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390650.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390650.html</trackback:ping><description><![CDATA[
              <p>服务器端：<br>    </p>
<pre name="code" class="java">// 创建一个非阻塞的server端socket ，用NIO
		SocketAcceptor acceptor = new NioSocketAcceptor();

		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

		// 设定这个过滤器一行一行(\r\n)的读数据
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		//设定服务器端的消息处理器,一个SamplMinaServerHandler对象(自己实现)继承IoHandlerAdapter
		acceptor.setHandler(new IoHandlerAdapter(){
			//当一个客端端连结进入时
			@Override
			public void sessionOpened(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("incomming client : "+session.getRemoteAddress());
			}
			
			//当一个客户端关闭时
			@Override
			public void sessionClosed(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("on client disconnect : "+session.getRemoteAddress());
			}

			//当客户端发送的消息到达时
			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				// TODO Auto-generated method stub
				String s =  (String)message;
				System.out.println("收到客户端发来的消息:"+s);
				//测试将消息回给客户端
				session.write(s+count);
				count ++;
			}
			private int count =0;
		});
		//端口号
		int bindPort= 9988;
		
		//绑定打开，启动服务器
		acceptor.bind(new InetSocketAddress(bindPort));
		
		System.out.println("Mina Server is listing on:="+bindPort);</pre>
<p>  <br>   <br>   <br>  客户端：<br>   </p>
<pre name="code" class="java">// create TCP/IP connector
		NioSocketConnector connector = new NioSocketConnector();

		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();

		// 设定这个过滤器将一行一行(/r/n)的读取数据
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		// 设定服务器端的消息处理器:一个SamplMinaServerHandler对象,
		connector.setHandler(new IoHandlerAdapter(){
			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				// 我们己设定了服务器解析消息的规则是一行一行读取,这里就可转为String:
				String s = (String) message;
				// Write the received data back to remote peer
				System.out.println("服务器发来的收到消息: " + s);
				// 测试将消息回送给客户端
				session.write(s);
			}

			@Override
			public void sessionClosed(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("one Clinet Disconnect !");
			}

			@Override
			public void sessionOpened(IoSession session) throws Exception {
				// TODO Auto-generated method stub
				System.out.println("incomming client  " + session.getRemoteAddress());
				session.write("我来啦........");
			}
		});
		
		// Set connect timeout.
		connector.setConnectTimeout(30);
		
		// 连结到服务器:
		ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",
				9988));
		
		// Wait for the connection attempt to be finished.
		cf.awaitUninterruptibly();
		cf.getSession().getCloseFuture().awaitUninterruptibly();
		connector.dispose();</pre>
<p> <br>   <br>   <br>       </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1687791#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390650.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-09-28 10:52 <a href="http://www.blogjava.net/sblig/archive/2012/09/28/390650.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分表分区</title><link>http://www.blogjava.net/sblig/archive/2012/09/27/390651.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Thu, 27 Sep 2012 01:58:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/09/27/390651.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390651.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/09/27/390651.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390651.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390651.html</trackback:ping><description><![CDATA[
              <p>分表  用用户ID位数取模</p>
<p> </p>
<p>分区  用时间进行分区</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1686428#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390651.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-09-27 09:58 <a href="http://www.blogjava.net/sblig/archive/2012/09/27/390651.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>代理 下载网页，挖掘数据</title><link>http://www.blogjava.net/sblig/archive/2012/09/25/390652.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Tue, 25 Sep 2012 05:52:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/09/25/390652.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390652.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/09/25/390652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390652.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390652.html</trackback:ping><description><![CDATA[
              <pre name="code" class="java">URL url = new URL("http://blog.csdn.net/mywait_00/article/details/1698627");

//设置代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("openproxy.fsfd.com", 8080));
//打开代理
URLConnection coon = url.openConnection(proxy);
//访问的时候需要设置 user-agent
coon.setRequestProperty("User-Agent","Mozila/4.0(compatible;MSIE 5.0;Windows XP;DigExt");

BufferedReader in = new BufferedReader(new InputStreamReader(coon.getInputStream()));

String inputLine; StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    html.append(inputLine);
}

</pre>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1684991#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-09-25 13:52 <a href="http://www.blogjava.net/sblig/archive/2012/09/25/390652.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java nio 编程学习 一</title><link>http://www.blogjava.net/sblig/archive/2012/09/21/390653.html</link><dc:creator>李凡</dc:creator><author>李凡</author><pubDate>Fri, 21 Sep 2012 08:08:00 GMT</pubDate><guid>http://www.blogjava.net/sblig/archive/2012/09/21/390653.html</guid><wfw:comment>http://www.blogjava.net/sblig/comments/390653.html</wfw:comment><comments>http://www.blogjava.net/sblig/archive/2012/09/21/390653.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sblig/comments/commentRss/390653.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sblig/services/trackbacks/390653.html</trackback:ping><description><![CDATA[
              <p>Java.nio中的主要类<br>ServerSocketChannel:ServerSocket的替代类.<br>SocketChannel:Socket的替代类<br>Selector:为ServerSocketChannel监控接受就绪事件,为SocketChannel监控连接就绪,读就绪和写就绪事件<br>SelectionKey:代表ServerSocketChannel及SocketChannel向Selector注册事件句柄<br>向SocketChannel和ServerSocketChannel注册事件:<br>SelectionKey key=serverSocketChannel.register(selector,op)<br>Op的可选值<br>对于ServerSocketChannel只有一个事件:<br>(1)SelectionKye.OP_ACCEPT:接受连接就绪事件,表示至少有了一个客户连接,服务器可以接受这个连接<br>SocketChannel可能发生3种事件<br>(1)SelectionKey.OP_CONNECT:连接就为事件,表示客户与服务器的连接已经成功<br>(2)SelectionKey.OP_WRITE/OP_READ:写的就绪事件,表示已经可以向输出流写数据了SocketChannel提供了接受和发送的方法<br>可以使用:read(ByteBuffer)write(ByteBuffer)写入写出<br><br><span style="color: #0000ff;"> </span><strong><span style="color: #0000ff;">ServerSocketChannel类<br></span></strong>方法:(PS继承过SelectableChannel类的方法)<br>    (1)open()静态方法获取ServerSocketChannel对象.<br>     (2)accept同ServerSocket,不过获取的是SocketChannel,根据是否阻塞返回null还是阻塞,值得注意的是accept返回的SocketChannel是阻塞模式的使用configureBlocking更改模式<br>     (3)socket() 返回关联的ServerSocket<br><span style="color: #0000ff;"><strong>SocketChannel类<br></strong></span>此类是Socket类的替代类  <br>方法:(PS继承过SelectableChannel类的方法)<br>(1)open() open(SocketAddress)静态方法用来创建SocketChannel对象,第二个重写还会建立于远程服务器的连接.<br>(2)socket()返回关联的Socket对象<br>(3)isConnected()是否建立连接<br>(4)isConnectionPending判断是否正在进行远程连接<br>(5)connect() 建立远程连接() 根据是否阻塞而不同<br>(6)finishConnect() 视图完成远程连接  <br>(7)read()读取数据(这个应该是接数据)<br>(8)write()写数据(这个是发送数据)<br><br>声明：</p>
<pre name="code" class="java">public static int PORT = 8888;
ServerSocketChannel serverChannel;
ServerSocket serverSocket;
Selector  selector;</pre>
<p> <br>初始化：</p>
<pre name="code" class="java">// 1. 分配一个 ServerSocketChannel 文件描述符
serverChannel = ServerSocketChannel.open();

// 2. 从 ServerSocketChannel里获取一个对于的 socket
serverSocket = serverChannel.socket();

// 3. 生成一个 Selector
selector = Selector.open();

// 4. 把 socket 绑定到端口上
serverSocket.bind(new InetSocketAddress(iport));

// 5. serverChannel 未非bolck
serverChannel.configureBlocking(false);

// 6. 通过Selector注册ServerSocketChannel: 只能注册 accept
// 而SocketChannel可以注册CONNENCT,READ,WRITE ; register -&gt; validOps
// 在各个子类实现不同
serverChannel.register(selector, SelectionKey.OP_ACCEPT);</pre>
<p> <br>开启服务：</p>
<pre name="code" class="java">while (true) {
	try {
			// 获得IO准备就绪的channel数量
			int n = selector.select();
			
			// 没有channel准备就绪,继续执行
			if (n == 0) {
				continue;
			}
			
			// 用一个iterator返回Selector的selectedkeys
			Iterator it = selector.selectedKeys().iterator();
			
			// 处理每一个SelectionKey
			while (it.hasNext()) {
						SelectionKey key = (SelectionKey) it.next();
					
						// 判断是否有新的连接到达
						if (key.isAcceptable()) {
					
							// 返回SelectionKey的ServerSocketChannel
							ServerSocketChannel server = (ServerSocketChannel) key
							.channel();
							System.out.println("有连接");
							SocketChannel channel = server.accept();
							
							registerChannel(selector, channel, SelectionKey.OP_READ);
							
							doWork(channel);
						}
					
						// 判断是否有数据在此channel里需要读取
						if (key.isReadable()) {
							processData(key);
						}
			}
			
			// 删除 selectedkeys
			it.remove();
	
		} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
		}
}</pre>
<p> <br><br><br></p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://sblig.iteye.com/blog/1683074#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              <img src ="http://www.blogjava.net/sblig/aggbug/390653.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sblig/" target="_blank">李凡</a> 2012-09-21 16:08 <a href="http://www.blogjava.net/sblig/archive/2012/09/21/390653.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>