Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package com.hyeb.back.controller; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.ServletContextAware; import com.hyeb.back.authenticate.CaptchaService; /** * Controller - * * @author SHOP++ Team * @version 3.0 */ @Controller("adminCommonController") @RequestMapping("/admin/common") public class CommonController implements ServletContextAware { @Value("${system.name}") private String systemName; @Value("${system.version}") private String systemVersion; @Value("${system.description}") private String systemDescription; @Value("${system.show_powered}") private Boolean systemShowPowered; @Resource(name = "captchaServiceImpl") private CaptchaService captchaService; /** servletContext */ private ServletContext servletContext; public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } /** * */ @RequestMapping(value = "/main", method = RequestMethod.GET) public String main() { return "/admin/common/main"; } /** * ?? */ @RequestMapping(value = "/captcha", method = RequestMethod.GET) public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception { if (StringUtils.isEmpty(captchaId)) { captchaId = request.getSession().getId(); } String pragma = new StringBuffer().append("PowderBy").toString(); String value = new StringBuffer().append("hyeb").toString(); response.addHeader(pragma, value); response.setHeader("Pragma", "no-cache");//HTTP1.0Pragma ??Pragmano-cache,??????? Pragma response.setHeader("Cache-Control", "no-cache");// HTTP1.1?Cache-Control ???,no-cache????? response.setHeader("Cache-Control", "no-store");//HTTP1.1?Cache-Control ???no-store???? response.setDateHeader("Expires", 0);//Expires?GMT???????? response.setContentType("image/jpeg"); ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); BufferedImage bufferedImage = captchaService.buildImage(captchaId); ImageIO.write(bufferedImage, "jpg", servletOutputStream); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } } /** * ?? */ @RequestMapping("/error") public String error() { return "/admin/common/error"; } /** * ?? */ @RequestMapping("/unauthorized") public String unauthorized(HttpServletRequest request, HttpServletResponse response) { String requestType = request.getHeader("X-Requested-With"); if (requestType != null && requestType.equalsIgnoreCase("XMLHttpRequest")) { response.addHeader("loginStatus", "unauthorized"); try { response.sendError(HttpServletResponse.SC_FORBIDDEN); } catch (IOException e) { e.printStackTrace(); } return null; } return "/admin/common/unauthorized"; } }