1.步骤:
修改$CATALINA_HOME/bin/catalina.sh文件($CATALINA_HOME是指tomcat安装目录),在原文件的40行新增如下内容即可
CATALINA_OPTS="-Djava.awt.headless=true"
存盘退出就完成. 很简单吧 :)))
2.测试:
2.1建立目录
cd /usr/local/tomcat/webapps/
mkdir –p TestImage/WEB-INF/classes
2.2编写index.jsp
在/usr/local/tomcat/webapps/ TestImage目录下建立index.jsp,内容如下:
<% @page import ="java.awt.*, java.awt.image.*, java.io.*, com.sun.image.codec.jpeg.*" % >
<%
BufferedImage bi = new BufferedImage (200, 100, BufferedImage .TYPE_INT_RGB);
Graphics g = bi.getGraphics ();
g.drawString ("test java image" , 0, 45);
FileOutputStream fos = new FileOutputStream (application.getRealPath ("/" ) + "test.jpg" );
JPEGImageEncoder jpegie = JPEGCodec .createJPEGEncoder (fos);
jpegie.encode (bi);
fos.close ();
%>
<img src ='test.jpg' >
2.3运行
启动tomcat,打开浏览器,输入:http://localhost:8080/TestImage