当前位置: 技术问答>java相关
高手何在????????:Servlet高手急救:欲从servlet输出图形,请问linux环境下如何配置,web服务器为tomcat
来源: 互联网 发布时间:2015-02-06
本文导语: 注意:服务器为linux 代码为: import java.io.*; import java.util.*; import com.sun.image.codec.jpeg.*; import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import java.awt.image.*; public class JPEGServlet extends HttpServlet {...
注意:服务器为linux
代码为:
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
public class JPEGServlet extends HttpServlet {
private Font mFont=new Font("隶书", Font.BOLD,32);
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("image/jpeg");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(160,64,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,160,64);
g.setColor(Color.green);
g.setFont(mFont);
g.drawString("中国",10,60);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
}
执行时报以下错误:
Internal Servlet Error:
java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java, Compiled Code)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java, Compiled Code)
at java.awt.Font.initializeFont(Font.java, Compiled Code)
at java.awt.Font.(Font.java, Compiled Code)
at JPEGServlet.(JPEGServlet.java, Compiled Code)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.Handler.service(Handler.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java, Compiled Code)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java, Compiled Code)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java, Compiled Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java, Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java, Compiled Code)
at java.lang.Thread.run(Thread.java, Compiled Code)
代码为:
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
public class JPEGServlet extends HttpServlet {
private Font mFont=new Font("隶书", Font.BOLD,32);
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("image/jpeg");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(160,64,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,160,64);
g.setColor(Color.green);
g.setFont(mFont);
g.drawString("中国",10,60);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
}
执行时报以下错误:
Internal Servlet Error:
java.lang.NoClassDefFoundError: sun/awt/X11GraphicsEnvironment
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java, Compiled Code)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java, Compiled Code)
at java.awt.Font.initializeFont(Font.java, Compiled Code)
at java.awt.Font.(Font.java, Compiled Code)
at JPEGServlet.(JPEGServlet.java, Compiled Code)
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.Handler.service(Handler.java, Compiled Code)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java, Compiled Code)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java, Compiled Code)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java, Compiled Code)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java, Compiled Code)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java, Compiled Code)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java, Compiled Code)
at java.lang.Thread.run(Thread.java, Compiled Code)
|
不好意思,今天我一直上不了CSDN,总是报数据库连接超时!:(
你说的这个错误说明java没能找到本地图形环境。
你是否安装了图形环境,是否在图形环境下运行tomcat?
我也试一下吧。下去换个环境看一下,不知等会能不能再进来。