📄 imageservlet.java
字号:
package com.gctech.sms.voice.common;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageServlet
extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init()
throws ServletException
{
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
HttpSession currentSession = request.getSession(false);
if(currentSession == null)
currentSession = request.getSession(true);
String CONTENT_TYPE = "image/jpeg;charset=GB2312";
response.setContentType(CONTENT_TYPE);
String text = ""; //要嵌的文字
String imageFile = ""; //被嵌的图片的虚拟路径
int x = 0; //坐标
int y = 0;
String fontColor = ""; //字体颜色
int fontSize = 0; //字体大小
String fontStyle = ""; //字体风格(斜体,粗体等)
String fontName = ""; //字体名称
try
{
int rand = (int)(Math.random() * 100000);
if(rand < 10)
text = "00000" + rand;
else if(rand < 100)
text = "0000" + rand;
else if(rand < 1000)
text = "000" + rand;
else if(rand < 10000)
text = "00" + rand;
else
text = "" + rand;
imageFile = request.getParameter("templete");
x = 2;
y = 15;
fontColor = "0000FF";
fontSize = 14;
fontStyle = "plain";
fontName = "arial";
}
catch(Exception e)
{
e.printStackTrace();
}
ServletOutputStream output = response.getOutputStream();
if(imageFile.toLowerCase().endsWith(".jpeg")
|| imageFile.toLowerCase().endsWith(".jpg"))
{
InputStream imageIn = this.getServletContext().getResourceAsStream(imageFile);
// System.out.println("the real imageFile is " + imageFile);
// System.out.println("the new version");
// InputStream imageIn = new FileInputStream(new
// File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g = image.getGraphics();
//设置颜色
g.setColor(new Color(Integer.parseInt(fontColor,16)));
//设置字体
Font mFont = new Font(fontName,Font.PLAIN,fontSize); //默认字体
if(fontStyle.equalsIgnoreCase("italic"))
mFont = new Font(fontName,Font.ITALIC,fontSize);
if(fontStyle.equalsIgnoreCase("bold"))
mFont = new Font(fontName,Font.BOLD,fontSize);
if(fontStyle.equalsIgnoreCase("plain"))
mFont = new Font(fontName,Font.PLAIN,fontSize);
g.setFont(mFont);
//输出文字
g.drawString(text,x,y);
//输出数据流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);
imageIn.close();
}
//当前生成的时间
currentSession.setAttribute("verifyPwdTime",new Long(System.currentTimeMillis()));
currentSession.setAttribute("verifyPwd",text);
output.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
doGet(request,response);
}
//Clean up resources
public void destroy()
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -