天天看點

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

友情提示:後面在2014 7 29 日有增加一個新的解決方法和問題原因的說明 筆者在開發一個java ssh+mysql的項目過程中,本來在windows下開發測試,在myeclipse中的部署都沒有問題,但當想要上線測試到linux伺服器時,我預先做了移植測試,在Linux上,在啟動過程中,如果驗證碼圖檔是采用背景圖檔的,在windows下沒問題,但是在linux(centos5.5)上就會有問題:異常資訊如下: 1. 導緻的原因 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'net.shopxx.common.JCaptchaEngine#ee20ab' defined in file [/usr/local/apache-tomcat-6.0.32/webapps/shopx/WEB-INF/classes/applicationContext.xml]:  Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [net.shopxx.common.JCaptchaEngine]: Constructor threw exception; nested exception is com.octo.captcha.CaptchaException: Unknown error during file reading  Caused by: javax.imageio.IIOException: Not a JPEG file: starts with 0x42 0x4d         at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:603)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:341)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:475)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:596)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1042)         at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1022)         at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:89)         at com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator.getImage(FileReaderRandomBackgroundGenerator.java:201)         ... 75 more 2014-4-22 2:15:55 org.apache.catalina.core.StandardContext start Caused by:  java.awt.HeadlessException:  No X11 DISPLAY variable was set, but this program performed an operation which requires it. Caused by: com.octo.captcha.CaptchaException: Unknown error during file reading  2  問題分析 最關鍵部分就是這句錯誤,在讀檔案時發生未知錯誤,這個問題的因該是檔案系統路徑造成的, 我說一下如何解決的辦法: 在原來的JCaptchaEngine這個類中,路徑是這樣寫的,這個是測試shopxx,也有這個問題,我直接使用shopxx來說了,如果需要看shopxx的源碼直接發送郵件到([email protected]) private static final String IMAGE_PATH = "./net/shopxx/captcha/";// 随機背景圖檔路徑 你會在JCaptchaEngine類檔案中找到如下代碼,這句代碼的意思是,在擷取驗證碼時采用不同的背景圖檔,背景圖檔的位置放在IMAGE_PATH指定的目錄下。 BackgroundGenerator backgroundGenerator = new FileReaderRandomBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_PATH); 3  解決辦法 方法一:不使用原有的圖檔作為驗證碼的背景了,而是使用漸變色代替驗證碼的背景     在驗證碼擷取時修改成不要驗證碼背景,也就是把驗證碼背景顔色設定為純色,      替換上邊一行,代碼如下:       //背景顔色随機生成       // BackgroundGenerator backgroundGenerator = new GradientBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, Color.white,Color.white);  方法二:把private static final String IMAGE_PATH = "./net/shopxx/captcha/";// 随機背景圖檔路徑下的不是jpg格式的圖檔删除,shopxx中在圖檔驗證碼背景圖檔檔案夾下多加了一個captcha_bg_61.bmp的圖檔,我把該圖檔删了,一切正常。 4. 重新開機伺服器, 更新日期:2014-07-29 早9:24 5.更新該文檔後的總結,在第一次解決這個問題的時候,我自認為是驗證碼背景圖檔檔案夾路徑在window上和linux不一緻引起的,其實不然,報的錯誤有一個非常明顯得提示如下:         上面紅色的異常提示說不是一個jpeg檔案,我在查找我背景圖檔存放的目錄後,果然發現一個captcha_bg_61.bmp的圖檔檔案,很顯然,這個bmp的圖檔是window特有的檔案,這隻是可以解釋的原因之一。 6.從JCaptcha源碼解釋說明: <a href="http://opensourcejavaphp.net/java/jcaptcha/com/octo/captcha/component/image/backgroundgenerator/FileReaderRandomBackgroundGenerator.java.html">FileReaderRandomBackgroundGenerator.java</a>

從代碼中可以看出decoder是JPEGCodec的,其實這個也是我知道問題所在了,然後才這樣了解的,總之一句話,在你存放驗證碼背景圖檔的檔案夾src ./net/shopxx/captcha/ 下,隻要存放的都是真正jpg的檔案,真正你懂的,就是不要通過改字尾名過來的。而是你存儲的時候就是.jpg的,搞定收工。