亚洲欧洲日产韩国在线,亚洲色欲久久久综合网东京热,亚洲AV永久纯肉无码精品动漫http://www.tkk7.com/tinguo002/category/52098.html<script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- iteye 460 60 --> <ins class="adsbygoogle" style="display:inline-block;width:468px;height:60px" data-ad-client="ca-pub-2876867208357149" data-ad-slot="0418982663"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> zh-cnFri, 07 Nov 2014 14:49:30 GMTFri, 07 Nov 2014 14:49:30 GMT60Linux 下配置 JDK 環(huán)境變量http://www.tkk7.com/tinguo002/archive/2014/08/25/417314.html一堣而安一堣而安Mon, 25 Aug 2014 13:38:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/25/417314.htmlhttp://www.tkk7.com/tinguo002/comments/417314.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/25/417314.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/417314.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/417314.html介紹在linux下配置jdk環(huán)境變量的幾種常用方法。

首先在linux下安裝jdk,如果出現(xiàn)提示權(quán)限不夠(且root下也提示權(quán)限不夠),可用#ls -l filename命令查看一下,如果顯示類似如:

-rw-rw-rw- 1 root root ….

則表示任何用戶都沒有可執(zhí)行權(quán)限(即使是root用戶)。

解決方法:

#chmod a+x filename

這樣,安裝好后,就可以接下來進(jìn)行環(huán)境變量的配置了。這里給出三種可選方法:

一、修改/etc/profile文件

當(dāng)本機(jī)僅僅作為開發(fā)使用時(shí)推薦使用這種方法,因?yàn)榇朔N配置時(shí)所有用戶的shell都有權(quán)使用這些環(huán)境變量,可能會(huì)給系統(tǒng)帶來安全性問題。

用文本編輯器打開/etc/profile,在profile文件末尾加入:

JAVA_HOME=/usr/share/jdk1.5.0_05
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH搜索

重新登錄即可。

二、修改.bashrc文件

這種方法更為安全,它可以把使用這些環(huán)境變量的權(quán)限控制到用戶級(jí)別,如果需要給某個(gè)用戶權(quán)限使用這些環(huán)境變量,只需要修改其個(gè)人用戶主目錄下的.bashrc文件就可以了。

用文本編輯器打開用戶目錄下的.bashrc文件,在.bashrc文件末尾加入:

set JAVA_HOME=/usr/share/jdk1.5.0_05
export JAVA_HOME
set PATH=$JAVA_HOME/bin:$PATH
export PATH
set CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export CLASSPATH

重新登錄。

三、直接在shell下設(shè)置變量

不推薦使用這種方法,因?yàn)閾Q個(gè)shell,該設(shè)置就無效了。這種方法僅僅是臨時(shí)使用,以后要使用的時(shí)候又要重新設(shè)置,比較麻煩。

只需在shell終端執(zhí)行下列命令:

export JAVA_HOME=/usr/share/jdk1.5.0_05
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

注意:

1.要將 /usr/share/jdk1.5.0_05jdk 改為jdk安裝目錄
2. linux下用冒號(hào)”:”來分隔路徑
3. $PATH / $CLASSPATH / $JAVA_HOME 是用來引用原來的環(huán)境變量的值在設(shè)置環(huán)境變量時(shí)特別要注意不能把原來的值給覆蓋掉了。
4. CLASSPATH中當(dāng)前目錄”.”不能丟掉。
5. export是把這三個(gè)變量導(dǎo)出為全局變量。
6. 大小寫必須嚴(yán)格區(qū)分。

一堣而安 2014-08-25 21:38 發(fā)表評(píng)論
]]>
圖片轉(zhuǎn)字符串 http://www.tkk7.com/tinguo002/archive/2014/08/05/416579.html一堣而安一堣而安Tue, 05 Aug 2014 03:30:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/05/416579.htmlhttp://www.tkk7.com/tinguo002/comments/416579.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/05/416579.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416579.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416579.htmlpackage com.apexsoft.mobile.utils;import java.io.FileInputStream;import java.io.Fil...  閱讀全文

一堣而安 2014-08-05 11:30 發(fā)表評(píng)論
]]>
java中InputStream轉(zhuǎn)化為byte[]數(shù)組 http://www.tkk7.com/tinguo002/archive/2014/08/04/416551.html一堣而安一堣而安Mon, 04 Aug 2014 13:12:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/04/416551.htmlhttp://www.tkk7.com/tinguo002/comments/416551.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/04/416551.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416551.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416551.html在commons-io包中org.apache.commons.io.IOUtils類的toByteArray(InputStream input)已經(jīng)有實(shí)現(xiàn)了,我們可以參考下思路,完成我們的方法,我們可以用類似下面的代碼實(shí)現(xiàn)inputStream轉(zhuǎn)化為byte[]數(shù)組
public static byte[] toByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
    return output.toByteArray();
}

下面是IOUtils中摘錄出與toByteArray相關(guān)的方法

org.apache.commons.io.IOUtils.toByteArray

方法如下:
public static byte[] toByteArray(InputStream input)
  throws IOException
{
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  copy(input, output);
  return output.toByteArray();
}

public static int copy(InputStream input, OutputStream output)
  throws IOException
{
  long count = copyLarge(input, output);
  if (count > 2147483647L) {
    return -1;
  }
  return (int)count;
}

public static long copyLarge(InputStream input, OutputStream output)
  throws IOException
{
  byte[] buffer = new byte[4096];
  long count = 0L;
  int n = 0;
  while (-1 != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
    count += n;
  }
  return count;
}

文章詳細(xì)出處:http://blog.csdn.net/zdwzzu2006/article/details/7745827


一堣而安 2014-08-04 21:12 發(fā)表評(píng)論
]]>
tomcat虛擬目錄配置 http://www.tkk7.com/tinguo002/archive/2014/08/04/416549.html一堣而安一堣而安Mon, 04 Aug 2014 12:57:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/04/416549.htmlhttp://www.tkk7.com/tinguo002/comments/416549.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/04/416549.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416549.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416549.htmlTomcat6.0虛擬目錄配置[轉(zhuǎn)]
設(shè)虛擬目錄 "site",通過 http://localhost:8080/site 訪問物理路徑 D:"site 文件夾里面的內(nèi)容。設(shè)置過程如下:
1.
復(fù)制 Tomcat6.0"webapps"ROOT 目錄下的 WEB-INF 文件夾到 D:"site 目錄下。
2.
打開 D:"site"WEB-INF 目錄下的 web.xml 文件, </description> 之后加入:
<!--JSPC servlet mappings start -->
<!--JSPC servlet mappings end -->
3.
打開 Tomcat6.0"conf"server.xml 文件,在 <Host> </Host> 之間加入:
<Context path="/site" docBase="d:"site"></Context>
path="/site"
就是虛擬目錄的名稱
docBase="d:"site">
為物理路徑
4.
打開 Tomcat6.0"conf"web.xml 文件,找到:
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
false設(shè)成true保存,重啟Tomcat,現(xiàn)在就可以應(yīng)用 http://localhost:8080/site 虛擬目錄了。

Tomcat6設(shè)置虛擬目錄的方法, 不修改server.xml


tomcat文件夾的conf"catalina"localhost(對(duì)于Tomcat6版本及其以上,需要自己創(chuàng)建catalinalocalhost這兩個(gè)文件夾)

增加project .xml文件(該文件名的project要和下面的“path=“/xxx"”xxx相同)

文件內(nèi)容:

<Context path="/project" reloadable="true" docBase="E:"javastudio"oob" workDir="E:"javastudio"oob"work" />


docBase
是項(xiàng)目文件夾的web-inf文件夾的上一層目錄
workDir
是指Tomcat解析Jsp轉(zhuǎn)換為Java文件,并編譯為class存放的文件夾,設(shè)置在項(xiàng)目文件夾里面,可以避免移植到其他地方首次讀取jsp文件需要重新解析 。一般格式:項(xiàng)目文件夾"work
reloadable
是指可以重新加載,一般設(shè)置為true,方便使用,不需要經(jīng)常重啟Tomcat
 

以后啟動(dòng)Tomcat,在瀏覽器輸入http://localhost:8080/project就能訪問
該項(xiàng)目的welcome文件。

轉(zhuǎn):TomCat 6.0虛擬目錄配置!20080309 星期日 13:51之前在5.0下配置虛擬目錄,我一般是采用在conf"server.xml中增加<Context .../>的方法,可是此法在6.0中失效(后經(jīng)驗(yàn)證有效,可能是之前實(shí)驗(yàn)過程中有誤)。按照tomcat 6.0啟動(dòng)之后的相關(guān)文檔的說明http://localhost:8080/docs/config/context.html,嘗試了一些方法:

-----------------------------tomcat6.0文檔中關(guān)于Context的說明-------------------
You may define as many Context elements as you wish. Each such Context MUST have a unique context path. In addition, a Context must be present with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.

For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.


Context elements may be explicitly defined:

in the $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
in the $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
in individual files (with a ".xml" extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml) extension will be used as the context path. Multi-level context paths may be defined using #, e.g. context#path.xml. The default web application may be defined by using a file called ROOT.xml.
if the previous file was not found for this application, in an individual file at /META-INF/context.xml inside the application files
inside a Host element in the main conf/server.xml
-------------------------------------------------------------------------------

逐一驗(yàn)證,方法12均無效,成功的有以下2種:(下文用%tomcat%表示Tomcat6.0的安裝目錄,希望在瀏覽器中通過http://localhost:8080/abc/default.jsp 來訪問d:"myJsp"default.jsp)

方法一:(文檔中說不建議使用)

找到%tomcat%"conf"server.xml,在</Host>之前加入:
<Context docBase="d:"myJsp" path="/abc" />
保存文件之后,重啟Tomcat即可。注意:大小寫不能錯(cuò)! 斜桿"/""""的方向別搞錯(cuò)。

方法二:該方法推薦使用,比較簡單。

%tomcat%"conf"Catalina"locahost(該目錄可能需要手工創(chuàng)建)下新建一個(gè)文件abc.xml,注意文件名中的abc就表示虛擬目錄的名稱,所以不可隨意命名,該文件的內(nèi)容為:
<Context docBase="d:"myJsp" />
重啟Tomcat即可。

-------------------------------------------------------------------------------------------
其它設(shè)置:當(dāng)url中未明確指定文件名時(shí),是否列出相關(guān)目錄下所有文件的開關(guān)設(shè)置:

打開 %tomcat%"conf"web.xml 文件,找到:
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
false改成true即可。

-------------------------------------------------------------------------------------------
其他人的同類經(jīng)驗(yàn):http://fengzhiyu-sh.javaeye.com/blog/153506 經(jīng)驗(yàn)證無需設(shè)置文中的web application下的web.xml文件。


文章詳細(xì)參考:http://www.cnblogs.com/bingoidea/archive/2009/06/06/1497787.html

一堣而安 2014-08-04 20:57 發(fā)表評(píng)論
]]>
Tomcat的窗口名稱修改http://www.tkk7.com/tinguo002/archive/2014/08/04/416548.html一堣而安一堣而安Mon, 04 Aug 2014 12:56:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/04/416548.htmlhttp://www.tkk7.com/tinguo002/comments/416548.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/04/416548.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416548.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416548.htmlcatalina.bat

找到下面的東東:


:doStart
shift
if not "%OS%" == "Windows_NT" goto noTitle
set _EXECJAVA=start "Tomcat" %_RUNJAVA%
goto gotTitle

修改紅色部分

一堣而安 2014-08-04 20:56 發(fā)表評(píng)論
]]>
java下載網(wǎng)頁內(nèi)容和網(wǎng)絡(luò)圖片 http://www.tkk7.com/tinguo002/archive/2014/08/04/416542.html一堣而安一堣而安Mon, 04 Aug 2014 10:38:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/08/04/416542.htmlhttp://www.tkk7.com/tinguo002/comments/416542.htmlhttp://www.tkk7.com/tinguo002/archive/2014/08/04/416542.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416542.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416542.htmlimport java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlResource {

    
public static void main(String[] args){
        
try {
            System.out.println(UrlResource.getUrlDetail(
"http://www.baidu.com",true));
            saveUrlFile(
"http://www.baidu.com/img/baidu_jgylogo3.gif""D:\\1.gif");
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    
//獲取網(wǎng)絡(luò)文件,轉(zhuǎn)存到fileDes中,fileDes需要帶文件后綴名
    public static void saveUrlFile(String fileUrl,String fileDes) throws Exception
    
{
        File toFile 
= new File(fileDes);
        
if (toFile.exists())
        
{
//            throw new Exception("file exist");
            return;
        }

        toFile.createNewFile();
        FileOutputStream outImgStream 
= new FileOutputStream(toFile);
        outImgStream.write(getUrlFileData(fileUrl));
        outImgStream.close();
    }

    
    
//獲取鏈接地址文件的byte數(shù)據(jù)
    public static byte[] getUrlFileData(String fileUrl) throws Exception
    
{
        URL url 
= new URL(fileUrl);
        HttpURLConnection httpConn 
= (HttpURLConnection) url.openConnection();
        httpConn.connect();
        InputStream cin 
= httpConn.getInputStream();
        ByteArrayOutputStream outStream 
= new ByteArrayOutputStream();
        
byte[] buffer = new byte[1024];
        
int len = 0;
        
while ((len = cin.read(buffer)) != -1{
            outStream.write(buffer, 
0, len);
        }

        cin.close();
        
byte[] fileData = outStream.toByteArray();
        outStream.close();
        
return fileData;
    }

    
    
//獲取鏈接地址的字符數(shù)據(jù),wichSep是否換行標(biāo)記
    public static String getUrlDetail(String urlStr,boolean withSep) throws Exception
    
{
        URL url 
= new URL(urlStr);
        HttpURLConnection httpConn 
= (HttpURLConnection)url.openConnection();
        httpConn.connect();
        InputStream cin 
= httpConn.getInputStream();
        BufferedReader reader 
= new BufferedReader(new InputStreamReader(cin,"UTF-8"));
        StringBuffer sb 
= new StringBuffer();
        String rl 
= null;
        
while((rl = reader.readLine()) != null)
        
{
            
if (withSep)
            
{
                sb.append(rl).append(System.getProperty(
"line.separator"));
            }

            
else
            
{
                sb.append(rl);
            }

        }

        
return sb.toString();
    }

    
}

// 禁止圖像緩存
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);


文章詳細(xì)參考:http://blog.csdn.net/pandakong/article/details/7430844


一堣而安 2014-08-04 18:38 發(fā)表評(píng)論
]]>
redis使用http://www.tkk7.com/tinguo002/archive/2014/07/30/416340.html一堣而安一堣而安Wed, 30 Jul 2014 04:13:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/07/30/416340.htmlhttp://www.tkk7.com/tinguo002/comments/416340.htmlhttp://www.tkk7.com/tinguo002/archive/2014/07/30/416340.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416340.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416340.html

閱讀

 


下載的windows版本是redis-2.0.2,解壓到D盤下:

D:\redis-2.0.2


進(jìn)到該目錄下,有下列文件:

 redis-server.exe:服務(wù)程序

   redis-check-dump.exe:本地?cái)?shù)據(jù)庫檢查

   redis-check-aof.exe:更新日志檢查

   redis-benchmark.exe:性能測試,用以模擬同時(shí)由N個(gè)客戶端發(fā)送M個(gè) SETs/GETs 查詢 (類似于 Apache 的ab 工具).


啟動(dòng)Redis服務(wù)(conf文件指定配置文件,若不指定則默認(rèn)):

D:\redis-2.0.2>redis-server.exe redis.conf



啟動(dòng)cmd窗口要一直開著,關(guān)閉后則Redis服務(wù)關(guān)閉

這時(shí)服務(wù)開啟著,另外開一個(gè)窗口進(jìn)行,設(shè)置客戶端:

D:\redis-2.0.2>redis-cli.exe -h 202.117.16.133 -p 6379

然后可以開始玩了:



--------------------------------------------------------------------------------------------------------------------------------

 

Redis提供了多種語言的客戶端,包括Java,C++,python。

 

Redis官網(wǎng)上推薦的Java包是Jedis,去下載Jedis,在Java項(xiàng)目中導(dǎo)入Jedis包,開始發(fā)現(xiàn)有錯(cuò)誤,是因?yàn)槿鄙賝rg.apache.commons這個(gè)包,

去網(wǎng)上找此包,下載導(dǎo)入后,Jedis就沒有錯(cuò)誤了。


可以開始了,用Jedis來操作Redis:






http://www.cnblogs.com/kkgreen/archive/2011/11/09/2243554.htm




你的
redis在真實(shí)環(huán)境中不可以誰想訪問就訪問,所以,必須要設(shè)置密碼。

設(shè)置密碼的步驟如下:

<!--[if !supportLists]-->1.       <!--[endif]-->修改redis.conf文件配置

 

root@ubuntu:/usr/local/redis-2.4.14#  vim redis.conf

#  requirepass foobared去掉注釋,foobared改為自己的密碼,我在這里改為123456

requirepass  123456

 

<!--[if !supportLists]-->2.       <!--[endif]-->啟動(dòng)服務(wù)

 

root@ubuntu:/usr/local/redis-2.4.14# ./src/redis-server redis.conf

 

<!--[if !supportLists]-->3.       <!--[endif]-->客戶端連接

 

naxsu@ubuntu:/usr/local/redis-2.4.14$ ./src/redis-cli

redis 127.0.0.1:6379> get a

(error) ERR operation not permitted

redis 127.0.0.1:6379>

提示沒有權(quán)限

naxsu@ubuntu:/usr/local/redis-2.4.14$ ./src/redis-cli -a 123456

redis 127.0.0.1:6379> get a

"b"

 

到此說明設(shè)置密碼有效了。

設(shè)置隨機(jī)啟動(dòng)

在服務(wù)器上,你每次重啟機(jī)器后再去啟動(dòng)redis的服務(wù),這是很麻煩的,所以將Redis作為 Linux 服務(wù)隨機(jī)啟動(dòng)是很有必要的。

修改/etc/rc.local文件

 

root@ubuntu:/usr/local/redis-2.4.14# vim /etc/rc.local

 

在最后加入下面一行代碼

 

./usr/local/redis-2.4.14/src/redis-server /usr/local/redis-2.4.14/redis.conf

 

重啟機(jī)器看看效果

 

根據(jù)我的測試,設(shè)置是成功的。

l

文章詳細(xì)參考:

http://www.iteye.com/topic/1124400

一堣而安 2014-07-30 12:13 發(fā)表評(píng)論
]]>
java request 獲取域名http://www.tkk7.com/tinguo002/archive/2014/07/23/416121.html一堣而安一堣而安Wed, 23 Jul 2014 03:44:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/07/23/416121.htmlhttp://www.tkk7.com/tinguo002/comments/416121.htmlhttp://www.tkk7.com/tinguo002/archive/2014/07/23/416121.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416121.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416121.html獲取域名,如:http://f0rb.iteye.com/


獲取帶部署環(huán)境上下文的域名,如: http://www.iteye.com/admin/

StringBuffer url = request.getRequestURL();  

String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString();  

文章詳細(xì)參考:http://f0rb.iteye.com/blog/1253746



一堣而安 2014-07-23 11:44 發(fā)表評(píng)論
]]>
JAVA反射機(jī)制,把JavaBean屬性轉(zhuǎn)成字符串http://www.tkk7.com/tinguo002/archive/2014/07/22/416089.html一堣而安一堣而安Tue, 22 Jul 2014 07:14:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/07/22/416089.htmlhttp://www.tkk7.com/tinguo002/comments/416089.htmlhttp://www.tkk7.com/tinguo002/archive/2014/07/22/416089.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416089.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416089.htmlpackage com.jetsum.util;import java.io.StringReader;import java.lang.reflect.Field;impo...  閱讀全文

一堣而安 2014-07-22 15:14 發(fā)表評(píng)論
]]>
Java String轉(zhuǎn)Float精度不準(zhǔn)確問題http://www.tkk7.com/tinguo002/archive/2014/07/21/416058.html一堣而安一堣而安Mon, 21 Jul 2014 11:39:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/07/21/416058.htmlhttp://www.tkk7.com/tinguo002/comments/416058.htmlhttp://www.tkk7.com/tinguo002/archive/2014/07/21/416058.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/416058.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/416058.htmlBigDecimal b1 = new BigDecimal(floatValStr );
BigDecimal b2 = new BigDecimal(100);
dqll = b1.multiply(b2).toString();    //4.5*100

有關(guān)BigDecimal  api參考:
http://blog.sina.com.cn/s/blog_6a0cd5e501011soa.html

一堣而安 2014-07-21 19:39 發(fā)表評(píng)論
]]>
java.net.SocketException: Connection reset 解決方法http://www.tkk7.com/tinguo002/archive/2014/06/27/415170.html一堣而安一堣而安Fri, 27 Jun 2014 03:43:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/06/27/415170.htmlhttp://www.tkk7.com/tinguo002/comments/415170.htmlhttp://www.tkk7.com/tinguo002/archive/2014/06/27/415170.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/415170.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/415170.htmlhttp://www.cnblogs.com/zmc/p/3295761.html

當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時(shí)間不使用的情況下,該連接會(huì)自動(dòng)回收并失效,但客戶端并不知道,在進(jìn)行數(shù)據(jù)庫操作時(shí)仍然使用的是無效的數(shù)據(jù)庫連接,這樣,就導(dǎo)致客戶端程序報(bào)“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常。


解決辦法:客戶端在使用一個(gè)無效的連接時(shí)會(huì)先對(duì)該連接進(jìn)行測試,如果發(fā)現(xiàn)該連接已經(jīng)無效,則重新從連接池獲取有效數(shù)據(jù)庫連接來使用。

在tomcat的context.xml里面設(shè)置數(shù)據(jù)源時(shí)候可參考:

 <Resource auth="Container"
  driverClassName="oracle.jdbc.OracleDriver"
  type="javax.sql.DataSource"
  url="jdbc:oracle:thin:@11.11.11.45:1521:orcl"
  name="jdbc/login"
  username="login"
  password="login"
  maxActive="15"
  maxIdle="10"
  maxWait="-1"
  minIdle="2"
  removeAbandonedTimeout="5"
  testOnBorrow="true"
  testWhileIdle="true"
  testOnReturn="true"
  removeAbandoned="true"
  logAbandoned="true"
  validationQuery="select 1 from dual"
 /> 
 
 
參考:http://www.cnblogs.com/younes/archive/2012/06/01/2529483.html




DBCP數(shù)據(jù)庫連接失效的解決方法(Io 異常:Connection reset)


網(wǎng)上很多評(píng)論說DBCP有很多BUG,但是都沒有指明是什么BUG,只有一部分人說數(shù)據(jù)庫如果因?yàn)槟撤N原因斷掉后再DBCP取道的連接都是失效的連接,而沒有重新取。有的時(shí)候會(huì)報(bào)Io 異常:Connection reset。

解決方法:

spring中datasource的配置如下:
    <bean id="dispatchdataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:myserver" />
    <property name="username" value="user1" />
    <property name="password" value="pwd" />
    <property name="maxActive" value="10000" />
    <property name="maxIdle" value="30" />
     <property name="minIdle" value="2" />
    <property name="maxWait" value="600000" />
    <property name="testOnBorrow" value="true"/>
    <property name="testWhileIdle" value="true"/>
    <property name="validationQuery" value="select 1 from dual"/>
</bean>

 

分析:

DBCP使用apache的對(duì)象池ObjectPool作為連接池的實(shí)現(xiàn),有以下主要的方法

Object borrowObject() throws Exception;從對(duì)象池取得一個(gè)有效對(duì)象

void returnObject(Object obj) throws Exception;使用完的對(duì)象放回對(duì)象池

void invalidateObject(Object obj) throws Exception;使對(duì)象失效

void addObject() throws Exception;生成一個(gè)新對(duì)象


ObjectPool的一個(gè)實(shí)現(xiàn)就是GenericObjectPool,這個(gè)類使用對(duì)象工廠PoolableObjectFactory實(shí)現(xiàn)對(duì)象的生成,失效檢查等等功能,以其實(shí)現(xiàn)數(shù)據(jù)庫連接工廠PoolableConnectionFactory做以說明,主要方法:

     Object makeObject() throws Exception; 使用ConnectionFactory生成新連接

     void destroyObject(Object obj) throws Exception;關(guān)閉連接

     boolean validateObject(Object obj); 驗(yàn)證連接是否有效,如果_validationQuery不空,則使用該屬性作為驗(yàn)證連接是否有效的sql語句,查詢數(shù)據(jù)庫

     void activateObject(Object obj) throws Exception;激活連接對(duì)象

     void passivateObject(Object obj) throws Exception; 關(guān)閉連接生成過的Statement和ResultSet,使連接處于非活動(dòng)狀態(tài)

    而GenericObjectPool有幾個(gè)主要屬性

     _timeBetweenEvictionRunsMillis:失效檢查線程運(yùn)行時(shí)間間隔,默認(rèn)-1

     _maxIdle:對(duì)象池中對(duì)象最大個(gè)數(shù)

     _minIdle:對(duì)象池中對(duì)象最小個(gè)數(shù)

     _maxActive:可以從對(duì)象池中取出的對(duì)象最大個(gè)數(shù),為0則表示沒有限制,默認(rèn)為8

     在構(gòu)造GenericObjectPool時(shí),會(huì)生成一個(gè)內(nèi)嵌類Evictor,實(shí)現(xiàn)自Runnable接口。如果 _timeBetweenEvictionRunsMillis大于0,每過_timeBetweenEvictionRunsMillis毫秒 Evictor會(huì)調(diào)用evict()方法,檢查對(duì)象的閑置時(shí)間是否大于 _minEvictableIdleTimeMillis毫秒(_minEvictableIdleTimeMillis小于等于0時(shí)則忽略,默認(rèn)為30 分鐘),是則銷毀此對(duì)象,否則就激活并校驗(yàn)對(duì)象,然后調(diào)用ensureMinIdle方法檢查確保池中對(duì)象個(gè)數(shù)不小于_minIdle。在調(diào)用 returnObject方法把對(duì)象放回對(duì)象池,首先檢查該對(duì)象是否有效,然后調(diào)用PoolableObjectFactory 的passivateObject方法使對(duì)象處于非活動(dòng)狀態(tài)。再檢查對(duì)象池中對(duì)象個(gè)數(shù)是否小于_maxIdle,是則可以把此對(duì)象放回對(duì)象池,否則銷毀此對(duì)象。

     還有幾個(gè)很重要的屬性,_testOnBorrow、_testOnReturn、_testWhileIdle,這些屬性的意義是取得、返回對(duì)象和空閑時(shí)是否進(jìn)行驗(yàn)證,檢查對(duì)象是否有效,默認(rèn)都為false即不驗(yàn)證。所以當(dāng)使用DBCP時(shí),數(shù)據(jù)庫連接因?yàn)槟撤N原因斷掉后,再從連接池中取得連接又不進(jìn)行驗(yàn)證,這時(shí)取得的連接實(shí)際已經(jīng)時(shí)無效的數(shù)據(jù)庫連接了。網(wǎng)上很多說 DBCP的bug應(yīng)該都是如此吧,只有把這些屬性設(shè)為true,再提供_validationQuery語句就可以保證數(shù)據(jù)庫連接始終有效了,oracle數(shù)據(jù)庫可以使用SELECT COUNT(*) FROM DUAL,不過DBCP要求_validationQuery語句查詢的記錄集必須不為空,可能這也可以算一個(gè)小小的BUG,其實(shí)只要_validationQuery語句執(zhí)行通過就可以了。

參考:http://hi.baidu.com/dobodo/item/7d95e3384d181cc4392ffab5



Tomcat Resource可配置的屬性






一堣而安 2014-06-27 11:43 發(fā)表評(píng)論
]]>
web 打印http://www.tkk7.com/tinguo002/archive/2014/06/26/415154.html一堣而安一堣而安Thu, 26 Jun 2014 06:40:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/06/26/415154.htmlhttp://www.tkk7.com/tinguo002/comments/415154.htmlhttp://www.tkk7.com/tinguo002/archive/2014/06/26/415154.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/415154.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/415154.htmlhttp://www.jb51.net/article/21444.htm
http://www.downdiy.com/kfyy/php/20140323/9e7b9b20201d3a49bb835efc2edc23d3.html  保持線條被打印



一堣而安 2014-06-26 14:40 發(fā)表評(píng)論
]]>
時(shí)間比較http://www.tkk7.com/tinguo002/archive/2014/05/26/414127.html一堣而安一堣而安Mon, 26 May 2014 09:47:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/05/26/414127.htmlhttp://www.tkk7.com/tinguo002/comments/414127.htmlhttp://www.tkk7.com/tinguo002/archive/2014/05/26/414127.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/414127.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/414127.html

import java.util.Date;
import java.text.ParseException;搜索
import java.text.SimpleDateFormat;

public class TimeCompare {

public static int Hour(Date time){
SimpleDateFormat st=new SimpleDateFormat("yyyyMMddHH");
return Integer.parseInt(st.format(time));
}

public static Date StringToDate(String s){
Date time=new Date();
SimpleDateFormat sd=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try{
time=sd.parse(s);
}
catch (ParseException e) {
System.out.println("輸入的日期格式有誤!");
}
return time;
}

public static void main(String[] args) {
String a="2012/01/01 18:08:20";
String b="2012/01/01 18:01:20";
String c="2012/01/02 01:01:01";
if(Hour(StringToDate(a))<=Hour(StringToDate(b))&&Hour(StringToDate(a))<Hour(StringToDate(c)))
System.out.println("成功");
else
System.out.println("失敗");
}
}


一堣而安 2014-05-26 17:47 發(fā)表評(píng)論
]]>
iframe應(yīng)用session丟失的問題 http://www.tkk7.com/tinguo002/archive/2014/05/22/413994.html一堣而安一堣而安Thu, 22 May 2014 15:07:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/05/22/413994.htmlhttp://www.tkk7.com/tinguo002/comments/413994.htmlhttp://www.tkk7.com/tinguo002/archive/2014/05/22/413994.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/413994.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/413994.htmlhttp://www.cnblogs.com/fengqingtao/archive/2011/03/16/1986174.html

iframe應(yīng)用session丟失的問題

在網(wǎng)站群的建設(shè)中,各子站需要共享主站的footer等公共信息。同時(shí)主站的后臺(tái)管理也集成了各子站的管理,采取的方式是使用iframe嵌入各站的頁面。在本機(jī)開發(fā)環(huán)境中,沒有出現(xiàn)任何的問題。但是一放到測試環(huán)境中,便遇到session丟失的問題。
        環(huán)境:應(yīng)用服務(wù)器采用tomcat6.0,各個(gè)站點(diǎn)單獨(dú)使用一個(gè)應(yīng)用服務(wù)器,部署在一臺(tái)物理服務(wù)器上。外部訪問采用同一個(gè)IP,但是不同的端口。
       起初以為,IE它的安全策略默認(rèn)是會(huì)把iframe中的頁面站點(diǎn)認(rèn)為是不可信任的,它會(huì)阻止該站點(diǎn)傳過來的cookie(如果你在iframe中的URL跳轉(zhuǎn)是用的localhost,則不會(huì)被阻擋),所以因?yàn)闆]法使用cookie了,session便失效了。解決的方法是在過濾器,或者被嵌入的頁面內(nèi)加入屬性為P3P的header信息。java為:response.addHeader("P3P","CP=CAO PSA OUR");但是依然沒有成功。網(wǎng)上的解決方案都是這么說,況且自己以前還弄過,都成功過,這次怎么弄都不好。
        今天腦子安靜下來,仔細(xì)的分析這里面的原因。如果是IE的安全限制,但是火狐、google瀏覽器沒有這樣的限制,為什么這兩個(gè)瀏覽器也出現(xiàn)這樣的情況。這肯定不僅僅和跨域引起的P3P的安全問題有關(guān)。于是在本機(jī)測試,通過iframe引入測試環(huán)境中的鏈接,設(shè)置了P3P,發(fā)現(xiàn)一切正常。這就更說明了,測試環(huán)境中的問題絕對(duì)不是P3P的問題了。而且使用了同一個(gè)IP,也應(yīng)該沒有跨域的說法。那原因到底是什么呢?
        慢慢的,我將視線注意到了端口上。這些網(wǎng)站的訪問方式都是:同一IP+不同端口,難道和端口有關(guān)系。上網(wǎng)搜,關(guān)于這方面的內(nèi)容太少了,但是總算在零星的資源中,找到了里面的原因。IP相同的兩個(gè)session對(duì)應(yīng)的cookie是一樣的,而不幸的是sessionID就保存在cookie中,這樣先訪問A,再訪問B的時(shí)候,B的sessionid會(huì)覆蓋A的sessionid。這個(gè)事情沒辦法解決,所以你不要搞兩個(gè)端口,最好是搞兩個(gè)IP。原來都是cookie惹的禍,它不會(huì)區(qū)分端口,造成這多個(gè)站點(diǎn)不斷的后來的覆蓋前面的,從而造成session的丟失。問題解決了,將相互有引用的應(yīng)用架構(gòu)在不同的虛擬主機(jī)中,或者映射不同的IP。



一堣而安 2014-05-22 23:07 發(fā)表評(píng)論
]]>
Java DES 加密和解密源碼http://www.tkk7.com/tinguo002/archive/2014/05/13/413610.html一堣而安一堣而安Tue, 13 May 2014 09:19:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/05/13/413610.htmlhttp://www.tkk7.com/tinguo002/comments/413610.htmlhttp://www.tkk7.com/tinguo002/archive/2014/05/13/413610.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/413610.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/413610.html文章參考:http://www.oschina.net/code/snippet_727646_18383

Java密碼學(xué)結(jié)構(gòu)設(shè)計(jì)遵循兩個(gè)原則:

1) 算法的獨(dú)立性和可靠性。

2) 實(shí)現(xiàn)的獨(dú)立性和相互作用性。

算法的獨(dú)立性是通過定義密碼服務(wù)類來獲得。用戶只需了解密碼算法的概念,而不用去關(guān)心如何實(shí)現(xiàn)這些概念。實(shí)現(xiàn)的獨(dú)立性和相互作用性通過密碼服務(wù)提供器來實(shí)現(xiàn)。密碼服務(wù)提供器是實(shí)現(xiàn)一個(gè)或多個(gè)密碼服務(wù)的一個(gè)或多個(gè)程序包。軟件開發(fā)商根據(jù)一定接口,將各種算法實(shí)現(xiàn)后,打包成一個(gè)提供器,用戶可以安裝不同的提供器。安裝和配置提供器,可將包含提供器的ZIPJAR文件放在CLASSPATH,再編輯Java安全屬性文件來設(shè)置定義一個(gè)提供器。


DES算法及如何利用DES算法加密和解密類文件的步驟

DES算法簡介
DESData Encryption Standard)是發(fā)明最早的最廣泛使用的分組對(duì)稱加密算法。DES算法的入口參數(shù)有三個(gè):KeyDataMode。其中Key8個(gè)字節(jié)共64位,是DES算法的工作密鑰;Data也為8個(gè)字節(jié)64位,是要被加密或被解密的數(shù)據(jù);ModeDES的工作方式,有兩種:加密或解密。

package com.afreon.util;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DesUtil {

    
private final static String DES = "DES";

    
public static void main(String[] args) throws Exception {
        String data 
= "123 456";
        String key 
= "wang!@#$%";
        System.err.println(encrypt(data, key));
        System.err.println(decrypt(encrypt(data, key), key));

    }

    
    
/**
     * Description 根據(jù)鍵值進(jìn)行加密
     * 
@param data 
     * 
@param key  加密鍵byte數(shù)組
     * 
@return
     * 
@throws Exception
     
*/

    
public static String encrypt(String data, String key) throws Exception {
        
byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs 
= new BASE64Encoder().encode(bt);
        
return strs;
    }


    
/**
     * Description 根據(jù)鍵值進(jìn)行解密
     * 
@param data
     * 
@param key  加密鍵byte數(shù)組
     * 
@return
     * 
@throws IOException
     * 
@throws Exception
     
*/

    
public static String decrypt(String data, String key) throws IOException,
            Exception 
{
        
if (data == null)
            
return null;
        BASE64Decoder decoder 
= new BASE64Decoder();
        
byte[] buf = decoder.decodeBuffer(data);
        
byte[] bt = decrypt(buf,key.getBytes());
        
return new String(bt);
    }


    
/**
     * Description 根據(jù)鍵值進(jìn)行加密
     * 
@param data
     * 
@param key  加密鍵byte數(shù)組
     * 
@return
     * 
@throws Exception
     
*/

    
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        
// 生成一個(gè)可信任的隨機(jī)數(shù)源
        SecureRandom sr = new SecureRandom();

        
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象
        DESKeySpec dks = new DESKeySpec(key);

        
// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey 
= keyFactory.generateSecret(dks);

        
// Cipher對(duì)象實(shí)際完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        
// 用密鑰初始化Cipher對(duì)象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        
return cipher.doFinal(data);
    }

    
    
    
/**
     * Description 根據(jù)鍵值進(jìn)行解密
     * 
@param data
     * 
@param key  加密鍵byte數(shù)組
     * 
@return
     * 
@throws Exception
     
*/

    
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        
// 生成一個(gè)可信任的隨機(jī)數(shù)源
        SecureRandom sr = new SecureRandom();

        
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象
        DESKeySpec dks = new DESKeySpec(key);

        
// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey 
= keyFactory.generateSecret(dks);

        
// Cipher對(duì)象實(shí)際完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        
// 用密鑰初始化Cipher對(duì)象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        
return cipher.doFinal(data);
    }

}






一堣而安 2014-05-13 17:19 發(fā)表評(píng)論
]]>
java.lang.NoClassDefFoundError: Could not initialize class 的原因http://www.tkk7.com/tinguo002/archive/2014/04/24/412883.html一堣而安一堣而安Thu, 24 Apr 2014 06:22:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/24/412883.htmlhttp://www.tkk7.com/tinguo002/comments/412883.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/24/412883.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412883.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412883.html解決辦法:通過myeclipse的 clean清除現(xiàn)有的class文件,重新編譯。或者從以前的備份中找到此class文件替換。
原因2:引入的jar包沖突導(dǎo)致。
解決辦法:baidu找一下,包沖突的事例。

一堣而安 2014-04-24 14:22 發(fā)表評(píng)論
]]>
java生成uuid(轉(zhuǎn)載)http://www.tkk7.com/tinguo002/archive/2014/04/16/412497.html一堣而安一堣而安Wed, 16 Apr 2014 01:07:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/16/412497.htmlhttp://www.tkk7.com/tinguo002/comments/412497.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/16/412497.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412497.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412497.htmlpublic class UniqId {      private static char[...  閱讀全文

一堣而安 2014-04-16 09:07 發(fā)表評(píng)論
]]>
Map、JavaBean、JSON的相互轉(zhuǎn)換 http://www.tkk7.com/tinguo002/archive/2014/04/12/412355.html一堣而安一堣而安Sat, 12 Apr 2014 09:24:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/12/412355.htmlhttp://www.tkk7.com/tinguo002/comments/412355.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/12/412355.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412355.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412355.htmlpackage com.suning.crawler.util; import java.lang.reflect.Method; import java...  閱讀全文

一堣而安 2014-04-12 17:24 發(fā)表評(píng)論
]]>
JQuery AJAX提交中文亂碼的解決方案http://www.tkk7.com/tinguo002/archive/2014/04/11/412328.html一堣而安一堣而安Fri, 11 Apr 2014 11:35:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/11/412328.htmlhttp://www.tkk7.com/tinguo002/comments/412328.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/11/412328.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412328.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412328.html要解決這個(gè)中文亂碼問題,就必須給AJAX異步提交指定顯 示的charset!
jQuery(form).ajaxSubmit({ 
    url: "ajax.aspx?a=memberlogin", 
    type: "post", 
    dataType: "json", 
    contentType: "application/x-www-form-urlencoded; charset=utf-8", 
    success: showLoginResponse
});
詳細(xì)出處參考:http://www.jb51.net/article/24097.htm


解決辦法2:
java代碼中,將response.setCharacterEncoding("UTF-8")放在action方法的最前面。
詳細(xì)出處參考:http://bbs.csdn.net/topics/360098992?page=1

一堣而安 2014-04-11 19:35 發(fā)表評(píng)論
]]>
java FTP Linux系統(tǒng)下 文件上傳下載http://www.tkk7.com/tinguo002/archive/2014/04/10/412256.html一堣而安一堣而安Thu, 10 Apr 2014 10:42:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/10/412256.htmlhttp://www.tkk7.com/tinguo002/comments/412256.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/10/412256.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412256.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412256.htmlimport java.io.Fil...  閱讀全文

一堣而安 2014-04-10 18:42 發(fā)表評(píng)論
]]>
sun.net.ftp異常 PORT 192,168,0,80,9,205: 550 Permission denied.http://www.tkk7.com/tinguo002/archive/2014/04/10/412224.html一堣而安一堣而安Thu, 10 Apr 2014 06:19:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/10/412224.htmlhttp://www.tkk7.com/tinguo002/comments/412224.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/10/412224.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412224.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412224.html

我使用sun.net.ftp來連接linux的vsftp時(shí),出現(xiàn)如下的異常(在windows下是沒有的)

 

 

java.io.FileNotFoundException: PORT 192,168,0,80,9,205: 550 Permission denied.

 

at sun.net.ftp.FtpClient.readReply(FtpClient.java:236)

at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:193)

at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:434)

at sun.net.ftp.FtpClient.put(FtpClient.java:594)

at org.lab24.util.FtpTool.processfile(FtpTool.java:193)

at org.lab24.util.FtpTool.upload(FtpTool.java:116)

at org.lab24.spider.Spider.run(Spider.java:55)

at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:743)

at java.lang.Thread.run(Thread.java:619)

exception e in ftp upload(): java.io.FileNotFoundException: PORT 192,168,0,80,9,205: 550 Permission denied.

 

后來查過網(wǎng)上的解決方案,才知道:

該類默認(rèn)使用的是Port模式傳輸數(shù)據(jù),但實(shí)際上Linux配的是Pasv模式,所以傳不過去,修改辦法:

 

ftpclient.sendServer("quote PASV");
設(shè)置一下傳輸模式就可以傳了。

一堣而安 2014-04-10 14:19 發(fā)表評(píng)論
]]>
避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常http://www.tkk7.com/tinguo002/archive/2014/04/09/412161.html一堣而安一堣而安Wed, 09 Apr 2014 09:34:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/09/412161.htmlhttp://www.tkk7.com/tinguo002/comments/412161.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/09/412161.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/412161.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/412161.html/**解決辦法**
* 1添加一個(gè)方法
* 2調(diào)用這個(gè)方法生成一個(gè)新的httpClient對(duì)象
**/



import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpUtils
{
    
private HttpClient httpClient;
    
public HttpUtils()
    
{
        httpClient 
= null;
    }


    
public void openConnection()
    
{
        httpClient 
= new DefaultHttpClient();
      
  httpClient = getSecuredHttpClient(httpClient
    });
        


    
    
/**
     * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常
     * 不用導(dǎo)入SSL證書
     * 
     * 
@author shipengzhi(shipengzhi@sogou-inc.com)
     * 
     
*/

    
private static DefaultHttpClient getSecuredHttpClient(HttpClient httpClient) {
        
final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
        
try {
            SSLContext ctx 
= SSLContext.getInstance("TLS");
            X509TrustManager tm 
= new X509TrustManager() {
                @Override
                
public X509Certificate[] getAcceptedIssuers() {
                    
return _AcceptedIssuers;
                }


                @Override
                
public void checkServerTrusted(X509Certificate[] chain,
                        String authType) 
throws CertificateException {
                }


                @Override
                
public void checkClientTrusted(X509Certificate[] chain,
                        String authType) 
throws CertificateException {
                }

            }
;
            ctx.init(
nullnew TrustManager[] { tm }new SecureRandom());
            SSLSocketFactory ssf 
= new SSLSocketFactory(ctx,
                    SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm 
= httpClient.getConnectionManager();
            SchemeRegistry sr 
= ccm.getSchemeRegistry();
            sr.register(
new Scheme("https"443, ssf));
            
return new DefaultHttpClient(ccm, httpClient.getParams());
        }
 catch (Exception e) {
            System.out.println(
"=====:=====");
            e.printStackTrace();
        }

        
return null;
    }

}



一堣而安 2014-04-09 17:34 發(fā)表評(píng)論
]]>
string類型轉(zhuǎn)為double 值變化了http://www.tkk7.com/tinguo002/archive/2014/04/04/411961.html一堣而安一堣而安Fri, 04 Apr 2014 06:39:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/04/411961.htmlhttp://www.tkk7.com/tinguo002/comments/411961.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/04/411961.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/411961.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/411961.htmlhttp://zhidao.baidu.com/link?url=NeIRa5raPCZw7sR2DR1hzNk8aewMuNdDCNJ83qRMMWWfXaPVct7rWqVNWZnfkQAroEzRvKn5XxK4rK1lWEl2-_

精度的問題!用基本類型的double類型進(jìn)行運(yùn)算可能會(huì)丟失精度。而且特別大的數(shù)又沒法處理。所以如果用BigDecimal這個(gè)類問題就解決了。這個(gè)類在java.Math包下。它可以處理任意精度的數(shù)據(jù)。對(duì)于樓主出現(xiàn)的問題,我從新寫了段代碼,供樓主參考。但是主要是還得查看API!代碼如下:

import java.math.*;

public class oopp

{

 public static void 搜索main(String[] args)

 {

  String a="1467000000";

  double aa=Double.parseDouble(a);

  BigDecimal beichushu=new BigDecimal(aa);

  BigDecimal chushu=new BigDecimal(100000000);

  BigDecimal result=beichushu.divide(chushu,new MathContext(4));//MathConText(4)表示結(jié)果精確4位!

  boolean isTrue=String.valueOf(result).equals("14.67");

  System.out.println("1467000000除以100000000="+result);

  System.out.println(result+"與14.67比較的結(jié)果是"+isTrue);

 }

}





一堣而安 2014-04-04 14:39 發(fā)表評(píng)論
]]>
Tomcat啟動(dòng)時(shí)一閃而過,看不到錯(cuò)誤信息http://www.tkk7.com/tinguo002/archive/2014/04/04/411957.html一堣而安一堣而安Fri, 04 Apr 2014 06:21:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/04/04/411957.htmlhttp://www.tkk7.com/tinguo002/comments/411957.htmlhttp://www.tkk7.com/tinguo002/archive/2014/04/04/411957.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/411957.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/411957.htmlhttp://blog.163.com/kangle0925@126/blog/static/277581982011926102830712/

有時(shí)Tomcat的啟動(dòng)窗口一閃而過,根本就看不出啟動(dòng)過程中發(fā)生了什么錯(cuò)誤。這中間的原因有好多種,最常見的解決辦法就是使用run命令,打開startup.bat文件,找到下面這行:

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

修改為:

call "%EXECUTABLE%" run %CMD_LINE_ARGS%

這樣,Tomcat啟動(dòng)時(shí)就不會(huì)彈出新窗口,就可以從容不迫地觀察這的啟動(dòng)錯(cuò)誤了。

修改文件后:需要從DOS命令行窗口進(jìn)入到%TOMCAT_HOME%\bin路徑下,然后啟動(dòng)startup.bat文件。

 

 Tomcat啟動(dòng)時(shí)一閃而過,看不多錯(cuò)誤信息 - Sky - Sky的博客



一堣而安 2014-04-04 14:21 發(fā)表評(píng)論
]]>
java日期http://www.tkk7.com/tinguo002/archive/2014/03/18/411194.html一堣而安一堣而安Tue, 18 Mar 2014 13:01:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/03/18/411194.htmlhttp://www.tkk7.com/tinguo002/comments/411194.htmlhttp://www.tkk7.com/tinguo002/archive/2014/03/18/411194.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/411194.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/411194.htmlhttp://blog.csdn.net/liwenfeng1022/article/details/6534176

//字符串日期加1天
       String sgrq = "20140101";
       SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
       try {
         Date d = formatter.parse(sgrq);
         String qxrq = formatter.format(d.getTime() + (1 * 24 * 60 * 60 * 1000));
       } catch (ParseException e) {      
         e.printStackTrace();
       }


1.用java.util.Calender來實(shí)現(xiàn)

   Calendar calendar=Calendar.getInstance();  
   calendar.setTime(new Date());
   System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
   calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+1);//讓日期加1  
   System.out.println(calendar.get(Calendar.DATE));//加1之后的日期Top
===============================================================================

2.用java.text.SimpleDateFormat和java.util.Date來實(shí)現(xiàn)
          
    Date d=new Date();  
   SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
   System.out.println("今天的日期:"+df.format(d));  
   System.out.println("兩天前的日期:" + df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000)));  
   System.out.println("三天后的日期:" + df.format(new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000)));

===============================================================================

GregorianCalendar gc=new GregorianCalendar();
gc.setTime(new Date);
gc.add(field,value);
value為正則往后,為負(fù)則往前
field取1加1年,取2加半年,取3加一季度,取4加一周
取5加一天....

===============================================================================

/*
*java中對(duì)日期的加減操作
*gc.add(1,-1)表示年份減一.
*gc.add(2,-1)表示月份減一.
*gc.add(3.-1)表示周減一.
*gc.add(5,-1)表示天減一.
*以此類推應(yīng)該可以精確的毫秒吧.沒有再試.大家可以試試.
*GregorianCalendar類的add(int field,int amount)方法表示年月日加減.
*field參數(shù)表示年,月.日等.
*amount參數(shù)表示要加減的數(shù)量.
*
* UseDate.java 測試如下:
*/
package temp.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
public class UseDate {

Date d=new Date();
GregorianCalendar gc =new GregorianCalendar();
SimpleDateFormat sf  =new SimpleDateFormat("yyyy-MM-dd");

public String getYears()
{
gc.setTime(d);
gc.add(1,+1);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

return sf.format(gc.getTime());
}

public String getHalfYear()
{
gc.setTime(d);
gc.add(2,+6);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

return sf.format(gc.getTime());
}
public String getQuarters()
{
gc.setTime(d);
gc.add(2,+3);
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DATE));

return sf.format(gc.getTime());
}

public String getLocalDate()
{
return sf.format(d);
}


public static  void  main(String[] args)
{
UseDate ud= new UseDate();
System.out.println(ud.getLocalDate());
System.out.println(ud.getYears());
System.out.println(ud.getHalfYear());
System.out.println(ud.getQuarters());
}

}
    //當(dāng)月第一天和最后一天

     SimpleDateFormat sf  =new SimpleDateFormat("yyyyMMdd");
     Calendar a=Calendar.getInstance();
     int MaxDate=a.get(Calendar.DATE);
     System.out.println("該月最大天數(shù):"+MaxDate+","+sf.format(a.getTime()));
     a.set(Calendar.DATE, 1);//把日期設(shè)置為當(dāng)月第一天 
     System.out.println("當(dāng)月第一天:"+sf.format(a.getTime())); 
     a.roll(Calendar.DATE, -1);//日期回滾一天,也就是最后一天

     System.out.println("當(dāng)月最后一天:"+sf.format(a.getTime())); 

4 ,

GregorianCalendar gc=new GregorianCalendar();
       
        try {
            gc.setTime( new SimpleDateFormat("yyyyMM").parse("200901"));
            gc.add(2, -Integer.parseInt("7"));
        } catch (ParseException e) {
            // TODO 自動(dòng)生成された catch ブロック
            e.printStackTrace();
        }
        System.out.println(new SimpleDateFormat("yyyyMM").format(gc.getTime()));

運(yùn)行結(jié)果:200806



一堣而安 2014-03-18 21:01 發(fā)表評(píng)論
]]>
JAVA身份證驗(yàn)證http://www.tkk7.com/tinguo002/archive/2014/02/27/410374.html一堣而安一堣而安Thu, 27 Feb 2014 02:45:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/02/27/410374.htmlhttp://www.tkk7.com/tinguo002/comments/410374.htmlhttp://www.tkk7.com/tinguo002/archive/2014/02/27/410374.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/410374.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/410374.htmlpublic class IDCard {

      private String _codeError;

      //wi =2(n-1)(mod 11)
      final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
      // verify digit
      final int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
      private int[] ai = new int[18];
      private static String[] _areaCode={"11","12","13","14","15","21","22"
          ,"23","31","32","33","34","35","36","37","41","42","43","44"
          ,"45","46","50","51","52","53","54","61","62","63","64","65","71","81","82","91"};
      private static HashMap<String,Integer> dateMap;
      private static HashMap<String,String> areaCodeMap;
      static{
            dateMap=new HashMap<String,Integer>();
            dateMap.put("01",31);
            dateMap.put("02",null);
            dateMap.put("03",31);
            dateMap.put("04",30);
            dateMap.put("05",31);
            dateMap.put("06",30);
            dateMap.put("07",31);
            dateMap.put("08",31);
            dateMap.put("09",30);
            dateMap.put("10",31);
            dateMap.put("11",30);
            dateMap.put("12",31);
            areaCodeMap=new HashMap<String,String>();
            for(String code:_areaCode){
                  areaCodeMap.put(code,null);
            }
      }

      //驗(yàn)證身份證位數(shù),15位和18位身份證
      public boolean verifyLength(String code){
            int length=code.length();
            if(length==15 || length==18){
                  return true;
            }else{
                  _codeError="錯(cuò)誤:輸入的身份證號(hào)不是15位和18位的";
                  return false;
            }
      }

      //判斷地區(qū)碼
      public boolean verifyAreaCode(String code){
            String areaCode=code.substring(0,2);
//            Element child=  _areaCodeElement.getChild("_"+areaCode);
            if(areaCodeMap.containsKey(areaCode)){
                  return true;
            }else{
                  _codeError="錯(cuò)誤:輸入的身份證號(hào)的地區(qū)碼(1-2位)["+areaCode+"]不符合中國行政區(qū)劃分代碼規(guī)定(GB/T2260-1999)";
                  return false;
            }
      }

      //判斷月份和日期
      public boolean verifyBirthdayCode(String code){
            //驗(yàn)證月份
            String month=code.substring(10,12);
            boolean isEighteenCode=(18==code.length());
            if(!dateMap.containsKey(month)){
                  _codeError="錯(cuò)誤:輸入的身份證號(hào)"+(isEighteenCode?"(11-12位)":"(9-10位)")+"不存在["+month+"]月份,不符合要求(GB/T7408)";
                  return false;
            }
            //驗(yàn)證日期
            String dayCode=code.substring(12,14);
            Integer day=dateMap.get(month);
            String yearCode=code.substring(6,10);
            Integer year=Integer.valueOf(yearCode);

            //非2月的情況
            if(day!=null){
                  if(Integer.valueOf(dayCode)>day || Integer.valueOf(dayCode)<1){
                        _codeError="錯(cuò)誤:輸入的身份證號(hào)"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號(hào)不符合小月1-30天大月1-31天的規(guī)定(GB/T7408)";
                        return false;
                  }
            }
            //2月的情況
            else{
                  //閏月的情況
                  if((year%4==0&&year%100!=0)||(year%400==0)){
                        if(Integer.valueOf(dayCode)>29 || Integer.valueOf(dayCode)<1){
                              _codeError="錯(cuò)誤:輸入的身份證號(hào)"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號(hào)在"+year+"閏年的情況下未符合1-29號(hào)的規(guī)定(GB/T7408)";
                              return false;
                        }
                  }
                  //非閏月的情況
                  else{
                        if (Integer.valueOf(dayCode) > 28 || Integer.valueOf(dayCode) < 1) {
                              _codeError="錯(cuò)誤:輸入的身份證號(hào)"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號(hào)在"+year+"平年的情況下未符合1-28號(hào)的規(guī)定(GB/T7408)";
                              return false;
                        }
                  }
            }
            return true;
      }

      //驗(yàn)證身份除了最后位其他的是否包含字母
      public boolean containsAllNumber(String code) {
            String str="";
            if(code.length()==15){
                  str=code.substring(0,15);
            }else if(code.length()==18){
                  str=code.substring(0,17);
            }
            char[] ch = str.toCharArray();
            for (int i = 0; i < ch.length; i++) {
                  if (! (ch[i] >= '0' && ch[i] <= '9')) {
                        _codeError="錯(cuò)誤:輸入的身份證號(hào)第"+(i+1)+"位包含字母";
                        return false;
                  }
            }
            return true;
      }

      public String getCodeError(){
            return _codeError;
      }

      //驗(yàn)證身份證
      public boolean verify(String idcard) {
            _codeError="";
            //驗(yàn)證身份證位數(shù),15位和18位身份證
            if(!verifyLength(idcard)){
                return false;
            }
            //驗(yàn)證身份除了最后位其他的是否包含字母
            if(!containsAllNumber(idcard)){
                  return false;
            }

            //如果是15位的就轉(zhuǎn)成18位的身份證
            String eifhteencard="";
            if (idcard.length() == 15) {
                  eifhteencard = uptoeighteen(idcard);
            }else{
                  eifhteencard=idcard;
            }
            //驗(yàn)證身份證的地區(qū)碼
            if(!verifyAreaCode(eifhteencard)){
                  return false;
            }
            //判斷月份和日期
            if(!verifyBirthdayCode(eifhteencard)){
                  return false;
            }
            //驗(yàn)證18位校驗(yàn)碼,校驗(yàn)碼采用ISO 7064:1983,MOD 11-2 校驗(yàn)碼系統(tǒng)
            if(!verifyMOD(eifhteencard)){
                  return false;
            }
            return true;
      }

      //驗(yàn)證18位校驗(yàn)碼,校驗(yàn)碼采用ISO 7064:1983,MOD 11-2 校驗(yàn)碼系統(tǒng)
      public boolean verifyMOD(String code){
            String verify = code.substring(17, 18);
            if("x".equals(verify)){
                  code=code.replaceAll("x","X");
                  verify="X";
            }
            String verifyIndex=getVerify(code);
            if (verify.equals(verifyIndex)) {
                  return true;
            }
//            int x=17;
//            if(code.length()==15){
//                  x=14;
//            }
            _codeError="錯(cuò)誤:輸入的身份證號(hào)最末尾的數(shù)字驗(yàn)證碼錯(cuò)誤";
            return false;
      }

      //獲得校驗(yàn)位
      public String getVerify(String eightcardid) {
            int remaining = 0;

            if (eightcardid.length() == 18) {
                  eightcardid = eightcardid.substring(0, 17);
            }

            if (eightcardid.length() == 17) {
                  int sum = 0;
                  for (int i = 0; i < 17; i++) {
                        String k = eightcardid.substring(i, i + 1);
                        ai[i] = Integer.parseInt(k);
                  }

                  for (int i = 0; i < 17; i++) {
                        sum = sum + wi[i] * ai[i];
                  }
                  remaining = sum % 11;
            }

            return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
      }

      //15位轉(zhuǎn)18位身份證
      public String uptoeighteen(String fifteencardid) {
            String eightcardid = fifteencardid.substring(0, 6);
            eightcardid = eightcardid + "19";
            eightcardid = eightcardid + fifteencardid.substring(6, 15);
            eightcardid = eightcardid + getVerify(eightcardid);
            return eightcardid;
      }



調(diào) 用 new IDCard().verify(身份證id);
轉(zhuǎn)載:http://www.oschina.net/code/snippet_249203_24013


一堣而安 2014-02-27 10:45 發(fā)表評(píng)論
]]>
java獲取路徑http://www.tkk7.com/tinguo002/archive/2014/02/24/410231.html一堣而安一堣而安Mon, 24 Feb 2014 02:43:00 GMThttp://www.tkk7.com/tinguo002/archive/2014/02/24/410231.htmlhttp://www.tkk7.com/tinguo002/comments/410231.htmlhttp://www.tkk7.com/tinguo002/archive/2014/02/24/410231.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/410231.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/410231.html

getAbsolutePath() 得到絕對(duì)路徑、全路徑。
getpath 得到縮寫的路徑,根據(jù)當(dāng)前目錄位置可以縮寫路徑。得到相對(duì)路徑
getCanonicalPath() 得到標(biāo)準(zhǔn)路徑,將統(tǒng)一平臺(tái)間的路徑寫法差異。
package util;

import java.io.File;

public class CurrentDirectory {
    
    
public static void print(Object o) {
        System.out.println(o);
    }


    
public static void main(String[] args) throws Exception {
        print(Thread.currentThread().getContextClassLoader().getResource(
""));
        print(CurrentDirectory.
class.getClassLoader().getResource(""));
        
//print(this.getClass().getResource("/").toString().replace("file:/", "")); //在非靜態(tài)方法中可以使用
        print(ClassLoader.getSystemResource(""));
        print(CurrentDirectory.
class.getResource(""));
        print(CurrentDirectory.
class.getResource("/"));
        print(
new File("").getAbsolutePath());
        print(System.getProperty(
"user.dir"));
    }

    
}




一堣而安 2014-02-24 10:43 發(fā)表評(píng)論
]]>
樹形全部展示代碼http://www.tkk7.com/tinguo002/archive/2013/12/30/408218.html一堣而安一堣而安Mon, 30 Dec 2013 11:16:00 GMThttp://www.tkk7.com/tinguo002/archive/2013/12/30/408218.htmlhttp://www.tkk7.com/tinguo002/comments/408218.htmlhttp://www.tkk7.com/tinguo002/archive/2013/12/30/408218.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/408218.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/408218.htmljsp:代碼

<script type="text/javascript">

  $(function(){
   //alert('${tip}');
   if('${tip}'!=''){
    $('#tip').css('display','inline-block');
   }
  });
 
  var zTreeObj;
  var allSelectedId="";
  var allSelectedName = "";
  var zNodes = ${data};
  var setting = {
   isSimpleData: true,
   treeNodeKey: "id",         //設(shè)置節(jié)點(diǎn)唯一標(biāo)識(shí)屬性名稱
   treeNodeParentKey: "pId",  //設(shè)置節(jié)點(diǎn)的父節(jié)點(diǎn)唯一標(biāo)識(shí)屬性名稱
   nameCol: "name",           //設(shè)置 zTree 顯示節(jié)點(diǎn)名稱的屬性名稱,此處默認(rèn)為Name
   showLine: true,            //在樹型中是否顯示線條樣式
   
   check: {
    enable: true,
    chkStyle: "<%=type%>",
    chkboxType: <%=chkboxType%>,
    radioType: "all"
   },
   callback: {
    onClick: onClick
   },
   data: {
    simpleData: {
     enable: true
    }
   }
  };
   
  $(function(){
   $("#btnClose").bind("click",doClose);
   $("#btnConfirm").bind("click",doConfirm);
   zTreeObj = $("#tree").zTree(setting,zNodes);
  });
  
  //關(guān)閉窗口
  function doClose(){
   window.close();
  }
   
  function doConfirm(){
   var checkedNodes = zTreeObj.getCheckedNodes(true);
   alert(checkedNodes);
   for (i=0;i<checkedNodes.length;i++) {
   
    var treeNode = checkedNodes[i];
    alert(treeNode.id);
     if(!treeNode.open){
      allSelectedId += (allSelectedId == "" ? "" : ",") + treeNode.id;
      allSelectedName += (allSelectedName == "" ? "" : ",") + treeNode.name;      
     }
    }
   $('#hiddenId').val(allSelectedId);
   $('#hiddenName').val(allSelectedName);
  }
  
  function showMenu() {
   var deptname = $("#deptname");
   var offset = deptname.offset();
   $("#menu").width(deptname.width())
   .css({left:offset.left + "px", top:offset.top + deptname.outerHeight() + "px"})
   .slideDown("fast");
  }
  
  function hideMenu() {
   $("#menu").fadeOut("fast");
  }
  
  function onClick(e, treeId, treeNode) {
   alert(treeId);
   if(treeNode.checked){
    hideMenu();
   }else{
    zTreeObj.checkNode(treeNode, true, null, true);
   }
   return false;
  }
  
  function onCheck(e, treeId, treeNode){
   alert(treeNode.id);
   allSelectedId += (allSelectedId == "" ? "" : ",") + treeNode.pId+"|"+treeNode.id+"|"+treeNode.name;
  }
 </script>

java代碼:

public class CameraTreeAPI {
 private static Logger log = Logger.getLogger(CameraTreeAPI.class);
 
 /**
  *
  * 功能說明:獲取整個(gè)ztree攝像頭樹
  * @return  滿足ztree要求的json數(shù)據(jù)
  * String
  * @author chh
  * @Jun 14, 2012
  */
 public  String getZTree(String systemUnid,String selectUnid){
  selectUnid = ","+StrUtil.formatNull(selectUnid)+",";
  JSONArray array = new JSONArray();
  try{
   List<BusinessCamera> list = new BusinessCameraManager().doFindBySystemUnid(systemUnid);
   
   JSONObject top = new JSONObject();
   top.put("id","0");
   top.put("name","攝像頭列表");
   top.put("open",true);
   array.add(top);
   
   if(list!=null && list.size()>0){    
    for(Object object : list){
     BusinessCamera camera = (BusinessCamera)object;
     JSONObject json = new JSONObject();
     json.put("id",camera.getUnid());
     json.put("name",camera.getName());
     json.put("pId",camera.getPunid());
     json.put("checked", selectUnid.indexOf(camera.getUnid()) >= 0);
     if(hasChildren(list,camera)){
      json.put("open",true);
     }
     array.add(json);
    }
   }
  }catch(Exception e){
   e.printStackTrace();
   log.error(e.getMessage(),e);
  }
  return array.toString();
 }
 
 public boolean hasChildren(List<BusinessCamera> allData,BusinessCamera camera){
  if(allData == null || allData.isEmpty() || camera == null){
   return false;
  }
  for(BusinessCamera unit : allData){
   if(unit.getUnid().equalsIgnoreCase(camera.getUnid())){
    continue;
   }
   if(camera.getUnid().equalsIgnoreCase(unit.getPunid())){
    return true;
   }
  }
  return false;
 }
 
}




一堣而安 2013-12-30 19:16 發(fā)表評(píng)論
]]>
耦合度與設(shè)計(jì)模式 http://www.tkk7.com/tinguo002/archive/2013/10/09/404774.html一堣而安一堣而安Wed, 09 Oct 2013 01:16:00 GMThttp://www.tkk7.com/tinguo002/archive/2013/10/09/404774.htmlhttp://www.tkk7.com/tinguo002/comments/404774.htmlhttp://www.tkk7.com/tinguo002/archive/2013/10/09/404774.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/404774.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/404774.htmlhttp://socket.blog.163.com/blog/static/209873004201096210555/

高內(nèi)聚,低耦合(High Cohesion、Low Coupling)
是一句近乎于“為實(shí)現(xiàn)四個(gè)現(xiàn)代化而努力”式的口號(hào),耳熟并不一定能詳。這個(gè)口號(hào)是在軟件工程里第一次碰到的,它的定義估計(jì)是計(jì)算專業(yè)術(shù)語里最欠扁的定義,內(nèi)聚:一個(gè)模塊內(nèi)各個(gè)元素彼此結(jié)合的緊密程序。耦合:一個(gè)軟件結(jié)構(gòu)內(nèi)不同模塊之間互連程序的度量。這兩個(gè)定義,相當(dāng)?shù)?#8220;形而上學(xué)”“不知所云”。這是軟件工程中判斷設(shè)計(jì)好壞的標(biāo)準(zhǔn),主要是面向?qū)ο笤O(shè)計(jì),主要是看類的內(nèi)聚性是否高,偶合度是否低。為什么要高內(nèi)聚,使模塊之間的關(guān)系越緊密出錯(cuò)就趙少,越緊密運(yùn)行效率越高!低耦合,就是說,子程序之間的關(guān)系越簡單,越容易擴(kuò)展和維護(hù),越復(fù)雜就會(huì)產(chǎn)生出更多的意想不到的錯(cuò)誤!不利于維護(hù)。

       
程序員天生都是內(nèi)聚的高手,違反高內(nèi)聚的反而是那些有一定水平的程序員,可能是誤用了設(shè)計(jì)模式,可能是考慮問題過分全面,把一個(gè)功能單一的類分割成多個(gè)類。這里重點(diǎn)說低耦合,耦合就是類之間的互相調(diào)用關(guān)系,如果耦合很強(qiáng),互相牽扯調(diào)用很多,那么會(huì)牽一發(fā)而動(dòng)全身,不利于維護(hù)和擴(kuò)展。什么方法、數(shù)據(jù)、不加分析地往一個(gè)類里整,甚至?xí)r一個(gè)類的所有數(shù)據(jù)成員設(shè)成public,方便在其它類里直接修改數(shù)據(jù)。生活中很多“低耦合”的例子,如床與床單,如果是緊耦合的話,換床單時(shí),也要換床。再如桌子與抽屜。


耦合度與設(shè)計(jì)模式 - share39 - 互聯(lián)的天空


      
圖中的上半部分中,把一個(gè)軟件模塊抽象成線條,軟件模塊互相依賴,這是一種面向過程的開發(fā)方式,如果軟件是不改變的,那么這種高耦合度的結(jié)構(gòu)也無可厚非,有時(shí)象底層軟件如操作系統(tǒng)等,一切以執(zhí)行效率優(yōu)先,而且需求并不改變頻繁的情況下,是適用的。但在實(shí)際開發(fā)過程中,軟件需求的變化是導(dǎo)致這種模式被否定的真正原因。一旦有一個(gè)模塊改變,那么需要改動(dòng)到其它好幾個(gè)模塊,而且更要命的時(shí),被影響的模塊又會(huì)影響其它模塊,或者干脆又把影響力傳遞給當(dāng)初最先改動(dòng)的模塊。


       
下圖中的模塊結(jié)構(gòu)通過梳理,使他們依附于一條粗線條,它是主邏輯模塊,是相對(duì)穩(wěn)定的高層的、抽象的模塊。而其它通過一個(gè)小空心圓點(diǎn)(表示接口)與粗線條進(jìn)行連接的小短線條代表次模塊,它被分成兩個(gè)部分,接口和實(shí)現(xiàn)接口的子類對(duì)象,它們通過接口與主邏輯模塊形成依賴。相對(duì)來說,次模塊變化較快,而主模塊變化較慢。剛才提到的生活例子中,床相對(duì)于床單來說是主要的,核心的,成本較高的,它的使用年限是10年,而床單使用年限是2年,就是說床的模塊變化速度慢于床單的變化速度,如果床2個(gè)月變一次,那么邏輯結(jié)構(gòu)就混亂了。床為床單提供了一個(gè)尺寸接口,床單只要符合這個(gè)接口,就可以組合使用。那么這個(gè)接口就必須是相對(duì)穩(wěn)定的。


      
設(shè)計(jì)模式做為軟件開發(fā)過程中一種創(chuàng)造性的總結(jié)思維,使軟件開發(fā)人員,在開發(fā)軟件產(chǎn)品時(shí),有了更加明確的軟件解耦的思路,設(shè)計(jì)模式來源于生活,卻指導(dǎo)于軟件。事實(shí)上,短期來看,要求低耦合并沒有很明顯的好處,因?yàn)槔迷O(shè)計(jì)模式來解決問題是需要軟件開發(fā)的智力和時(shí)間成本的。況且引入了誤用設(shè)計(jì)模式的風(fēng)險(xiǎn),所以短期內(nèi)使用設(shè)計(jì)模式來解耦反而會(huì)影響系統(tǒng)的開發(fā)進(jìn)度。低耦合的好處是長期回報(bào)的,體現(xiàn)在系統(tǒng)持續(xù)發(fā)展的過程中,系統(tǒng)具有更好的重用性,維護(hù)性,擴(kuò)展性,持續(xù)的支持業(yè)務(wù)的發(fā)展,而不會(huì)成為業(yè)務(wù)發(fā)展的障礙。



一堣而安 2013-10-09 09:16 發(fā)表評(píng)論
]]>
ArrayList的toArray(轉(zhuǎn))http://www.tkk7.com/tinguo002/archive/2013/07/04/401205.html一堣而安一堣而安Thu, 04 Jul 2013 03:52:00 GMThttp://www.tkk7.com/tinguo002/archive/2013/07/04/401205.htmlhttp://www.tkk7.com/tinguo002/comments/401205.htmlhttp://www.tkk7.com/tinguo002/archive/2013/07/04/401205.html#Feedback0http://www.tkk7.com/tinguo002/comments/commentRss/401205.htmlhttp://www.tkk7.com/tinguo002/services/trackbacks/401205.html
http://www.cnblogs.com/ihou/archive/2012/05/10/2494578.html

ArrayList提供了一個(gè)將List轉(zhuǎn)為數(shù)組的一個(gè)非常方便的方法toArray。toArray有兩個(gè)重載的方法:

1.list.toArray();

2.list.toArray(T[]  a);

對(duì)于第一個(gè)重載方法,是將list直接轉(zhuǎn)為Object[] 數(shù)組;

第二種方法是將list轉(zhuǎn)化為你所需要類型的數(shù)組,當(dāng)然我們用的時(shí)候會(huì)轉(zhuǎn)化為與list內(nèi)容相同的類型。

不明真像的同學(xué)喜歡用第一個(gè),是這樣寫:

1
2
3
4
5
6
7
ArrayList<String> list=new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            list.add(""+i);
        }
       
        String[] array= (String[]) list.toArray();
      

結(jié)果一運(yùn)行,報(bào)錯(cuò):

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

原因一看就知道了,不能將Object[] 轉(zhuǎn)化為String[].轉(zhuǎn)化的話只能是取出每一個(gè)元素再轉(zhuǎn)化,像這樣:

1
2
3
4
5
Object[] arr = list.toArray();
        for (int i = 0; i < arr.length; i++) {
            String e = (String) arr[i];
            System.out.println(e);
        }

所以第一個(gè)重構(gòu)方法就不是那么好使了。

實(shí)際上,將list世界轉(zhuǎn)化為array的時(shí)候,第二種重構(gòu)方法更方便,用法如下:

1
2
String[] array =new String[list.size()];
        list.toArray(array);<br><br>另附,兩個(gè)重構(gòu)方法的源碼:

1.
public Object[] toArray(); {
Object[] result = new Object[size];
System.arraycopy(elementData, 0, result, 0, size);;
return result;
}

2.

public Object[] toArray(Object a[]); {
if (a.length < size);
a = (Object[]);java.lang.reflect.Array.newInstance(
a.getClass();.getComponentType();, size);;
System.arraycopy(elementData, 0, a, 0, size);;

if (a.length > size);
a[size] = null;

return a;
}

1
<br><br>
1
2
<br>
  


一堣而安 2013-07-04 11:52 發(fā)表評(píng)論
]]>
主站蜘蛛池模板: 亚洲另类古典武侠| 亚洲无线一二三四区手机| 久久亚洲一区二区| 中国国产高清免费av片| 久久久久久A亚洲欧洲AV冫| 国产成人无码精品久久久免费| 亚洲AV蜜桃永久无码精品| 国产成人不卡亚洲精品91| 在线看片无码永久免费aⅴ| 亚洲人成自拍网站在线观看| 精品少妇人妻AV免费久久洗澡| 亚洲а∨精品天堂在线| 免费一级黄色毛片| jizz中国免费| 久久亚洲精品中文字幕无码| 精品无码国产污污污免费网站| 亚洲午夜电影一区二区三区| 成年女人18级毛片毛片免费观看| 亚洲人成网站在线在线观看| 免费一级特黄特色大片在线观看| 99免费在线视频| 亚洲欧洲精品视频在线观看| 成人毛片免费观看视频大全| 羞羞漫画小舞被黄漫免费| 在线亚洲v日韩v| 亚洲免费日韩无码系列| 丁香花在线视频观看免费 | 成人浮力影院免费看| 亚洲人妖女同在线播放| 日韩免费在线观看| 两性色午夜视频免费网| 亚洲码一区二区三区| 好爽好紧好大的免费视频国产| 一区二区三区免费精品视频| 亚洲AV无码专区国产乱码电影 | 亚洲精品国产肉丝袜久久| 日韩免费观看一级毛片看看| 青柠影视在线观看免费高清 | 久久亚洲国产精品123区| 97公开免费视频| 国产av无码专区亚洲av毛片搜|