当前位置: 技术问答>java相关
用servlet生成图片,在jsp叶面中显示。 出错!在线~~~
来源: 互联网 发布时间:2015-07-09
本文导语: 代码如下~: 也是在网上找到的,但不知道为什么不能正确生成jpeg图片 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.geom.*; import java...
代码如下~:
也是在网上找到的,但不知道为什么不能正确生成jpeg图片
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.geom.*;
import java.awt.image.*;
public class VoteImage extends HttpServlet {
private String strFile = null;
private Color color[]={Color.red,Color.black,Color.orange,Color.green};
private int baseAng = 30;
public void doGet(ServletContext context,HttpServletResponse response) throws ServletException,IOException {
strFile = context.getRealPath("count.txt");
float[][] xy = new float[4][2];
xy = getNumAndPercent(strFile);
int[] ang = new int[4];
ang[0] = (int)(xy[0][1]*360);
ang[1] = (int)(xy[1][1]*360);
ang[2] = (int)(xy[2][1]*360);
ang[3] = 360-ang[0]-ang[1]-ang[2];
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(150,100,BufferedImage.
TYPE_INT_RGB);
Graphics2D g=(Graphics2D)image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.white);
g.fillRect(0,0,150,100);
AffineTransform at = null;
Arc2D arc = null;
int fromAng = baseAng;
at = AffineTransform.getRotateInstance((-20*java.lang.Math.PI)/180,45,37);
g.setTransform(at);
int r =6;
int dx = (int)(r*java.lang.Math.cos((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
int dy = (int)(r*java.lang.Math.sin((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
arc = new Arc2D.Double(10+dx,24-dy,80,50,fromAng,ang[0],Arc2D.PIE);
g.setColor(color[0]);
g.fill(arc);
fromAng+=ang[0];
for (int i=1;i360){
fromAng-=360;
}
}
at = AffineTransform.getRotateInstance(0,arc.getCenterX(),arc.
getCenterY());
g.setTransform(at);
for (int i=0;i
也是在网上找到的,但不知道为什么不能正确生成jpeg图片
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.geom.*;
import java.awt.image.*;
public class VoteImage extends HttpServlet {
private String strFile = null;
private Color color[]={Color.red,Color.black,Color.orange,Color.green};
private int baseAng = 30;
public void doGet(ServletContext context,HttpServletResponse response) throws ServletException,IOException {
strFile = context.getRealPath("count.txt");
float[][] xy = new float[4][2];
xy = getNumAndPercent(strFile);
int[] ang = new int[4];
ang[0] = (int)(xy[0][1]*360);
ang[1] = (int)(xy[1][1]*360);
ang[2] = (int)(xy[2][1]*360);
ang[3] = 360-ang[0]-ang[1]-ang[2];
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(150,100,BufferedImage.
TYPE_INT_RGB);
Graphics2D g=(Graphics2D)image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.white);
g.fillRect(0,0,150,100);
AffineTransform at = null;
Arc2D arc = null;
int fromAng = baseAng;
at = AffineTransform.getRotateInstance((-20*java.lang.Math.PI)/180,45,37);
g.setTransform(at);
int r =6;
int dx = (int)(r*java.lang.Math.cos((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
int dy = (int)(r*java.lang.Math.sin((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
arc = new Arc2D.Double(10+dx,24-dy,80,50,fromAng,ang[0],Arc2D.PIE);
g.setColor(color[0]);
g.fill(arc);
fromAng+=ang[0];
for (int i=1;i360){
fromAng-=360;
}
}
at = AffineTransform.getRotateInstance(0,arc.getCenterX(),arc.
getCenterY());
g.setTransform(at);
for (int i=0;i