轉載:http://superich2008.iteye.com/blog/2047830
失敗提示信息為:程序包com.sun.image.codec.jpeg不存在
這個類文件的位置在jre/lib/rt.jar
而我們設置的java_home下面的lib/dt.jar中沒有這個文件,導致編譯失敗。通過配置maven-compiler-plugin插件可以解決此問題。
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<optimize>true</optimize>
<debug>true</debug>
<showDeprecation>true</showDeprecation>
<showWarnings>false</showWarnings>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
在windows下面用;分隔,linux下面用:分隔。
方案-:
查詢網上的解決方案,但是仍然報編譯失敗。后經過查找,最終定位問題。
原因是由於編譯的依賴JDK版本過高引起的。從JDK1.7開始,中com.sun.image.codec.jpeg這個類被刪除,所以編譯總是報錯,解決方案,編譯的JDK版本設置為JDK1.6或者以下版本,編譯通過。
方案二:
解決代碼API引用問題。
原始代碼:
ByteArrayOutputStream out = null; byte[] b = null; try { BufferedImage bi = ImageIO.read(is); Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH); BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB); thumbnail.getGraphics().drawImage(Itemp, 0, 0, null); out = new ByteArrayOutputStream(); // 繪圖 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail); param.setQuality(1.0f, false); encoder.encode(thumbnail); out.flush(); b = out.toByteArray(); out.close(); bi.flush(); bi = null; } catch (IOException e) { logger.error(Util.stackTrace2String(e)); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
改變實現方式:
ByteArrayOutputStream out = null; byte[] b = null; try { BufferedImage bi = ImageIO.read(is); // Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH); // BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB); // thumbnail.getGraphics().drawImage(Itemp, 0, 0, null); out = new ByteArrayOutputStream(); // 繪圖 // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); // JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail); // param.setQuality(1.0f, false); // encoder.encode(thumbnail); ImageIO.write(bi, "jpg", out); out.flush(); b = out.toByteArray(); out.close(); bi.flush(); bi = null; } catch (IOException e) { logger.error(Util.stackTrace2String(e)); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
即可解決報錯問題。
