扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在本页阅读全文(共19页)
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 String strFile="c:\\count.txt"; private Font mFont=new Font("Courier", Font.BOLD,12);; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { String s; int intCount; s=getCount(); intCount=new Integer(s).intValue(); intCount=intCount+1; writeCount(intCount+""); response.setContentType("image/jpeg"); ServletOutputStream out=response.getOutputStream(); BufferedImage image=new BufferedImage(110,16,BufferedImage.TYPE_INT_RGB); Graphics g=image.getGraphics(); g.setColor(Color.black); g.fillRect(0,0,110,16); g.setColor(Color.white); g.setFont(mFont); char c; for(int i=0;i<10;i++) { c=s.charAt(i); g.drawString(c+"",i*11+1,12); g.drawLine((i+1)*11-1,0,(i+1)*11-1,16); } JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); } public synchronized void writeCount(String content) { try { PrintWriter pw = new PrintWriter(new FileOutputStream(strFile)); pw.println(content); pw.close(); } catch(IOException e) { System.out.println("Write file error:"+e.getMessage()); } } public synchronized String getCount() { String strCount; int len; int leftLen; try { BufferedReader inf = new BufferedReader(new FileReader(strFile)); strCount=inf.readLine(); inf.close(); } catch(Exception e) { System.out.println("Get counter error:"+e.getMessage()); return reChar('0',10); } len=strCount.length(); if (len<=0) { return reChar('0',10); } if (len>10) { return reChar('9',10); } leftLen=10-len; return reChar('0',leftLen)+strCount; } private String reChar(char c,int num){ int i; if (num<=0) return ""; String s=""; for(i=0;i<num;i++) { s=s+c; } return s; } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者