標籤:ring pac import detail 索引 取圖 rgs 技術 利用
本文轉自:http://blog.csdn.net/u011637069/article/details/51112187
SpringMVC中controller通過返回ModelAndView然後通過ViewResolver找到相應的視圖。可以返回jsp可以返回Map等。
在做驗證碼圖片的時候如何處理讓我作難,使用struts2的時候在Action中返回位元組流,然後在struts.xml檔案中配置result的type為stream即可。但在SpringMVC中如何處理呢?
下面我將以代碼的方式講解一下如何在SpringMVC中產生驗證碼圖片:
首先,驗證碼圖片產生工具類:
ImageUtil.java
package org.qxl.onlinexam.util;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;public final class ImageUtil { // 驗證碼字元集 private static final char[] chars = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘}; // 字元數量 private static final int SIZE = 4; // 幹擾線數量 private static final int LINES = 5; // 寬度 private static final int WIDTH = 80; // 高度 private static final int HEIGHT = 40; // 字型大小 private static final int FONT_SIZE = 30; /** * 產生隨機驗證碼及圖片 * Object[0]:驗證碼字串; * Object[1]:驗證碼圖片。 */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1.建立空白圖片 BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 2.擷取圖片畫筆 Graphics graphic = image.getGraphics(); // 3.設定畫筆顏色 graphic.setColor(Color.LIGHT_GRAY); // 4.繪製矩形背景 graphic.fillRect(0, 0, WIDTH, HEIGHT); // 5.畫隨機字元 Random ran = new Random(); for (int i = 0; i <SIZE; i++) { // 取隨機字元索引 int n = ran.nextInt(chars.length); // 設定隨機顏色 graphic.setColor(getRandomColor()); // 設定字型大小 graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // 畫字元 graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3); // 記錄字元 sb.append(chars[n]); } // 6.畫幹擾線 for (int i = 0; i < LINES; i++) { // 設定隨機顏色 graphic.setColor(getRandomColor()); // 隨機畫線 graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7.返回驗證碼和圖片 return new Object[]{sb.toString(), image}; } /** * 隨機取色 */ public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; } public static void main(String[] args) throws IOException { Object[] objs = createImage(); BufferedImage image = (BufferedImage) objs[1]; OutputStream os = new FileOutputStream("d:/1.png"); ImageIO.write(image, "png", os); os.close(); }}
然後,寫Controller
//產生驗證碼圖片 @RequestMapping("/valicode.do") //對應/user/valicode.do請求 public void valicode(HttpServletResponse response,HttpSession session) throws Exception{ //利用圖片工具產生圖片 //第一個參數是產生的驗證碼,第二個參數是產生的圖片 Object[] objs = ImageUtil.createImage(); //將驗證碼存入Session session.setAttribute("imageCode",objs[0]); //將圖片輸出給瀏覽器 BufferedImage image = (BufferedImage) objs[1]; response.setContentType("image/png"); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); }
結果:
自此,就全部結束了。是不是很簡單呢
SpringMVC中controller返回圖片(轉)