PIXNET Logo登入

Jemmy Walker

跳到主文

部落格全站分類:不設分類

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 1月 06 週四 201117:16
  • JDOM解析XML字串(非檔案)

痞客邦的第二篇,還不太熟怎麼用。
進行POC過程發現如果XML不是檔案,而是字串,JDOM如何去parse? 這以前晃兄有教過一次。
StringReader sr = new StringReader(xmlString);
Document doc = builder.build(sr);
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(434)

  • 個人分類:Java
▲top
  • 11月 13 週六 201022:05
  • JVM 64bit版Run Eclipse彈出錯誤視窗之解

  在Windows 7 64bit跑Eclipse,用SVN提交或更新專案時,老彈出如下落落長視窗:

Failed to load JavaHL Library.
These are the errors that were encountered:
no libapr-1 in java.library.path
no libapriconv-1 in java.library.path
D:\Soft\Ruby\bin\libeay32.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
D:\Soft\Ruby\bin\ssleay32.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
no libaprutil-1 in java.library.path
no libsasl in java.library.path
no libsvn_subr-1 in java.library.path
no libsvn_delta-1 in java.library.path
no libsvn_diff-1 in java.library.path
no libsvn_wc-1 in java.library.path
no libsvn_fs-1 in java.library.path
no libsvn_repos-1 in java.library.path
no libsvn_ra-1 in java.library.path
no libsvn_client-1 in java.library.path
no libsvnjavahl-1 in java.library.path
no svnjavahl-1 in java.library.path
no svnjavahl in java.library.path
java.library.path = …

  也不知為何和Ruby有關,經Google指示,看來是JVM 64bit的問題,下載安裝64bit版的SVN Client就行了。參照http://www.sliksvn.com/en/download
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(663)

  • 個人分類:Java
▲top
  • 7月 22 週四 201019:05
  • JavaMail再體驗

  今天狀況還真不少,也更了解JavaMail,曾因發Mail中文出現亂碼,經由客戶提供sample,從純文字改以HTML方式發送,果然OK。

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");

Session session = Session.getDefaultInstance(props);
session.setDebug(this.debug);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.from));

msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.recipients));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(this.copyTos));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(this.blindCopyTo));
msg.setSubject(mailSubject,"UTF-8"); // 主旨轉UTF-8

Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(this.mailContent, "text/html;charset=UTF-8");
mp.addBodyPart(mbp);

msg.setContent(mp);
msg.setContent(this.mailContent, "text/html;charset=UTF-8");
msg.setText(this.mailContent);
msg.setSentDate(new Date());
Transport.send(msg);

  從上面看到,mailContent被設置了三次:MimebodyPart的setText、MimeMessage的setContent和MimeMessage的setText。後來試出上述灰色字根本都不需要。只需setContent指定html的charset編碼,就不會出現亂碼。   而Transport.send為何也被灰色掉。透過Internet寄出可以,可是要發給Lotus Notes的Users就有問題了,原因不知為何就是被擋,還是循回老方法,透過Session取得Transport來發送就可以work:

Transport transport = session.getTransport("smtp");
transport.connect(this.host, "", ""); // this.username, this.password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(1,837)

  • 個人分類:Java
▲top
  • 7月 10 週六 201023:22
  • JavaMail初體驗

  寫了那麼久的Java,現在才有機會寫JavaMail。之前存在一個誤解是要架設Mail Server,不過修正的是OS都有自己內建的Mail Server,也許沒有產品化的功能強大。   JavaMail目前最新版是1.4.3,在Maven放的是1.4.1,還需引入activation,作用先不深究。pom.xml設定如下:

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.1</version>
</dependency>

  程式寫來也很簡單,片段如下:

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props);
session.setDebug(this.debug);           // boolean
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(this.from));    // 寄件者
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.recipients));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(this.copyTos));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(this.blindCopyTo));
msg.setSubject(this.mailSubject);        // 標題
msg.setText(this.mailContext);           // 內容
msg.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
transport.connect(this.host, "", ""); // this.username, this.password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

  大多setter屬性都是字串型態,其中收件者(Recipients)、CC和BCC若是對象有多人,和寄Mail一樣用逗號分隔mail address。   寫JavaMail確實簡單,但也遇上了編碼瓶頸待解。發到客戶的Linux端的話,把訊息內容從Big5轉ISO-8859-1是可以正常顯示中文的,簡單轉換如下: new String(str.getBytes("Big5"), "ISO-8859-1")   可是發到AIX或是Lotus Notes的Domino Server的SMTP下,就會變亂碼…,難搞。發不出心中的Mail。
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(521)

  • 個人分類:Java
▲top
  • 7月 01 週四 201017:48
  • JDOM: Element轉成XML String

  討厭的JDOM,語法習慣都和一般的Java libraries不太一樣,當然還沒像IBM的那麼奇怪。若想把單一的Element轉成XML String怎麼處理?壓根兒沒猜到JDOM把它當作Stream處理,所以沒有所謂的toXML、convert等method,要用XMLOutputer。轉字串很簡單的如下:

XMLOutputter outputter = new XMLOutputter();
String xml = outputter.outputString(element);

(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(292)

  • 個人分類:Java
▲top
  • 6月 30 週三 201018:58
  • JDOM issue: The Content already has an existing parent

  漫不經心的憂傷,踹到JDOM來。拿既有的Element當作一份新產出XML Document怎麼處理呢?假設Element值是<book>漫不經心</book>,若要以<book>為根元素,其寫法如下:

XMLOutputter outputter = new XMLOutputter();
fos = new FileOutputStream(filename);
Document doc = new Document(element.detach());
outputter.output(doc, fos);
fos.flush();
fos.close();

  而若想在<book>之上再有個<root>作為根元素,則寫法如下:

XMLOutputter outputter = new XMLOutputter();
fos = new FileOutputStream(filename);
Element root = new Element("root");
Document doc = new Document(root);
root.addContent(element.detach());
outputter.output(doc, fos);
fos.flush();
fos.close();

  沒辦法將Element物件當作內容被加到addContent,需用detach複製自己。否則常有以下的Exception: org.jdom.IllegalAddException: The Content already has an existing parent "root"
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(158)

  • 個人分類:Java
▲top
  • 5月 15 週六 201020:20
  • JSON-Lib初體驗

  任何一套Web Framework,都會遇到一對多,甚至再一對多或多對多的編輯畫面,雖然各有自己解決方式,但學習門檻、普遍適用性問題,都得考慮是否值得投資時間學更複雜的用法。而JSON一出,我倒覺得可以一分為二,單一的Table的增改刪查可以依賴Web Framework既有的解決方案,而複雜關聯性的增刪改查,可以統一使用JSON,也比XML省空間。   我還是用net.sf.json-lib套件,在前端Post(或Get)到後端的變數是JSON格式字串,它若不是物件(Object)就是陣列(Array),其用法區分如下: 型態 Java物件名 字串轉換成JSON 若為空JSON Array JSONArray JSONArray jsonArray = JSONArray.fromObject(str) [] Object JSONObject JSONObject jsonObject = JSONObject.fromObject(str) {}   fromObject這method相當於取得JSON的Root Node,若要從Root取得其子物件,method為get('key'),返回是一個物件,若想在取得時做型態轉換,尚有提供getString、getInt、getLong和getBoolean等;若是取得子陣列,則是getJSONArray,也是返回JSONArray物件。倘若JSONArray.fromObject(str)中的str開頭不是[開頭的會報錯。   fromObject的參數除了字串,也可以是JavaBean或List<JavaBean>或List<Map>,也就是JSON其實就是List+Map這兩個資料結構演化出來的。
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(1) 人氣(122)

  • 個人分類:Java
▲top
  • 4月 19 週一 201014:12
  • JSONObject無法new的Exception

  出現java.lang.NoSuchMethodError: org.apache.commons.collections.map.ListOrderedMap: method <init>()V not found。係JSON套件依賴於commons-collections。而JSON的pom.xml並未設定此依賴,是故需手動設定pom.xml如下:

<dependency>    <!-- 2008/4/15 -->
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>
<!-- JSON -->
<dependency>    <!-- 2009/7/11 -->
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.3</version>
    <classifier>jdk15</classifier>  <!-- 用於JDK 1.5 -->
</dependency>

(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(175)

  • 個人分類:Java
▲top
  • 4月 16 週五 201020:44
  • 泛型二三事

  Java泛型(Generic)功能強大易學難理解,易學是因為在一般程式開發時很好用,可是提昇到架構層次思維時,往往以為可以達成的工作卻不work,原因是不了解泛型的本身意涵與限制。   泛型作用是在於Runtime時才確認要work的Class,是故宣告個pulbic class Generic<T>,在用其它Class去call Generic時,才把T用其它Class取代,例:Generic<String> generic = new Generic<String>;。反過來說,call Generic的時候,還不確定T是什麼類別,或是Generic類別裡的程式又用到泛型,那應該不work的。舉例Spring的RowMapper例子如下,newInstance的參數是Class類別,直接傳泛型T不能compile:

public abstract class AbstractBaseDao<T> {
    // … 略
    protected RowMapper<T> getRowMapper() {
        return ParameterizedBeanPropertyRowMapper.newInstance(T); // Error
    }
}

  後來Google到一種特殊解法,但實在太詭異了,也沒去試,非到不得已才用之,以下是取得T的class的方式:

Class clazz = (Class) ((java.lang.reflect.ParameterizedType) 
    getClass().getGenericSuperclass() 
        ).getActualTypeArguments()[0];

估計0是第一個泛型,若有第二個則以此類推。而好像可以用java.lang.reflect.Type代替Class去接

  此外記錄一下static method使用泛型的用法,和一般method用法有差,也挺詭異的:

public static<T> T getT(Class<T> clazz) throws Exception {
    T obj = ac.getBean(clazz);
    return obj;
}

(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(683)

  • 個人分類:Java
▲top
  • 3月 29 週一 201010:29
  • Java call .NET Web Services

  兩年半前的Vibo案,Aqual Logic竟無法註冊.Net的WebServices,用Axis去call .net web services完全不通。近日受命研究連接某公司的.net web services,要用Java去call,結果在JBoss的官方文件竟找到Solution,出處在http://docs.jboss.org/jbossas/jboss4guide/r2/html/ch12.html,還是用javax.xml.rpc套件去call的,反而我還是無法使用Axis踹出來,不免有種滄海桑田之感。   某公司的web services的URL是https://…/Security.asmx。以asmx結尾是.Net手路之一,所以後面加?wsdl確定有它的存在。在該網頁檢視發現有個URL地位很重要:http://tempuri.org/,這麼解釋它:它是.Net WebServices參考的Namespace(命名空間)。之後找到JBoss附的source如下:

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Call;

import javax.xml.namespace.QName;

import java.net.URL;
public class WsClient {
    public static void main(String[] args) throws Exception {
        String urlstr   = "
https://…/Security.asmx?wsdl";  // 後面加?wsdl
        URL url =  new URL(urlstr);

        String ns        = "http://tempuri.org/";
        QName  qname     = new QName(ns, "Security");      // 查wsdl檔的<wsdl:service>的name屬性
        QName  port      = new QName(ns, "SecuritySoap");  //
查wsdl檔的<wsdl:portType>的name屬性 
        QName  operation = new QName(ns, "getConnection"); //
查wsdl檔的<wsdl:operation>的name屬性

        ServiceFactory factory = ServiceFactory.newInstance();
        Service        service = factory.createService(url, qname);
        Call           call    = service.createCall(port, operation);

        System.out.println("output:" + call.invoke(new Object[] {"UserID", "Password"}));
    }
}

  結果真的如該公司文件所寫,傳回XML String。其實傳XML String是比較不費工的作法,Web Services是可以傳回不少種類的型態,包括Collection,但要去接收就粉累人了。WSDL裡的service name相當於一個Jar、portType name相當於一個Interface、而operation name則相當於interface裡的method。   後來找出用Axis的call法,才發現Axis也是要照上面方式寫,Axis的Service、Call類別其實都自javax.xml.rpc套件演變而來,卻沒辦法用Axis標準寫法,先宣告Service和Call再去set一些屬性,而是和上面一樣全用建構子傳參數。如下:

import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class DotNetWebService {
    public static void main(String[] args) throws Exception { 
        String method = "getConnection";
        Service service = new Service(
                new URL("
https://…/Security.asmx?wsdl"),
                new QName("
http://tempuri.org/", "Security"));
        String ns        = "
http://tempuri.org/";
        QName  port      = new QName(ns, "SecuritySoap");
        QName  operation = new QName(ns, "getConnection");
        Call call = (Call) service.createCall(port, operation); 
        String ret = (String)call.invoke(new String[]{
{"UserID", "Password" });
        System.out.println(ret);
    } 
}

  寫到此處,忽然很想用力的丟滑鼠,這不是早該兩年前就該踹出來的嗎?EBS不給時間研發,再來批評軟體開發中心技術水平差。全面採取保守不冒風險的策略,其實就是置專案於最大的風險,而和Vibo全面對幹,卻又是採取太過激進、甘冒大不諱的策略。雖然EBS裡主導這種爛策略的龜sons都被fire,但其性若不改,其作為適足讓台灣資訊業蒙羞矣!
(繼續閱讀...)
文章標籤

Jemmy 發表在 痞客邦 留言(0) 人氣(2,481)

  • 個人分類:Java
▲top
12...6»

自訂側欄

自訂側欄

個人資訊

Jemmy
暱稱:
Jemmy
分類:
不設分類
好友:
累積中
地區:

熱門文章

  • (18,363)Windows route初體驗
  • (2,912)Java在換行字元的issue
  • (1,417)非我族類的DataTables.js:分頁和排序無作用原因
  • (1,319)Oracle的Array型態定義與存取
  • (789)Maven排除特定class來mvn package
  • (776)OGG for Java開發
  • (578)Struts-Menu初體驗
  • (436)ORA-08002
  • (73)Notes的Agent二三事
  • (33)六句聯

文章分類

  • PostgreSQL (1)
  • Ruby (6)
  • 圖書 (18)
  • 旅行 (8)
  • OSGi (13)
  • 健康 (9)
  • Google (6)
  • 歷史 (5)
  • Spring-Security (5)
  • Java Script (11)
  • Log (8)
  • 娛樂 (25)
  • IT趨勢 (12)
  • 心情 (92)
  • 組織 (41)
  • Java基本功 (18)
  • Regex (19)
  • 新聞與政治 (46)
  • 電腦和網際網路 (47)
  • Maven (28)
  • Spring (31)
  • LotusNotes (12)
  • Java (60)
  • Oracle (17)
  • Struts (21)
  • jQuery (10)
  • 未分類文章 (1)

最新文章

  • JDOM解析XML字串(非檔案)
  • Android SDK不能用於JDK 64bit
  • 非我族類的DataTables.js:分頁和排序無作用原因
  • java.lang.IllegalStateException: Committed之解
  • PostgreSQL二三事
  • Form/Field V.S. Document/Item
  • Spring Mail
  • Ant generate manifest.mf的class-path清單
  • dhcp.bat
  • Java update Notes的DateTime欄位

最新留言

  • [21/07/31] D 於文章「複習一下Servlet的機制...」留言:
    請問更換瀏覽器 Servlet的instance vari...
  • [17/05/04] 訪客 於文章「設定Source編碼...」留言:
    謝謝你~~...
  • [15/08/23] 洪秀柱 於文章「UltraEdit轉大小寫...」留言:
    此破解法可用於 UEstudio UltraEdit V2...
  • [10/10/15] Jemmy 於文章「Perl的跨行比對...」留言:
    哈! 學長, 好久不見了。幸虧這案子沒有Nested Tag...
  • [10/10/14] Benson 於文章「Perl的跨行比對...」留言:
    如果有Nested Tag, 這就不太適用; 我有時還是乖乖...
  • [10/05/25] 世文 於文章「JSON-Lib初體驗...」留言:
    用心經營的blog~^^ 加油!<br />---<br ...
  • [10/05/15] Jemmy 於文章「千分位...」留言:
    你的解法太高深了, 有空再練^^...
  • [10/05/14] Benson 於文章「千分位...」留言:
    可能是 gxe, 不管是 perl or java, 你懂我...
  • [10/05/14] Jemmy 於文章「千分位...」留言:
    練功一下而已, 而且我是要用Java,所以無法用Perl...
  • [10/05/14] Benson 於文章「千分位...」留言:
    學弟,你的問題是什麼呢?是把在引號內數字逗號去掉嗎?s/\"...

動態訂閱

文章精選

文章搜尋

誰來我家

參觀人氣

  • 本日人氣:
  • 累積人氣: