⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webutil.java

📁 又一个课程设计 简易 JSP 论坛 功能较简单的那种, 界面上模仿了 Discuz JSP 本来就学的不行, 只是尽量实现了 MVC
💻 JAVA
字号:
package cn.ialvin.web;
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.net.URI;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 提供一些常用的方法,均为静态方法.
 * <br /><br />(#)WebUtil.java	 2008-3-18 下午06:05:18<br />
 * @author Alvin(<b>ialvin.cn</b>) 广东 普宁 里湖
 */
public class WebUtil {
	public static String getParameter(HttpServletRequest request, String key) {
		if (key == null) return "";
		String value = request.getParameter(key);
		if (value == null) return "";
		return value;
	}
	
	/**
	 * 生成验证码图片
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void getCode(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		getCode(request, response, "VCode");
	}
	public static void getCode(HttpServletRequest request, HttpServletResponse response,
			String key)
			throws ServletException, IOException {
		getCode(request, response, "VCode", 80, 20);
	}
	public static void getCode(HttpServletRequest request, HttpServletResponse response,
			String key, int height, int width)
			throws ServletException, IOException {
		getCode(request, response, "VCode", width, height, 4);
	}
	public static void getCode(HttpServletRequest request, HttpServletResponse response,
			String key, int height, int width, int length)
			throws ServletException, IOException {
		response.setContentType("image/JPEG");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		OutputStream out = response.getOutputStream();
		if (width < 2) width = 80;
		if (height < 2) height = 20;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(new Color(0xF7FBFF));
		g.fillRect(0, 0, width, height);
		Random random = new Random();
		g.setFont(new Font("Times New Roman", Font.ITALIC, 18));
		String s = "";
		for (int i=0; i<length; i++) {
			s += random.nextInt(10) + "";
		}
		g.setColor(new Color(20+random.nextInt(110), 20+random.nextInt(110), 20+random.nextInt(110)));
		g.drawString(s, (width/2) - g.getFontMetrics().stringWidth(s)/2, 16);
		g.dispose();

		HttpSession session = request.getSession(true);
		session.setAttribute(key, s);
		ImageIO.write(image, "JPEG", out);
		out.flush();
		out.close();
	}
	
	public static String toJS(String s) {
		if (s == null) return "";
		return "unescape(\"" + Encoder.escape(s) + "\")";
	}

	public static void main(String[] args) {
		//System.out.println(sun.io.Converters.getDefaultEncodingName());
		System.out.println(System.getProperty("file.encoding"));
		System.out.println(System.getProperty("os.name"));
		System.out.println(System.getProperty("os.arch"));
		System.out.println(System.getProperty("os.version"));
		System.out.println(System.getProperty("user.dir"));
		System.out.println(System.getProperty("user.home"));
		System.out.println(System.getProperty("user.name"));
		System.out.println(System.getProperty("user.region"));
		System.out.println(System.getProperty("microedition.io.file.FileConnection.version"));
		System.out.println(URI.create("http://www.ialvin.cn/blog/default.asp").getHost());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -