google code prettify
2017年5月1日 星期一
分層領域模型
DO(Data Object):與數據庫表結構一一對應,通過DAO層向上傳輸數據源對象。
DTO(Data Transfer Object):數據傳輸對向,Service和Manager向外傳輸的對象。
BO(Business Object):業務對象。可以由Service層輸出的封裝業務邏輯對象。
VO(Value Object):顯示顯對象,通常是web向模板渲染引擎層傳輸對象。
2016年9月30日 星期五
用docker啟動redis服務
1、先至docker hub上找到redis頁面
https://hub.docker.com/_/redis/
2、依網頁上的指令pull redis
開啟Terminal,輸入「docker pull redis」
3、找到image id
輸入「docker images」
4、啟動redis
輸入「docker run --name redis -d -p 6379:6379 ${imageID}」
指令中的${imageID}記得要換成自己的image id,像圖片中的redis image id是1aa84b1b434e
所以我本機輸入的指令為「docker run --name redis -d -p 6379:6379 1aa84b1b434e」
Reference:
https://hub.docker.com/_/redis/
http://stackoverflow.com/questions/31889163/cannot-connect-to-redis-running-as-container-with-boot2docker
Mac安裝docker-compose
1、安裝setuptools
輸入「brew install python」
2、透過easy_install安裝pip
輸入「sudo easy_install pip」
3、透過pip安裝docker-compose
輸入「pip install docker-compose」
4、驗証docker-compose是否安裝成功
輸入「docker-compose」
Reference:
http://docs.python-guide.org/en/latest/starting/install/osx/
https://docs.docker.com/compose/install/
輸入「brew install python」
2、透過easy_install安裝pip
輸入「sudo easy_install pip」
3、透過pip安裝docker-compose
輸入「pip install docker-compose」
4、驗証docker-compose是否安裝成功
輸入「docker-compose」
Reference:
http://docs.python-guide.org/en/latest/starting/install/osx/
https://docs.docker.com/compose/install/
2016年7月16日 星期六
LeetCode - 371. Sum of Two Integers
Question:
Calculate the sum of two integers a and b, but you are not allowed to use the operator
+ and -.
Example:
Given a = 1 and b = 2, return 3.
Given a = 1 and b = 2, return 3.
Solution:
public class Solution {
public int getSum(int a, int b) {
return a+b;
}
}
LeetCode - 374. Guess Number Higher or Lower
Question:
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number is higher or lower.
You call a pre-defined API
guess(int num) which returns 3 possible results (-1, 1, or 0):
Solution:
public class Solution extends GuessGame {
public int guessNumber(int n) {
Long R = new Long(n);
Long L = 1L;
if(R!=L){
while(R>=L){
Long mid = ((L+R)>>1);
int result = guess(mid.intValue());
//System.out.println("mid:" + mid + " ,result:" + result);
if(result ==1){
L = mid+1;
}else if(result ==-1){
R = mid-1;
}else{
return mid.intValue();
}
}
}else{
return R.intValue();
}
return L.intValue();
}
}
LeetCode - 20. Valid Parentheses
Question:
Given a string containing just the characters
'(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order,
"()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Solution:
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
char[] array = s.toCharArray();
if (array.length % 2 != 0) {
return false;
}
for(int i =0;i<array.length;i++){
char c = array[i];
if(c=='(' || c=='[' || c=='{'){
stack.push(c);
continue;
}
if(stack.isEmpty()==true){
return false;
}
if(c==')'){
if (stack.pop()!='(')
return false;
}else if(c==']'){
if(stack.pop()!='[')
return false;
}else if(c=='}'){
if(stack.pop()!='{')
return false;
}else{
return false;
}
}
if(stack.isEmpty()==false){
return false;
}else{
return true;
}
}
}
2016年7月2日 星期六
在Mac上安裝Docker
關於Docker就不多說了,Docker現在出了Docker for Mac Beta
因此就不用再使用Docker Toolbox了
下載位置:https://docs.docker.com/docker-for-mac/
1、安裝非常簡單,從左邊拉到右邊就好了

2、安裝後執行,就可以在右上角看到執行中的圖示
3、就開始開終端機開始使用docker了,用「docker --version」確認版本資訊
4、安裝nginx,輸入指令「docker run -d -p 80:80 --name webserver nginx」
5、打開瀏覽器輸入「http://localhost/」就可以看到已經建立nginx服務了
Reference:
https://docs.docker.com/docker-for-mac/
因此就不用再使用Docker Toolbox了
下載位置:https://docs.docker.com/docker-for-mac/
1、安裝非常簡單,從左邊拉到右邊就好了

2、安裝後執行,就可以在右上角看到執行中的圖示
3、就開始開終端機開始使用docker了,用「docker --version」確認版本資訊
4、安裝nginx,輸入指令「docker run -d -p 80:80 --name webserver nginx」
5、打開瀏覽器輸入「http://localhost/」就可以看到已經建立nginx服務了
Reference:
https://docs.docker.com/docker-for-mac/
2016年3月19日 星期六
運用Jsoup取得網頁資訊
如果要使用Java去parse Web page, 可以使用Jsoup,簡單好用
以下範例為使用jsoup抓取yahoo 首頁的新聞標題
程式碼可以至github下載
以下範例為使用jsoup抓取yahoo 首頁的新聞標題
package test.charles.JsoupExample;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class App
{
public static void main( String[] args ) throws IOException
{
//read yahoo home page
String url = "https://tw.yahoo.com/";
Document doc = Jsoup.connect(url).get();
//get first news tab one
Element t1 = doc.getElementById("t1");
//get news title
Elements newsTitle = t1.select("a[href] > span");
//print size
System.out.println("size:" + newsTitle.size());
//print news title
for(Element e:newsTitle){
System.out.println("title:" + e.text());
}
}
}
程式碼可以至github下載
2016年3月16日 星期三
在Mac上安裝Cordova
當想要快速開發一個簡單的app又不會寫Android及iOS原生程式的話
我很推薦使用Cordova
這是一種使用Html + Javascript + CSS來模擬app,對於網頁工程師並沒什麼門檻 以下範列為在Mac上安裝Cordova,並建立範例的Android project
1、透過nodejs安裝cordova
輸入「sudo npm install cordova -g」
-g代表將此套件安裝在全域的資料夾中

2、檢查是否安裝成功
輸入「cordova --version」

3、切換到資料夾下建立Cordova專案
輸入「sudo cordova create hello」

此時可以看到成功產生出Cordova的專案

4、切到目錄下產生Android的專案
輸入「sudo cordova platforms add android」

5、之後再到platforms下檢查就可以看到android的專案嚕

結束!!
我很推薦使用Cordova
這是一種使用Html + Javascript + CSS來模擬app,對於網頁工程師並沒什麼門檻 以下範列為在Mac上安裝Cordova,並建立範例的Android project
1、透過nodejs安裝cordova
輸入「sudo npm install cordova -g」
-g代表將此套件安裝在全域的資料夾中

2、檢查是否安裝成功
輸入「cordova --version」

3、切換到資料夾下建立Cordova專案
輸入「sudo cordova create hello」

此時可以看到成功產生出Cordova的專案

4、切到目錄下產生Android的專案
輸入「sudo cordova platforms add android」

5、之後再到platforms下檢查就可以看到android的專案嚕

結束!!
2016年3月3日 星期四
Java Collections.sort
當使用自訂義的類別想要排序時,可以採用Collections.sort
範例如下
程式碼可以至github下載
範例如下
package test.charles.CollectionSort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class App
{
public static void main( String[] args )
{
List userList = new ArrayList();
//add user name and age
userList.add(new User("user1",19));
userList.add(new User("user2",40));
userList.add(new User("user3",3));
userList.add(new User("user4",27));
userList.add(new User("user5",10));
userList.add(new User("user6",70));
//sort by user age
Collections.sort(userList, new Comparator() {
public int compare(User o1, User o2) {
return o1.getAge().compareTo(o2.getAge());
}
});
//print information
for(User s :userList){
System.out.println("name:" + s.getName() + ",age:" + s.getAge());
}
}
}
程式碼可以至github下載
2016年2月21日 星期日
用javamail 取得mail資訊
最近在工作上遇到需要用程式抓取mail的相關資訊(收件者、寄件者、標題、內容…etc)
因為google到大多數都是採用javamail來實現,
而程式碼也相當簡單
程式碼也可以至github下載
因為google到大多數都是採用javamail來實現,
而程式碼也相當簡單
package lab.charles.email.test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailServer {
public static void main(String[] args) throws Exception{
// mail server connection parameters
String host = "mail server";
String user = "user name";
String password = "password";
// connect to my pop3 inbox
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
Store store = session.getStore("pop3");
store.connect(host, user, password);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
// get the list of inbox messages
Message[] messages = inbox.getMessages();
if (messages.length == 0) System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
// stop after listing ten messages
if (i > 10) {
System.exit(0);
inbox.close(true);
store.close();
}
System.out.println("Message " + (i + 1));
System.out.println("From : " + messages[i].getFrom()[0]);
System.out.println("Subject : " + messages[i].getSubject());
System.out.println("Sent Date : " + messages[i].getSentDate());
System.out.println();
}
inbox.close(true);
store.close();
}
}
程式碼也可以至github下載
2014年1月3日 星期五
在S3使用Cordova的照片功能會Crush
目前工作上有使用Cordova開發Mobile App
最近遇到一個很奇怪的問題,只要在S3上使用照像功能就會造成App Crush…
後來發現是因為相素太高,造成out of memory
後來網路上有提供相當多解法
http://community.phonegap.com/nitobi/topics/phonegap_camera_crash_on_android_4_1_2_samsung_galaxy_s3_htc
而我自己是使用以下的解法
http://community.phonegap.com/nitobi/topics/samsung_galaxy_s3_camera_crash
在AndroidManifest.xml中所有的activity中加上
android:alwaysRetainTaskState="true" android:launchMode="singleTask"這個屬性就可以了
最近遇到一個很奇怪的問題,只要在S3上使用照像功能就會造成App Crush…
後來發現是因為相素太高,造成out of memory
後來網路上有提供相當多解法
http://community.phonegap.com/nitobi/topics/phonegap_camera_crash_on_android_4_1_2_samsung_galaxy_s3_htc
而我自己是使用以下的解法
http://community.phonegap.com/nitobi/topics/samsung_galaxy_s3_camera_crash
在AndroidManifest.xml中所有的activity中加上
android:alwaysRetainTaskState="true" android:launchMode="singleTask"這個屬性就可以了
SQL Server查詢所有資料表及欄位屬性
現在在工作上常常因為客戶心血來潮就要我們交Table Schema
但哪來的時間去整理啊QQ
所以我現在都很賤的直接用SQL語法匯出直接交附XD
SQL語法如下:
SELECT
資料表名稱=Table_Name
,資料行名稱=COLUMN_Name
,資料型別=DATA_TYPE
,長度=CHARACTER_MAXIMUM_LENGTH
,是否允許NULL=IS_NULLABLE
, 預設值=COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.Columns
order by Table_Name
但哪來的時間去整理啊QQ
所以我現在都很賤的直接用SQL語法匯出直接交附XD
SQL語法如下:
SELECT
資料表名稱=Table_Name
,資料行名稱=COLUMN_Name
,資料型別=DATA_TYPE
,長度=CHARACTER_MAXIMUM_LENGTH
,是否允許NULL=IS_NULLABLE
, 預設值=COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.Columns
order by Table_Name
2013年12月9日 星期一
Window 7 上關閉Chrome的web security
chrome.exe --user -data-dir="C:/Chrome dev session" --disable-web-security
2013年11月21日 星期四
Android apk上架流程
1、關掉debug模式
將AndroidManifest.xml裡的android:debuggable設為false
2、匯出apk檔時記得使用keystore加簽
將來app更新時都需要使用相同的keystore才可以
3、登入Google paly Developer Console及付費開通
https://play.google.com/apps/publish/
4、開通後選擇新增應用程式
5、輸入app標題及上傳apk及輸入相關資訊
6、選擇上傳apk後可以看到正式發佈階段、Beta測試階段、Alpha測試階段
正式發佈:發佈至Google Play
Beta:外部測試
Alpha:內部測試
(但要測試寄apk給對方不是容易多了嗎…)
7、之後再去商店資訊、定價與發佈填寫相關資訊及上傳截圖即可
將AndroidManifest.xml裡的android:debuggable設為false
2、匯出apk檔時記得使用keystore加簽
將來app更新時都需要使用相同的keystore才可以
3、登入Google paly Developer Console及付費開通
https://play.google.com/apps/publish/
4、開通後選擇新增應用程式
5、輸入app標題及上傳apk及輸入相關資訊
6、選擇上傳apk後可以看到正式發佈階段、Beta測試階段、Alpha測試階段
正式發佈:發佈至Google Play
Beta:外部測試
Alpha:內部測試
(但要測試寄apk給對方不是容易多了嗎…)
7、之後再去商店資訊、定價與發佈填寫相關資訊及上傳截圖即可
用Eclipse打包Android app(apk)
1、在Android專案上右鍵→Android Tool→Export Sign Application Package...
2、選擇匯出專案
3、產生keystore,並輸入密碼及產生路徑
keystore用於對apk作加簽,因此上架之後app更新必須都使用相同的keystore加簽才可以
所以務必保存及記下密碼
4、 輸入keysotre中同開發者資訊及密碼
較需要注意的為Validity(years),google是建議至少為25年
5、選擇apk檔匯出路徑,之後就apk檔丟到手機安裝即可
2013年10月1日 星期二
Java String、StringBuffer、StringBuilder的差別
1 String
String:字符串常量,字符串長度不可變。
2 StringBuffer
StringBuffer:字符串變量(Synchronized,即線程安全)。如果要頻繁對字符串內容進行修改,出於效率考慮最好使用StringBuffer,如果想轉成String類型,可以調用StringBuffer的toString()方法。
Java.lang.StringBuffer線程安全的可變字符序列。在任意時間點上它都包含某種特定的字符序列,但通過某些方法調用可以改變該序列的長度和內容。可將字符串緩沖區安全地用於多個線程。
StringBuffer 上的主要操作是 append 和 insert 方法,可重載這些方法,以接受任意類型的數據。每個方法都能有效地將?定的數據轉換成字符串,然後將該字符串的字符追加或插入到字符串緩沖區中。append 方法始終將這些字符添加到緩沖區的末端;而 insert 方法則在指定的點添加字符。例如,如果 z 引用一個當前內容是“start”的字符串緩沖區對象,則此方法調用 z.append("le") 會使字符串緩沖區包含“startle”,而 z.insert(4, "le") 將更改字符串緩沖區,使之包含“starlet”。
3 StringBuilder
StringBuilder:字符串變量(非線程安全)。
java.lang.StringBuilder是一個可變的字符序列,是JDK5.0新增的。此類提供一個與 StringBuffer 兼容的 API,但不保證同步。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩沖區被單個線程使用的時候(這種情況很普遍)。如果可能,建議優先采用該類,因?在大多數實現中,它比 StringBuffer 要快。兩者的方法基本相同。
在大部分情況下,StringBuilder > StringBuffer。
4 三者區別
String 類型和StringBuffer的主要性能區別:String是不可變的對象, 因此在每次對 String 類型進行改變的時候,都會生成一個新的 String 對象,然後將指針指向新的 String 對象,所以經常改變內容的字符串最好不要用 String ,因?每次生成對象都會對系統性能產生影響,特別當內存中無引用對象多了以後, JVM 的 GC 就會開始工作,性能就會降低。
使用 StringBuffer 類時,每次都會對 StringBuffer 對象本身進行操作,而不是生成新的對象並改變對象引用。所以多數情況下推薦使用 StringBuffer ,特別是字符串對象經常改變的情況下。
在某些特別情況下, String 對象的字符串拼接其實是被 JVM 解釋成了 StringBuffer 對象的拼接,所以這些時候 String 對象的速度並不會比 StringBuffer 對象慢,例如:
生成 String S1 對象的速度並不比 StringBuffer慢。其實在 JVM 裏,自動做了如下轉換:
String S1 = “This is only a” + “ simple” + “test”;
JVM直接把上述語句當作:
String S1 = “This is only a simple test”;
所以速度很快。但要注意的是,如果拼接的字符串來自另外的String對象的話,JVM就不會自動轉換了,速度也就沒那麼快了,例如:
String S2 = “This is only a”;
String S3 = “ simple”;
String S4 = “ test”;
String S1 = S2 +S3 + S4;
這時候,JVM 會規規矩矩的按照原來的方式去做。
在大部分情況下,StringBuffer > String。
5 使用策略
(1)基本原則:如果要操作少量的數據,用String ;單線程操作大量數據,用StringBuilder ;多線程操作大量數據,用StringBuffer。
(2)不要使用String類的"+"來進行頻繁的拼接,因?那?的性能極差的,應該使用StringBuffer或StringBuilder類,這在Java的優化上是一條比較重要的原則。舉個例子:在使用String 的時候,拼接字符串時使用“+”在JVM上形成臨時的StringBuffer對象,同時在每一個字符串上都建立一個對象,拼接了兩個字符串一共需要創建4個對象!(一個保存結果的String,兩個字符串對象,一個臨時的StringBuffer對象)。而使用StringBuffer的話,只需創建2個對象!一個StringBuffer對象和保存最後結果的的String對象 。
(3)?了獲得更好的性能,在構造 StirngBuffer 或 StirngBuilder 時應盡可能指定它們的容量。當然,如果你操作的字符串長度不超過 16 個字符就不用了。不指定容量會顯著降低性能。
(4)StringBuilder一般使用在方法內部來完成類似"+"功能,因?是線程不安全的,所以用完以後可以丟棄。StringBuffer主要用在全局變量中。
(5)相同情況下使用 StirngBuilder 相比使用 StringBuffer 僅能獲得 10%~15% 左右的性能提升,但卻要冒多線程不安全的風險。而在現實的模塊化編程中,負責某一模塊的程序員不一定能清晰地判斷該模塊是否會放入多線程的環境中運行,因此:除非確定系統的瓶頸是在 StringBuffer 上,並且確定你的模塊不會運行在多線程模式下,才可以采用StringBuilder;否則還是用StringBuffer。
資料來源:http://rritw.com/a/JAVAbiancheng/JAVAzonghe/20130702/377964.html
String:字符串常量,字符串長度不可變。
2 StringBuffer
StringBuffer:字符串變量(Synchronized,即線程安全)。如果要頻繁對字符串內容進行修改,出於效率考慮最好使用StringBuffer,如果想轉成String類型,可以調用StringBuffer的toString()方法。
Java.lang.StringBuffer線程安全的可變字符序列。在任意時間點上它都包含某種特定的字符序列,但通過某些方法調用可以改變該序列的長度和內容。可將字符串緩沖區安全地用於多個線程。
StringBuffer 上的主要操作是 append 和 insert 方法,可重載這些方法,以接受任意類型的數據。每個方法都能有效地將?定的數據轉換成字符串,然後將該字符串的字符追加或插入到字符串緩沖區中。append 方法始終將這些字符添加到緩沖區的末端;而 insert 方法則在指定的點添加字符。例如,如果 z 引用一個當前內容是“start”的字符串緩沖區對象,則此方法調用 z.append("le") 會使字符串緩沖區包含“startle”,而 z.insert(4, "le") 將更改字符串緩沖區,使之包含“starlet”。
3 StringBuilder
StringBuilder:字符串變量(非線程安全)。
java.lang.StringBuilder是一個可變的字符序列,是JDK5.0新增的。此類提供一個與 StringBuffer 兼容的 API,但不保證同步。該類被設計用作 StringBuffer 的一個簡易替換,用在字符串緩沖區被單個線程使用的時候(這種情況很普遍)。如果可能,建議優先采用該類,因?在大多數實現中,它比 StringBuffer 要快。兩者的方法基本相同。
在大部分情況下,StringBuilder > StringBuffer。
4 三者區別
String 類型和StringBuffer的主要性能區別:String是不可變的對象, 因此在每次對 String 類型進行改變的時候,都會生成一個新的 String 對象,然後將指針指向新的 String 對象,所以經常改變內容的字符串最好不要用 String ,因?每次生成對象都會對系統性能產生影響,特別當內存中無引用對象多了以後, JVM 的 GC 就會開始工作,性能就會降低。
使用 StringBuffer 類時,每次都會對 StringBuffer 對象本身進行操作,而不是生成新的對象並改變對象引用。所以多數情況下推薦使用 StringBuffer ,特別是字符串對象經常改變的情況下。
在某些特別情況下, String 對象的字符串拼接其實是被 JVM 解釋成了 StringBuffer 對象的拼接,所以這些時候 String 對象的速度並不會比 StringBuffer 對象慢,例如:
| String S1 = “This is only a” + “ simple” + “ test”; StringBuffer Sb = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”); |
生成 String S1 對象的速度並不比 StringBuffer慢。其實在 JVM 裏,自動做了如下轉換:
String S1 = “This is only a” + “ simple” + “test”;
JVM直接把上述語句當作:
String S1 = “This is only a simple test”;
所以速度很快。但要注意的是,如果拼接的字符串來自另外的String對象的話,JVM就不會自動轉換了,速度也就沒那麼快了,例如:
String S2 = “This is only a”;
String S3 = “ simple”;
String S4 = “ test”;
String S1 = S2 +S3 + S4;
這時候,JVM 會規規矩矩的按照原來的方式去做。
在大部分情況下,StringBuffer > String。
5 使用策略
(1)基本原則:如果要操作少量的數據,用String ;單線程操作大量數據,用StringBuilder ;多線程操作大量數據,用StringBuffer。
(2)不要使用String類的"+"來進行頻繁的拼接,因?那?的性能極差的,應該使用StringBuffer或StringBuilder類,這在Java的優化上是一條比較重要的原則。舉個例子:在使用String 的時候,拼接字符串時使用“+”在JVM上形成臨時的StringBuffer對象,同時在每一個字符串上都建立一個對象,拼接了兩個字符串一共需要創建4個對象!(一個保存結果的String,兩個字符串對象,一個臨時的StringBuffer對象)。而使用StringBuffer的話,只需創建2個對象!一個StringBuffer對象和保存最後結果的的String對象 。
(3)?了獲得更好的性能,在構造 StirngBuffer 或 StirngBuilder 時應盡可能指定它們的容量。當然,如果你操作的字符串長度不超過 16 個字符就不用了。不指定容量會顯著降低性能。
(4)StringBuilder一般使用在方法內部來完成類似"+"功能,因?是線程不安全的,所以用完以後可以丟棄。StringBuffer主要用在全局變量中。
(5)相同情況下使用 StirngBuilder 相比使用 StringBuffer 僅能獲得 10%~15% 左右的性能提升,但卻要冒多線程不安全的風險。而在現實的模塊化編程中,負責某一模塊的程序員不一定能清晰地判斷該模塊是否會放入多線程的環境中運行,因此:除非確定系統的瓶頸是在 StringBuffer 上,並且確定你的模塊不會運行在多線程模式下,才可以采用StringBuilder;否則還是用StringBuffer。
資料來源:http://rritw.com/a/JAVAbiancheng/JAVAzonghe/20130702/377964.html
Java 取得WEB-INF/classes 的實體路徑位置
一般來說在Servlet中就可以取得WEB-INF的位置
但是我的需求是要在init DB connection時,就要取得實體路徑去抓取相關的檔案
所以就能用別的手法了
程式如下:
ClassLoader cl = this.getClass().getClassLoader();
String path = cl.getResource("/").getPath();
System.out.println("this path:" + path);
印出來的路徑如下:
this path:/D:/eclipse/workspace/CBCAdmin/javagip/WEB-INF/classes/
但是我的需求是要在init DB connection時,就要取得實體路徑去抓取相關的檔案
所以就能用別的手法了
程式如下:
ClassLoader cl = this.getClass().getClassLoader();
String path = cl.getResource("/").getPath();
System.out.println("this path:" + path);
印出來的路徑如下:
this path:/D:/eclipse/workspace/CBCAdmin/javagip/WEB-INF/classes/
Jacob使用
Jacob(Java-Com Bridge)
簡單來說,就是用Java呼叫COM元件的API
Jar檔下載位置:http://sourceforge.net/projects/jacob-project/
下方的範例程式:https://github.com/sabaao/sabaao/commit/d84a8df15fcb15e3106de9929aa9291592b986d8
而在使用時注意dll跟jar檔一定要是相同的版本,不然可能會出錯
以下為使用步驟(我使用的版本是1.14.3):
Step1:將jacob-1.14.3-x64.dll及jacob-1.14.3-x86.dll放置於C:\Windows\System32\下
Step2:注冊你要使用的元件(檔案路徑依情況作修改)
Gacutil.exe及Regsvcs.exe可以下列位置取得:
簡單來說,就是用Java呼叫COM元件的API
Jar檔下載位置:http://sourceforge.net/projects/jacob-project/
下方的範例程式:https://github.com/sabaao/sabaao/commit/d84a8df15fcb15e3106de9929aa9291592b986d8
而在使用時注意dll跟jar檔一定要是相同的版本,不然可能會出錯
以下為使用步驟(我使用的版本是1.14.3):
Step1:將jacob-1.14.3-x64.dll及jacob-1.14.3-x86.dll放置於C:\Windows\System32\下
Step2:注冊你要使用的元件(檔案路徑依情況作修改)
REGSVCS
D:\ConnStrManage\ConnStrForJava\ComSecureCnnStr.dll
GACUTIL /i
D:\ConnStrManage\ConnStrForJava\ComSecureCnnStr.dll
Gacutil.exe及Regsvcs.exe可以下列位置取得:
Gacutil.exe=>C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\Bin\x64
Regsvcs.exe=>C:\Windows\Microsoft.NET\Framework\v2.0.50727
Step3:於專案中加入jacob.jar參考
Util如下:
package tw.cbc.jacob;
import com.jacob.com.Dispatch;
public class Jacob {
private String dispatchName;
private String methodName;
private Object[] params;
private Dispatch dispatch;
public Jacob(String dispatchName){
this.dispatchName = dispatchName;
dispatch = new Dispatch(this.dispatchName);
}
public Jacob(String dispatchName,String methodName,Object[] params){
this.dispatchName = dispatchName;
this.methodName = methodName;
this.dispatch = new Dispatch(this.dispatchName);
this.params = params;
}
public String getDispatchName() {
return dispatchName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Dispatch getDispatch() {
return dispatch;
}
public Object call(){
Object obj = null;
if(params==null){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==0){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==1){
obj = Dispatch.call(this.dispatch,this.methodName,params[0]);
}else if(params.length==2){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1]);
}else if(params.length==3){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2]);
}else if(params.length==4){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3]);
}else if(params.length==5){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4]);
}else if(params.length==6){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5]);
}else if(params.length==7){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6]);
}else if(params.length==8){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6],params[7]);
}
return obj;
}
}
呼叫範例:
Jacob jacob = new Jacob("SecureCnnStr");
jacob.setMethodName("EntSecurCnnStr");
jacob.setParams(new Object[]{"SOD","D:\\CBC\\encode\\","IBPS1455"});
jacob.call();
jacob.setMethodName("GetConnStr");
jacob.setParams(null);
String result = jacob.call().toString();
import com.jacob.com.Dispatch;
public class Jacob {
private String dispatchName;
private String methodName;
private Object[] params;
private Dispatch dispatch;
public Jacob(String dispatchName){
this.dispatchName = dispatchName;
dispatch = new Dispatch(this.dispatchName);
}
public Jacob(String dispatchName,String methodName,Object[] params){
this.dispatchName = dispatchName;
this.methodName = methodName;
this.dispatch = new Dispatch(this.dispatchName);
this.params = params;
}
public String getDispatchName() {
return dispatchName;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Object[] getParams() {
return params;
}
public void setParams(Object[] params) {
this.params = params;
}
public Dispatch getDispatch() {
return dispatch;
}
public Object call(){
Object obj = null;
if(params==null){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==0){
obj = Dispatch.call(this.dispatch,this.methodName);
}else if(params.length==1){
obj = Dispatch.call(this.dispatch,this.methodName,params[0]);
}else if(params.length==2){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1]);
}else if(params.length==3){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2]);
}else if(params.length==4){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3]);
}else if(params.length==5){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4]);
}else if(params.length==6){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5]);
}else if(params.length==7){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6]);
}else if(params.length==8){
obj = Dispatch.call(this.dispatch,this.methodName,params[0],params[1],params[2],params[3],params[4],params[5],params[6],params[7]);
}
return obj;
}
}
呼叫範例:
Jacob jacob = new Jacob("SecureCnnStr");
jacob.setMethodName("EntSecurCnnStr");
jacob.setParams(new Object[]{"SOD","D:\\CBC\\encode\\","IBPS1455"});
jacob.call();
jacob.setMethodName("GetConnStr");
jacob.setParams(null);
String result = jacob.call().toString();
2013年9月30日 星期一
XSL replace功能
今天因為要處理專案的bug,所以又摸了許久不見的xsl
結果才發現xsl沒有replace的function,原本以為用translate就可以搞定
結果用法整個不太一樣…冏rz
後來google到別人寫好的replace function,就來趕快記錄一下
結果才發現xsl沒有replace的function,原本以為用translate就可以搞定
結果用法整個不太一樣…冏rz
後來google到別人寫好的replace function,就來趕快記錄一下
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
call template example:<xsl:call-template name="replace-string"> <xsl:with-param name="text" select="'aa::bb::cc'"/> <xsl:with-param name="replace" select="'::'" /> <xsl:with-param name="with" select="','"/> </xsl:call-template>
訂閱:
文章 (Atom)
















