今天在嘗試Run一個書上的一段Servlet教學範例時,發現無法執行

往前翻閱他的編譯記錄,發現到系統提示了 package com.sun.image.codec.jpeg does not exist 這個訊息

但在編寫程式碼時NetBeans並未報錯?!  好吧...我們來餵狗 Google一下

 

這篇文章有提到,以sun為開頭的package為sun公司的私有實現,並不是public可供使用的API

同樣地Oracle也寫了一篇 Why Developers Should Not Write Programs That Call 'sun' Packages 的文章

來提醒開發人員避免使用sun開頭的package,裡面提到:

「The sun.* packages are not part of the supported, public interface.
A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not
guaranteed to work even in future versions on the same platform.」

 

因此,當遇到這樣的狀況請改成用 Import Java原生的ImageIO類別來做處理:

EX:

若原先的程式碼片段為 

        ServletOutputStream out = response.getOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(bi);

請將其改成下面這種寫法

        ServletOutputStream out = response.getOutputStream();
        //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        //encoder.encode(bi);
        ImageIO.write(bi, "JPEG", out);

--

Note:

1. Stackoverflow那篇文章底下有一名網友提到ImageIO的讀取速度會很慢,有時間再來研究一下這部份@@"

 「Yes, unfortunately, reading a JPEG with ImageIO is much slower – Maurice Perry Apr 3 '12 at 12:13
--

參考資料來源:

1. http://stackoverflow.com/questions/1906673/import-com-sun-image-codec-jpeg

2. http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

3. http://tjmljw.iteye.com/blog/1939973

arrow
arrow
    文章標籤
    JAVA
    全站熱搜
    創作者介紹
    創作者 allen0818 的頭像
    allen0818

    allen0818 的部落格

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