Java tutorial
/** * Copyright (c) 2005-2009 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: WebUtils.java 763 2009-12-27 18:36:21Z calvinxiu $ */ package com.klwork.common.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import java.util.StringTokenizer; import java.util.zip.GZIPOutputStream; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; /** * HttpServlet. * * @author calvin */ public class WebUtils { public static final long ONE_YEAR_SECONDS = 60 * 60 * 24 * 365; /** * Header. */ public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) { //Http 1.0 header response.setDateHeader("Expires", System.currentTimeMillis() + expiresSeconds * 1000); //Http 1.1 header response.setHeader("Cache-Control", "max-age=" + expiresSeconds); } /** * Header. */ public static void setNoCacheHeader(HttpServletResponse response) { //Http 1.0 header response.setDateHeader("Expires", 0); //Http 1.1 header response.setHeader("Cache-Control", "no-cache"); } /** * LastModified Header. */ public static void setLastModifiedHeader(HttpServletResponse response, long lastModifiedDate) { response.setDateHeader("Last-Modified", lastModifiedDate); } /** * Etag Header. */ public static void setEtag(HttpServletResponse response, String etag) { response.setHeader("ETag", etag); } /** * ??If-Modified-Since Header, ?. * * , checkIfModifyfalse ,304 not modify status. * * @param lastModified ?. */ public static boolean checkIfModifiedSince(HttpServletRequest request, HttpServletResponse response, long lastModified) { long ifModifiedSince = request.getDateHeader("If-Modified-Since"); if ((ifModifiedSince != -1) && (lastModified < ifModifiedSince + 1000)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return false; } return true; } /** * ?? If-None-Match Header, Etag?. * * Etag, checkIfNoneMatchfalse, 304 not modify status. * * @param etag ETag. */ public static boolean checkIfNoneMatchEtag(HttpServletRequest request, HttpServletResponse response, String etag) { String headerValue = request.getHeader("If-None-Match"); if (headerValue != null) { boolean conditionSatisfied = false; if (!"*".equals(headerValue)) { StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ","); while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) { String currentToken = commaTokenizer.nextToken(); if (currentToken.trim().equals(etag)) { conditionSatisfied = true; } } } else { conditionSatisfied = true; } if (conditionSatisfied) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); response.setHeader("ETag", etag); return false; } } return true; } /** * ???gzip?. */ public static boolean checkAccetptGzip(HttpServletRequest request) { //Http1.1 header String acceptEncoding = request.getHeader("Accept-Encoding"); if (StringUtils.contains(acceptEncoding, "gzip")) { return true; } else { return false; } } /** * Gzip HeaderGZIPOutputStream. */ public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException { response.setHeader("Content-Encoding", "gzip"); response.setHeader("Vary", "Accept-Encoding"); return new GZIPOutputStream(response.getOutputStream()); } /** * ??Header. * * @param fileName ???. */ public static void setDownloadableHeader(HttpServletResponse response, String fileName) { response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); } /** * ??? * @param request * @param response */ public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ? Graphics g = image.getGraphics(); // ?? Random random = new Random(); // g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // ?155????? g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789"; // ????(4?) String sRand = ""; for (int i = 0; i < 4; i++) { int start = random.nextInt(base.length()); String rand = base.substring(start, start + 1); sRand = sRand.concat(rand); // ?? g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 13 * i + 6, 16); } // ??SESSION request.getSession().setAttribute("entrymrand", sRand); // g.dispose(); OutputStream out = response.getOutputStream(); // ? ImageIO.write(image, "JPEG", out); out.flush(); out.close(); } /** * ? * @param fc * @param bc * @return */ public static Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }