📄 imagesign.java
字号:
package test;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.ImageIO;
public class ImageSign extends HttpServlet {
ServletConfig config=null;
private String sign=""; //定义标记字符
public void init(ServletConfig config)throws ServletException
{
super.init(config);
this.config=config;
//获取自定义标记
sign=config.getInitParameter("signature");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("image/jpeg;charset=GBK");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
name=new String(name.getBytes("ISO-8859-1"));
String ima=request.getParameter("image");
try{
//获取当前文件路径
String path=request.getRealPath("/");
String fullpath=path+name+".jpg";
//建成立文件输出流
FileOutputStream ot = new FileOutputStream(fullpath);
// ServletOutputStream ot=response.getOutputStream();
//构造文件输入流
FileInputStream in=new FileInputStream(ima);
JPEGImageDecoder jpgCodec = JPEGCodec.createJPEGDecoder(in);
BufferedImage image = jpgCodec.decodeAsBufferedImage();
//构造绘图对象
Graphics g = image.getGraphics();
g.setColor(Color.red); //设置绘图颜色
//获取原图片宽和高
int width=image.getWidth();
int height=image.getHeight();
Font ff=new Font("Monotype Corsiva",Font.PLAIN,20);
g.setFont(ff);
//定义标记字符
//String signatur="caxton@126.com";
//将字符绘入指定图片位置(右下角)
g.drawString(sign,width-180,20);
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(ot);
//重新进行JPEG编码
encoder.encode(image);
in.close();
ot.close();
out.print("上传图片成功:<BR>");
out.print("图片存储路径为:"+path);
out.print("<BR><IMG src=\""+name+".jpg\"/>");
}
catch(Exception e)
{
out.print(e.toString());
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
public void destroy()
{
config=null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -