锘??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩国产欧美亚洲v片 ,亚洲熟妇成人精品一区,在线综合亚洲中文精品http://www.tkk7.com/sblig/zh-cnMon, 12 May 2025 18:12:49 GMTMon, 12 May 2025 18:12:49 GMT60璺熸垜瀛pring3 瀛︿範絎旇涓?鍒濆鍖栦笌閿姣?/title><link>http://www.tkk7.com/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.tkk7.com/sblig/archive/2012/10/18/390644.html</guid><wfw:comment>http://www.tkk7.com/sblig/comments/390644.html</wfw:comment><comments>http://www.tkk7.com/sblig/archive/2012/10/18/390644.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sblig/comments/commentRss/390644.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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;">璺熸垜瀛pring3 瀛︿範絎旇涓</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;">璺熸垜瀛pring3 瀛︿範絎旇浜?/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;">璺熸垜瀛pring3 瀛︿範絎旇涓?/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;">璺熸垜瀛pring3 瀛︿範絎旇鍥?/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;">璺熸垜瀛pring3 瀛︿範絎旇浜?娉ㄥ叆</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;">璺熸垜瀛pring3 瀛︿範絎旇鍏?娉ㄥ叆</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">/** * 寤惰繜鍒濆鍖朆ean * 寤惰繜鍒濆鍖栦篃鍙仛鎯版у垵濮嬪寲錛屾寚涓嶆彁鍓嶅垵濮嬪寲Bean錛岃屾槸鍙湁鍦ㄧ湡姝d嬌鐢ㄦ椂鎵嶅垱寤哄強鍒濆鍖朆ean銆? * 閰嶇疆鏂瑰紡寰堢畝鍗曞彧闇鍦?lt;bean>鏍囩涓婃寚瀹?鈥渓azy-init鈥?灞炴у間負鈥渢rue鈥濆嵆鍙歡榪熷垵濮嬪寲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"><!-- 寤惰繜鍒濆鍖朆ean 寤惰繜鍒濆鍖栦篃鍙仛鎯版у垵濮嬪寲錛屾寚涓嶆彁鍓嶅垵濮嬪寲Bean錛岃屾槸鍙湁鍦ㄧ湡姝d嬌鐢ㄦ椂鎵嶅垱寤哄強鍒濆鍖朆ean銆? 閰嶇疆鏂瑰紡寰堢畝鍗曞彧闇鍦?lt;bean>鏍囩涓婃寚瀹?鈥渓azy-init鈥?灞炴у間負鈥渢rue鈥濆嵆鍙歡榪熷垵濮嬪寲Bean銆?--> <bean id="lazyinitDi" class="com.diinit.DiLazyInit" lazy-init="true"> </bean></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瑕佸厛鍒濆鍖栧畬姣? * 鍚庢墠鍒濆鍖栧綋鍓岯ean錛岀敱浜庡彧鏈夆渟ingleton鈥滲ean鑳借Spring綆$悊閿姣侊紝鎵浠ュ綋鎸囧畾鐨凚ean閮芥槸鈥渟ingleton鈥? * 鏃訛紝浣跨敤depends-on灞炴ф寚瀹氱殑Bean瑕佸湪鎸囧畾鐨凚ean涔嬪悗閿姣併? * 鈥渄ecorator鈥濇寚瀹氫簡鈥渄epends-on鈥濆睘鎬т負鈥渓azyinitDi鈥濓紝鎵浠ュ湪鈥渄ecorator鈥滲ean鍒濆鍖栦箣鍓嶈鍏堝垵 * 濮嬪寲鈥渓azyinitDi鈥濓紝鑰屽湪閿姣佲渓azyinitDi鈥濅箣鍓嶅厛瑕侀攢姣佲渄ecorator鈥濓紝澶у娉ㄦ剰涓涓嬮攢姣侀『搴忥紝涓庢枃妗d笂鐨勪笉絎︺? * 鈥渄epends-on鈥濆睘鎬у彲浠ユ寚瀹氬涓狟ean錛岃嫢鎸囧畾澶氫釜Bean鍙互鐢ㄢ?鈥濄佲?鈥濄佺┖鏍煎垎鍓層? * * 閭b渄epends-on鈥濇湁浠涔堝ソ澶勫憿錛? * 涓昏鏄粰鍑烘槑紜殑鍒濆鍖栧強閿姣侀『搴忥紝姣斿瑕佸垵濮嬪寲鈥渄ecorator鈥濇椂瑕佺‘淇濃渓azyinitDi鈥滲ean鐨勮祫婧愬噯澶囧ソ浜嗭紝 * 鍚﹀垯浣跨敤鈥渄ecorator鈥濇椂浼氱湅涓嶅埌鍑嗗鐨勮祫婧愶紱鑰屽湪閿姣佹椂瑕佸厛鍦ㄢ渄ecorator鈥滲ean鐨勬妸瀵光渉elloApi鈥濊祫婧愮殑寮曠敤閲? * 鏀炬帀鎵嶈兘閿姣佲渓azyinitDi鈥濓紝鍚﹀垯鍙兘閿姣?鈥渓azyinitDi鈥濇椂鑰屸渄ecorator鈥濊繕淇濇寔鐫璧勬簮璁塊棶錛岄犳垚璧勬簮涓嶈兘閲婃斁鎴栭噴鏀鵑敊璇? */ 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"><!-- 鍒濆鍖栧強閿姣佹椂鐨勯『搴? 鈥渄ecorator鈥濇寚瀹氫簡鈥渄epends-on鈥濆睘鎬т負鈥渓azyinitDi鈥濓紝鎵浠ュ湪鈥渄ecorator鈥滲ean鍒濆鍖栦箣鍓? 瑕佸厛鍒濆鍖栤渓azyinitDi鈥濓紝鑰屽湪閿姣佲渓azyinitDi鈥濅箣鍓嶅厛瑕侀攢姣佲渄ecorator鈥濓紝澶у娉ㄦ剰涓涓嬮攢姣侀『搴?--> <bean id="decorator" class="com.diinit.ApiDecorator" depends-on="lazyinitDi"> <property name="helloApi"> <ref bean="lazyinitDi" /> </property> </bean></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>榪欑獊鐒跺鍑烘潵鐨勬墦鍗扮粨鏋滐紝璇存槑榪涜浜咥piDecorator鐨勫璞$殑鍒涘緩錛?/p> <p>浣嗘槸鍦ㄧ涓涓厤緗腑涔熸病娑夊強鍒奧燗piDecorator 綾葷殑鍔犺澆錛屾敞鍏?聽銆?/p> <p>聽</p> <p>浠涔堝師鍥犻犳垚鐨勫憿錛?span style="color: #0000ff;"><strong>鏄竴縐嶉殣钘忕殑娉ㄥ叆</strong></span>錛?緇х畫鎺㈢儲涓?...</p> <p>聽</p> <p>聽</p> <br/><br/> <span style="color:red;"> <a style="color:red;">宸叉湁 <strong>1</strong> 浜哄彂琛ㄧ暀璦錛岀寷鍑?>><strong>榪欓噷</strong><<-鍙備笌璁ㄨ</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.tkk7.com/sblig/aggbug/390644.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sblig/" target="_blank">鏉庡嚒</a> 2012-10-18 16:45 <a href="http://www.tkk7.com/sblig/archive/2012/10/18/390644.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>璺熸垜瀛pring3 瀛︿範絎旇鍏?娉ㄥ叆http://www.tkk7.com/sblig/archive/2012/10/18/390645.html鏉庡嚒鏉庡嚒Thu, 18 Oct 2012 06:32:00 GMThttp://www.tkk7.com/sblig/archive/2012/10/18/390645.htmlhttp://www.tkk7.com/sblig/comments/390645.htmlhttp://www.tkk7.com/sblig/archive/2012/10/18/390645.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390645.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390645.html


璺熸垜瀛pring3 瀛︿範絎旇涓

璺熸垜瀛pring3 瀛︿範絎旇浜?/a>

璺熸垜瀛pring3 瀛︿範絎旇涓?/span>

璺熸垜瀛pring3 瀛︿範絎旇鍥?/span>

璺熸垜瀛pring3 瀛︿範絎旇浜?娉ㄥ叆

寮曠敤鍏跺畠Bean

涓銆佹瀯閫犲櫒娉ㄥ叆鏂瑰紡錛?/strong>

錛?錛夐氳繃鈥?<constructor-arg>鈥濇爣絳劇殑ref灞炴ф潵寮曠敤鍏朵粬Bean

錛?錛夐氳繃鈥?<constructor-arg>鈥濇爣絳劇殑瀛?lt;ref>鏍囩鏉ュ紩鐢ㄥ叾浠朆ean錛屼嬌鐢╞ean灞炴ф潵鎸囧畾寮曠敤鐨凚ean

浜屻乻etter娉ㄥ叆鏂瑰紡錛?/strong>

錛?錛夐氳繃鈥?<property>鈥濇爣絳劇殑ref灞炴ф潵寮曠敤鍏朵粬Bean

錛?錛夐氳繃鈥?<property>鈥濇爣絳劇殑瀛?lt;ref>鏍囩鏉ュ紩鐢ㄥ叾浠朆ean錛屼嬌鐢╞ean灞炴ф潵鎸囧畾寮曠敤鐨凚ean

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;
	}
}

聽閰嶇疆娉ㄥ叆寮曠敤鍏朵粬鐨刡ean

<!-- 寮曠敤鍏朵粬鐨刡ean榪涜娉ㄥ叆 -->
	<bean id="helloBean" class="com.dilist.HelloDiBean">
		<constructor-arg index="0" ref="mapBean" />
		<property name="helloApi2">
			<ref bean="properBean" />
		</property>
	</bean>
	

鍏朵粬寮曠敤bean 鐨勯珮綰х敤娉曪細

/**
 * Spring榪樻彁渚涗簡鍙﹀涓ょ鏇撮珮綰х殑閰嶇疆鏂瑰紡錛?lt;ref local=鈥濃?>鍜?lt;ref parent=鈥濃?>錛?
 * 錛?錛?lt;ref local=鈥濃?>閰嶇疆鏂瑰紡錛氱敤浜庡紩鐢ㄩ氳繃<bean id=鈥漛eanName鈥?gt;鏂瑰紡涓氳繃id灞炴ф寚瀹氱殑Bean錛?
 * 		瀹冭兘鍒╃敤XML瑙f瀽鍣ㄧ殑楠岃瘉鍔熻兘鍦ㄨ鍙栭厤緗枃浠舵椂鏉ラ獙璇佸紩鐢ㄧ殑Bean鏄惁瀛樺湪銆?
 * 		鍥犳濡傛灉鍦ㄥ綋鍓嶉厤緗枃浠朵腑鏈夌浉浜掑紩鐢ㄧ殑Bean鍙互閲囩敤<ref local>鏂瑰紡浠庤屽鏋滈厤緗敊璇兘鍦ㄥ紑鍙戣皟璇曟椂灝卞彂鐜伴敊璇?
 * 錛?錛?lt;ref parent=鈥濃?>閰嶇疆鏂瑰紡錛氱敤浜庡紩鐢ㄧ埗瀹瑰櫒涓殑Bean錛屼笉浼氬紩鐢ㄥ綋鍓嶅鍣ㄤ腑鐨凚ean錛?
 *       褰撶劧鐖跺鍣ㄤ腑鐨凚ean鍜屽綋鍓嶅鍣ㄧ殑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;
	}

}

helloworld.xml錛?/span>

<!-- 娉ㄥ叆properties綾誨瀷 -->
	<bean id="properBean" class="com.dilist.HelloDiProperties">
		<property name="properties">
			<props value-type="int" merge="default"><!-- 铏界劧鎸囧畾value-type,浣嗘槸涓嶈搗浣滅敤 -->
				<prop key="1">1sss</prop>           <!-- Properties 寤哄拰鍊奸兘鏄疭tring綾誨瀷 -->
				<prop key="2">2</prop>
			</props>
		</property>
		<property name="properties2">
			<value> <!-- 鍒嗛殧絎﹀彲浠ユ槸 鈥滄崲琛屸濄佲滐紱鈥濄佲滐紝鈥?涓嶅緩璁鏂瑰紡錛屼紭鍏堥夋嫨絎竴縐嶆柟寮?-->
				1=11
				2=22;<!-- 榪欐牱鐨勫垎闅旂濂藉儚娌$敤 -->
			    3=33,
				4=44
			</value>
		</property>
	</bean>

	<!-- Spring榪樻彁渚涗簡鍙﹀涓ょ鏇撮珮綰х殑閰嶇疆鏂瑰紡錛?lt;ref local=鈥濃?>鍜?lt;ref parent=鈥濃?> -->
	<bean id="helloHigh" class="com.dilist.HelloHigh">
		<constructor-arg index="0"><ref local="properBean" /></constructor-arg>
		<property name="helloApi2"><ref parent="properBean" /></property>	
	</bean>

helloworldParent.xml錛?/span>

<!-- 娉ㄥ叆properties綾誨瀷 -->
	<bean id="properBean" class="com.dilist.HelloDiProperties">
		<property name="properties">
			<props value-type="int" merge="default"><!-- 铏界劧鎸囧畾value-type,浣嗘槸涓嶈搗浣滅敤 -->
				<prop key="1">2dss</prop>           <!-- Properties 寤哄拰鍊奸兘鏄疭tring綾誨瀷 -->
				<prop key="2">3aas</prop>
			</props>
		</property>
		<property name="properties2">
			<value> <!-- 鍒嗛殧絎﹀彲浠ユ槸 鈥滄崲琛屸濄佲滐紱鈥濄佲滐紝鈥?涓嶅緩璁鏂瑰紡錛屼紭鍏堥夋嫨絎竴縐嶆柟寮?-->
				1=111
				2=222;<!-- 榪欐牱鐨勫垎闅旂濂藉儚娌$敤 -->
			    3=333,
				4=444
			</value>
		</property>
	</bean>

璋冪敤澶?鍒╃敤鍔犺澆鐖跺鍣ㄧ殑鏂瑰紡錛屾敞鍏ョ埗瀹瑰櫒涓殑Bean錛?/p>

@Test
	public void testDiBeanHigh() {
		// 浠lasses涓烘牴鐩綍綆楄搗
		// 璇誨彇閰嶇疆鏂囦歡瀹炰緥鍖栦竴涓狪oc瀹瑰櫒

		// 鍒濆鍖栫埗瀹瑰櫒
		ApplicationContext parentContext = new ClassPathXmlApplicationContext(
				"helloworldParent.xml");

		// 鍒濆鍖栧綋鍓嶅鍣?
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "helloworld.xml" }, parentContext);

		// 鏋勯?+ setter娉ㄥ叆 寮曠敤鍏朵粬鐨刡ean娉ㄥ叆
		HelloApi helloApi = context.getBean("helloHigh", HelloApi.class);
		helloApi.sayHello();

	}


宸叉湁 0 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘





鏉庡嚒 2012-10-18 14:32 鍙戣〃璇勮
]]>
JavaScript 瀛︿範絎旇 姹囨?/title><link>http://www.tkk7.com/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.tkk7.com/sblig/archive/2012/10/17/390646.html</guid><wfw:comment>http://www.tkk7.com/sblig/comments/390646.html</wfw:comment><comments>http://www.tkk7.com/sblig/archive/2012/10/17/390646.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sblig/comments/commentRss/390646.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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 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 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 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 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 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 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 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 target="_blank" style="color: #108ac6;">1.8 JavaScript 瀛︿範絎旇 鍏?姝e垯琛ㄨ揪寮忋婁竴銆?/a></li> <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a target="_blank" style="color: #108ac6;">1.9 JavaScript 瀛︿範絎旇 鍏?姝e垯琛ㄨ揪寮忋婁簩銆?/a></li> <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a target="_blank" style="color: #108ac6;">1.10 JavaScript 瀛︿範絎旇 鍏?姝e垯琛ㄨ揪寮忋婁笁銆?/a></li> <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a 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 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 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 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 target="_blank" style="color: #108ac6;">1.15 JavaScript 瀛︿範絎旇涔?new鍜宎pply錛宑all</a></li> <li style="margin-top: 0px; margin-right: 0px; margin-bottom: 0.25em; margin-left: 30px; padding: 0px;"><a 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 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 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 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 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 target="_blank" style="color: #108ac6;">1.21 JavaScript 瀛︿範絎旇鍗佷簲 瑙勮寖鐨勭紪鐮?/a></li> </ul> <br/><br/> <span style="color:red;"> <a style="color:red;">宸叉湁 <strong>1</strong> 浜哄彂琛ㄧ暀璦錛岀寷鍑?>><strong>榪欓噷</strong><<-鍙備笌璁ㄨ</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.tkk7.com/sblig/aggbug/390646.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sblig/" target="_blank">鏉庡嚒</a> 2012-10-17 16:14 <a href="http://www.tkk7.com/sblig/archive/2012/10/17/390646.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title> 鍔ㄦ佺敓鎴恈lasshttp://www.tkk7.com/sblig/archive/2012/10/16/390647.html鏉庡嚒鏉庡嚒Tue, 16 Oct 2012 03:18:00 GMThttp://www.tkk7.com/sblig/archive/2012/10/16/390647.htmlhttp://www.tkk7.com/sblig/comments/390647.htmlhttp://www.tkk7.com/sblig/archive/2012/10/16/390647.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390647.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390647.htmlASM 榪涜鍔ㄦ佺敓鎴恈lass
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, "<init>", "()V", null, null);
		mw.visitVarInsn(ALOAD, 0);
		mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()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();
		}
		
		
	}
}

cglib 鍔ㄦ佺敓鎴恈lass 騫惰繘琛屾嫤鎴?/pre>

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;
		}

	}
}


宸叉湁 1 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘





鏉庡嚒 2012-10-16 11:18 鍙戣〃璇勮
]]>FtpUtil ftp宸ュ叿綾?榪囨護鏂囦歡鍚?/title><link>http://www.tkk7.com/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.tkk7.com/sblig/archive/2012/10/10/390648.html</guid><wfw:comment>http://www.tkk7.com/sblig/comments/390648.html</wfw:comment><comments>http://www.tkk7.com/sblig/archive/2012/10/10/390648.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sblig/comments/commentRss/390648.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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<String> 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<String> getFileList() throws IOException { List<String> fileList = new ArrayList<String>(); 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<String> getFileList(FileFilter filter) throws IOException { List<String> fileList = new ArrayList<String>(); 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<String> getFileNameList() throws IOException { List<String> fileNameList = new ArrayList<String>(); 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()) || "http://".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() < 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() < 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 < temp.length; i++) { if (temp[i].isDirectory()) { if (tagPath.lastIndexOf('/') > 0) { result = upPathFile(temp[i], tagPath + temp[i].getName() + "/"); } else { result = upPathFile(temp[i], tagPath + "/" + temp[i].getName() + "/"); } } else { if (tagPath.lastIndexOf('/') > 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 < 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 < 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<String> list = getFileList(); for (int i = 0; i < list.size(); i++) { String currPath = list.get(i); String fileName = getFileName(currPath); String currPathFul = getDir() + "/" + fileName; if (tagPath.lastIndexOf('/') > 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<String> list = getFileList(fileFilter); for (int i = 0; i < list.size(); i++) { String currPath = list.get(i); String fileName = getFileName(currPath); String currPathFul = getDir() + "/" + fileName; if (tagPath.lastIndexOf('/') > 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 < 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; } /** * 浠嶧TP鏂囦歡鏈嶅姟鍣ㄤ笂涓嬭澆鏂囦歡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(); } /** * 浠嶧TP鏂囦歡鏈嶅姟鍣ㄤ笂涓嬭澆鏂囦歡錛岃緭鍑哄埌瀛楄妭鏁扮粍涓? * * @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; } /** * 涓婁紶鏂囦歡鍒癋TP鏈嶅姟鍣?destination璺緞浠TP鏈嶅姟鍣ㄧ殑"/"寮濮嬶紝甯︽枃浠跺悕銆?涓婁紶鏂囦歡鍙兘浣跨敤浜岃繘鍒舵ā寮忥紝 * 褰撴枃浠跺瓨鍦ㄦ椂鍐嶆涓婁紶鍒欎細瑕嗙洊 * * @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(); // 姝よ浠g爜蹇呴』鏀懼湪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鏈嶅姟鍣? 涓婁紶鏂囦歡鍙兘浣跨敤浜岃繘鍒舵ā寮忥紝褰撴枃浠跺瓨鍦ㄦ椂鍐嶆涓婁紶鍒欎細瑕嗙洊 瀛楄妭鏁扮粍鍋氫負鏂囦歡鐨勮緭鍏ユ祦, * 姝ゆ柟娉曢傜敤浜嶫SP涓氳繃request杈撳叆嫻佹潵鐩存帴涓婁紶鏂囦歡鍦≧equestUpload綾諱腑璋冪敤浜嗘鏂規硶錛? * destination璺緞浠TP鏈嶅姟鍣ㄧ殑"/"寮濮嬶紝甯︽枃浠跺悕 * * @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(); // 姝よ浠g爜蹇呴』鏀懼湪buildList涔嬪悗 TelnetOutputStream ftpOut = ftpClient.put(destination); ftpOut.write(sourceData, 0, sourceData.length); // ftpOut.flush(); ftpOut.close(); } /** * 鍦‵TP鏈嶅姟鍣ㄤ笂寤虹珛鎸囧畾鐨勭洰褰?褰撶洰褰曞凡緇忓瓨鍦ㄧ殑鎯呬笅涓嶄細褰卞搷鐩綍涓嬬殑鏂囦歡,榪欐牱鐢ㄤ互鍒ゆ柇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 < 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)) > 0){ getFile.write(recvbuf,0,ch); } // while (true) { // ch = dis.read(); // if (ch < 0) { // break; // } // getFile.write(ch); // } //... }</pre> 聽 <p>聽</p> <p>聽</p> <br/><br/> <span style="color:red;"> <a style="color:red;">宸叉湁 <strong>0</strong> 浜哄彂琛ㄧ暀璦錛岀寷鍑?>><strong>榪欓噷</strong><<-鍙備笌璁ㄨ</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.tkk7.com/sblig/aggbug/390648.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sblig/" target="_blank">鏉庡嚒</a> 2012-10-10 16:26 <a href="http://www.tkk7.com/sblig/archive/2012/10/10/390648.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>java 楂樻ц兘緗戠粶緙栫▼ NIOhttp://www.tkk7.com/sblig/archive/2012/09/28/390649.html鏉庡嚒鏉庡嚒Fri, 28 Sep 2012 08:31:00 GMThttp://www.tkk7.com/sblig/archive/2012/09/28/390649.htmlhttp://www.tkk7.com/sblig/comments/390649.htmlhttp://www.tkk7.com/sblig/archive/2012/09/28/390649.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390649.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390649.html聽鏈嶅姟鍣ㄧ錛?/p>

 // 1. 鍒嗛厤涓涓?ServerSocketChannel 鏂囦歡鎻忚堪絎?
            serverChannel = ServerSocketChannel.open();

            // 2. 浠?ServerSocketChannel閲岃幏鍙栦竴涓浜庣殑 socket
            serverSocket = serverChannel.socket();

            // 3. 鐢熸垚涓涓?Selector
            selector = Selector.open();

            // 4. 鎶?socket 緇戝畾鍒扮鍙d笂
            serverSocket.bind(new InetSocketAddress(iport));

            // 5. serverChannel 鏈潪bolck
            serverChannel.configureBlocking(false);

            // 6. 閫氳繃Selector娉ㄥ唽ServerSocketChannel: 鍙兘娉ㄥ唽 accept
            // 鑰孲ocketChannel鍙互娉ㄥ唽CONNENCT,READ,WRITE ; register -> validOps
            // 鍦ㄥ悇涓瓙綾誨疄鐜頒笉鍚?
            serverChannel.register(selector, SelectionKey.OP_ACCEPT);
            while (true) {
			try {
				// 鑾峰緱IO鍑嗗灝辯華鐨刢hannel鏁伴噺
				int n = selector.select();

				// 娌℃湁channel鍑嗗灝辯華,緇х畫鎵ц
				if (n == 0) {
					continue;
				}

				// 鐢ㄤ竴涓猧terator榪斿洖Selector鐨剆electedkeys
				Iterator it = selector.selectedKeys().iterator();

				// 澶勭悊姣忎竴涓猄electionKey
				while (it.hasNext()) {
					SelectionKey key = (SelectionKey) it.next();

					// 鍒ゆ柇鏄惁鏈夋柊鐨勮繛鎺ュ埌杈?
					if (key.isAcceptable()) {
						
						// 榪斿洖SelectionKey鐨凷erverSocketChannel
						ServerSocketChannel server = (ServerSocketChannel) key
								.channel();
						System.out.println("鏈夎繛鎺?);
						SocketChannel channel = server.accept();
						
						registerChannel(selector, channel, SelectionKey.OP_READ);
						
						doWork(channel);
					}

					// 鍒ゆ柇鏄惁鏈夋暟鎹湪姝hannel閲岄渶瑕佽鍙?
					if (key.isReadable()) {
						processData(key);
					}
				}

				// 鍒犻櫎 selectedkeys
				it.remove();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

聽瀹㈡埛绔細

  //鎵撳紑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<SelectionKey> selectkeySets;
		SelectionKey selectionKey;
		Iterator<SelectionKey> 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 > 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);
                            }
                     }
                 }



宸叉湁 0 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘





鏉庡嚒 2012-09-28 16:31 鍙戣〃璇勮
]]>
java 楂樻ц兘緗戠粶緙栫▼ minahttp://www.tkk7.com/sblig/archive/2012/09/28/390650.html鏉庡嚒鏉庡嚒Fri, 28 Sep 2012 02:52:00 GMThttp://www.tkk7.com/sblig/archive/2012/09/28/390650.htmlhttp://www.tkk7.com/sblig/comments/390650.htmlhttp://www.tkk7.com/sblig/archive/2012/09/28/390650.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390650.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390650.html鏈嶅姟鍣ㄧ錛?br>聽聽聽聽

// 鍒涘緩涓涓潪闃誨鐨剆erver绔痵ocket 錛岀敤NIO
		SocketAcceptor acceptor = new NioSocketAcceptor();

		// 鍒涘緩鎺ユ敹鏁版嵁鐨勮繃婊ゅ櫒
		DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

		// 璁懼畾榪欎釜榪囨護鍣ㄤ竴琛屼竴琛?\r\n)鐨勮鏁版嵁
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		//璁懼畾鏈嶅姟鍣ㄧ鐨勬秷鎭鐞嗗櫒,涓涓猄amplMinaServerHandler瀵硅薄(鑷繁瀹炵幇)緇ф壙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);

聽聽
聽聽聽
聽聽聽
聽聽瀹㈡埛绔細
聽聽聽

// create TCP/IP connector
		NioSocketConnector connector = new NioSocketConnector();

		// 鍒涘緩鎺ユ敹鏁版嵁鐨勮繃婊ゅ櫒
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();

		// 璁懼畾榪欎釜榪囨護鍣ㄥ皢涓琛屼竴琛?/r/n)鐨勮鍙栨暟鎹?
		chain.addLast("myChin", new ProtocolCodecFilter(
				new TextLineCodecFactory()));

		// 璁懼畾鏈嶅姟鍣ㄧ鐨勬秷鎭鐞嗗櫒:涓涓猄amplMinaServerHandler瀵硅薄,
		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();


聽聽聽
聽聽聽
聽聽聽聽聽聽聽



宸叉湁 0 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘





鏉庡嚒 2012-09-28 10:52 鍙戣〃璇勮
]]>
鍒嗚〃鍒嗗尯http://www.tkk7.com/sblig/archive/2012/09/27/390651.html鏉庡嚒鏉庡嚒Thu, 27 Sep 2012 01:58:00 GMThttp://www.tkk7.com/sblig/archive/2012/09/27/390651.htmlhttp://www.tkk7.com/sblig/comments/390651.htmlhttp://www.tkk7.com/sblig/archive/2012/09/27/390651.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390651.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390651.html鍒嗚〃 聽鐢ㄧ敤鎴稩D浣嶆暟鍙栨ā

鍒嗗尯 聽鐢ㄦ椂闂磋繘琛屽垎鍖?/p>

宸叉湁 0 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘






鏉庡嚒 2012-09-27 09:58 鍙戣〃璇勮
]]>
浠g悊 涓嬭澆緗戦〉錛屾寲鎺樻暟鎹?/title><link>http://www.tkk7.com/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.tkk7.com/sblig/archive/2012/09/25/390652.html</guid><wfw:comment>http://www.tkk7.com/sblig/comments/390652.html</wfw:comment><comments>http://www.tkk7.com/sblig/archive/2012/09/25/390652.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sblig/comments/commentRss/390652.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/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"); //璁劇疆浠g悊 Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("openproxy.fsfd.com", 8080)); //鎵撳紑浠g悊 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 style="color:red;">宸叉湁 <strong>0</strong> 浜哄彂琛ㄧ暀璦錛岀寷鍑?>><strong>榪欓噷</strong><<-鍙備笌璁ㄨ</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.tkk7.com/sblig/aggbug/390652.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sblig/" target="_blank">鏉庡嚒</a> 2012-09-25 13:52 <a href="http://www.tkk7.com/sblig/archive/2012/09/25/390652.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>java nio 緙栫▼瀛︿範 涓http://www.tkk7.com/sblig/archive/2012/09/21/390653.html鏉庡嚒鏉庡嚒Fri, 21 Sep 2012 08:08:00 GMThttp://www.tkk7.com/sblig/archive/2012/09/21/390653.htmlhttp://www.tkk7.com/sblig/comments/390653.htmlhttp://www.tkk7.com/sblig/archive/2012/09/21/390653.html#Feedback0http://www.tkk7.com/sblig/comments/commentRss/390653.htmlhttp://www.tkk7.com/sblig/services/trackbacks/390653.htmlJava.nio涓殑涓昏綾?br>ServerSocketChannel:ServerSocket鐨勬浛浠g被.
SocketChannel:Socket鐨勬浛浠g被
Selector:涓篠erverSocketChannel鐩戞帶鎺ュ彈灝辯華浜嬩歡,涓篠ocketChannel鐩戞帶榪炴帴灝辯華,璇誨氨緇拰鍐欏氨緇簨浠?br>SelectionKey:浠h〃ServerSocketChannel鍙奡ocketChannel鍚慡elector娉ㄥ唽浜嬩歡鍙ユ焺
鍚慡ocketChannel鍜孲erverSocketChannel娉ㄥ唽浜嬩歡:
SelectionKey key=serverSocketChannel.register(selector,op)
Op鐨勫彲閫夊?br>瀵逛簬ServerSocketChannel鍙湁涓涓簨浠?
(1)SelectionKye.OP_ACCEPT:鎺ュ彈榪炴帴灝辯華浜嬩歡,琛ㄧず鑷沖皯鏈変簡涓涓鎴瘋繛鎺?鏈嶅姟鍣ㄥ彲浠ユ帴鍙楄繖涓繛鎺?br>SocketChannel鍙兘鍙戠敓3縐嶄簨浠?br>(1)SelectionKey.OP_CONNECT:榪炴帴灝變負浜嬩歡,琛ㄧず瀹㈡埛涓庢湇鍔″櫒鐨勮繛鎺ュ凡緇忔垚鍔?br>(2)SelectionKey.OP_WRITE/OP_READ:鍐欑殑灝辯華浜嬩歡,琛ㄧず宸茬粡鍙互鍚戣緭鍑烘祦鍐欐暟鎹簡SocketChannel鎻愪緵浜嗘帴鍙楀拰鍙戦佺殑鏂規硶
鍙互浣跨敤:read(ByteBuffer)write(ByteBuffer)鍐欏叆鍐欏嚭

ServerSocketChannel綾?br>鏂規硶:(PS緇ф壙榪嘢electableChannel綾葷殑鏂規硶)
聽聽聽 (1)open()闈欐佹柟娉曡幏鍙朣erverSocketChannel瀵硅薄.
聽聽聽聽 (2)accept鍚孲erverSocket,涓嶈繃鑾峰彇鐨勬槸SocketChannel,鏍規嵁鏄惁闃誨榪斿洖null榪樻槸闃誨,鍊煎緱娉ㄦ剰鐨勬槸accept榪斿洖鐨凷ocketChannel鏄樆濉炴ā寮忕殑浣跨敤configureBlocking鏇存敼妯″紡
聽聽聽聽 (3)socket() 榪斿洖鍏寵仈鐨凷erverSocket
SocketChannel綾?br>姝ょ被鏄疭ocket綾葷殑鏇夸唬綾宦?
鏂規硶:(PS緇ф壙榪嘢electableChannel綾葷殑鏂規硶)
(1)open() open(SocketAddress)闈欐佹柟娉曠敤鏉ュ垱寤篠ocketChannel瀵硅薄,絎簩涓噸鍐欒繕浼氬緩绔嬩簬榪滅▼鏈嶅姟鍣ㄧ殑榪炴帴.
(2)socket()榪斿洖鍏寵仈鐨凷ocket瀵硅薄
(3)isConnected()鏄惁寤虹珛榪炴帴
(4)isConnectionPending鍒ゆ柇鏄惁姝e湪榪涜榪滅▼榪炴帴
(5)connect() 寤虹珛榪滅▼榪炴帴() 鏍規嵁鏄惁闃誨鑰屼笉鍚?br>(6)finishConnect() 瑙嗗浘瀹屾垚榪滅▼榪炴帴聽
(7)read()璇誨彇鏁版嵁(榪欎釜搴旇鏄帴鏁版嵁)
(8)write()鍐欐暟鎹?榪欎釜鏄彂閫佹暟鎹?

澹版槑錛?/p>
public static int PORT = 8888;
ServerSocketChannel serverChannel;
ServerSocket serverSocket;
Selector  selector;


鍒濆鍖栵細

// 1. 鍒嗛厤涓涓?ServerSocketChannel 鏂囦歡鎻忚堪絎?
serverChannel = ServerSocketChannel.open();

// 2. 浠?ServerSocketChannel閲岃幏鍙栦竴涓浜庣殑 socket
serverSocket = serverChannel.socket();

// 3. 鐢熸垚涓涓?Selector
selector = Selector.open();

// 4. 鎶?socket 緇戝畾鍒扮鍙d笂
serverSocket.bind(new InetSocketAddress(iport));

// 5. serverChannel 鏈潪bolck
serverChannel.configureBlocking(false);

// 6. 閫氳繃Selector娉ㄥ唽ServerSocketChannel: 鍙兘娉ㄥ唽 accept
// 鑰孲ocketChannel鍙互娉ㄥ唽CONNENCT,READ,WRITE ; register -> validOps
// 鍦ㄥ悇涓瓙綾誨疄鐜頒笉鍚?
serverChannel.register(selector, SelectionKey.OP_ACCEPT);


寮鍚湇鍔★細

while (true) {
	try {
			// 鑾峰緱IO鍑嗗灝辯華鐨刢hannel鏁伴噺
			int n = selector.select();
			
			// 娌℃湁channel鍑嗗灝辯華,緇х畫鎵ц
			if (n == 0) {
				continue;
			}
			
			// 鐢ㄤ竴涓猧terator榪斿洖Selector鐨剆electedkeys
			Iterator it = selector.selectedKeys().iterator();
			
			// 澶勭悊姣忎竴涓猄electionKey
			while (it.hasNext()) {
						SelectionKey key = (SelectionKey) it.next();
					
						// 鍒ゆ柇鏄惁鏈夋柊鐨勮繛鎺ュ埌杈?
						if (key.isAcceptable()) {
					
							// 榪斿洖SelectionKey鐨凷erverSocketChannel
							ServerSocketChannel server = (ServerSocketChannel) key
							.channel();
							System.out.println("鏈夎繛鎺?);
							SocketChannel channel = server.accept();
							
							registerChannel(selector, channel, SelectionKey.OP_READ);
							
							doWork(channel);
						}
					
						// 鍒ゆ柇鏄惁鏈夋暟鎹湪姝hannel閲岄渶瑕佽鍙?
						if (key.isReadable()) {
							processData(key);
						}
			}
			
			// 鍒犻櫎 selectedkeys
			it.remove();
	
		} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
		}
}






宸叉湁 0 浜哄彂琛ㄧ暀璦錛岀寷鍑?>>榪欓噷<<-鍙備笌璁ㄨ


ITeye鎺ㄨ崘





鏉庡嚒 2012-09-21 16:08 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 77777亚洲午夜久久多人| 亚洲第一成年免费网站| 真正全免费视频a毛片| 91亚洲va在线天线va天堂va国产 | 亚洲暴爽av人人爽日日碰| 国产成人无码免费看视频软件| 亚洲国产精品成人久久久| 国产精品成人免费福利| 国产成人亚洲综合网站不卡| 成年女人男人免费视频播放 | 亚洲精品tv久久久久久久久| 精品一区二区三区免费观看| 亚洲成在人线av| 免费视频爱爱太爽了| 亚洲天堂免费在线| 免费成人黄色大片| 和老外3p爽粗大免费视频| 亚洲大片在线观看| 国产福利视精品永久免费| 亚洲妇女无套内射精| 亚洲精品网站在线观看不卡无广告| 国产偷国产偷亚洲高清人| 亚洲午夜精品一级在线播放放| 18勿入网站免费永久| 久久精品国产精品亚洲| 永久免费无码网站在线观看个| 国产亚洲AV手机在线观看| 久久久国产精品无码免费专区| 亚洲精品影院久久久久久| 天天操夜夜操免费视频| 中国videos性高清免费| 亚洲成aⅴ人在线观看| 青青青国产免费一夜七次郎| 国产偷窥女洗浴在线观看亚洲| 久草免费福利视频| 亚洲欧洲日本在线| 最近2019年免费中文字幕高清| 国产亚洲精品a在线观看app | 亚洲免费在线视频| 亚洲av无码成人精品区一本二本| 亚洲午夜无码久久久久|