Controller return picture in SPRINGMVC (go)

Source: Internet
Author: User

This article transferred from: http://blog.csdn.net/u011637069/article/details/51112187

SPRINGMVC the controller by returning Modelandview and then finding the corresponding view through Viewresolver. Can return JSP can return map and so on.

What to do when I do a captcha picture, let me perplexed, use struts2 to return a byte stream in action, and then configure the type of result in the Struts.xml file as stream. But how to deal with it in the SPRINGMVC?

Below I will explain how to generate the captcha image in Springmvc in code:

First, the CAPTCHA image generation tool class:

Imageutil.java

 PackageOrg.qxl.onlinexam.util;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.Random;ImportJavax.imageio.ImageIO; Public Final classImageutil {//Verification Code Character Set    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 '}; //Number of characters    Private Static Final intSIZE = 4; //number of interference lines    Private Static Final intLINES = 5; //width    Private Static Final intWIDTH = 80; //Height    Private Static Final intHEIGHT = 40; //Font Size    Private Static Final intFont_size = 30; /*** Generate random verification code and image * Object[0]: captcha string; * object[1]: Captcha picture. */     Public Staticobject[] CreateImage () {StringBuffer sb=NewStringBuffer (); //1. Create a blank imageBufferedImage image =Newbufferedimage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb); //2. Get the picture brushGraphics graphic =Image.getgraphics (); //3. Set the brush colorGraphic.setcolor (Color.light_gray); //4. Draw a rectangular backgroundGraphic.fillrect (0, 0, WIDTH, HEIGHT); //5. Draw random charactersRandom ran =NewRandom ();  for(inti = 0; I <SIZE; i++) {            //take random character index            intn =Ran.nextint (chars.length); //Set Random ColorsGraphic.setcolor (Getrandomcolor ()); //Set Font sizeGraphic.setfont (NewFont (NULL, Font.Bold +Font.Italic, font_size)); //stooped charactersgraphic.drawstring (Chars[n]+ "", I * width/size, HEIGHT*2/3); //Record characterssb.append (Chars[n]); }        //6. Draw the interference line         for(inti = 0; i < LINES; i++) {            //Set Random ColorsGraphic.setcolor (Getrandomcolor ()); //Random Draw LineGraphic.drawline (Ran.nextint (width), ran.nextint (HEIGHT), ran.nextint (width), ran.next        Int (HEIGHT)); }        //7. Return verification code and pictures        return Newobject[]{sb.tostring (), image}; }    /*** Random Color extraction*/     Public StaticColor Getrandomcolor () {Random ran=NewRandom (); Color Color=NewColor (Ran.nextint (256), Ran.nextint (), Ran.nextint (256)); returncolor; }         Public Static voidMain (string[] args)throwsIOException {object[] objs=createimage (); BufferedImage Image= (bufferedimage) objs[1]; OutputStream OS=NewFileOutputStream ("D:/1.png"); Imageio.write (Image,"PNG", OS);    Os.close (); }}

Then, write the controller

    //generate a Captcha picture@RequestMapping ("/valicode.do")//corresponding/user/valicode.do request     Public voidValicode (HttpServletResponse response,httpsession session)throwsexception{//Create a picture with the picture tool//The first parameter is the generated verification code, the second parameter is the resulting pictureobject[] Objs =Imageutil.createimage (); //Save verification Code to sessionSession.setattribute ("Imagecode", objs[0]); //output a picture to a browserBufferedImage image = (bufferedimage) objs[1]; Response.setcontenttype ("Image/png"); OutputStream OS=Response.getoutputstream (); Imageio.write (Image,"PNG", OS); }

Results:

Since then, it's all over. Isn't it easy?

Controller return picture in SPRINGMVC (go)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.