|
| 1 | +package com.juvenxu.mvnbook.account.www; |
| 2 | + |
| 3 | +import com.juvenxu.mvnbook.account.service.AccountService; |
| 4 | +import com.juvenxu.mvnbook.account.service.AccountServiceException; |
| 5 | +import org.springframework.context.ApplicationContext; |
| 6 | +import org.springframework.web.context.support.WebApplicationContextUtils; |
| 7 | + |
| 8 | +import javax.servlet.ServletException; |
| 9 | +import javax.servlet.http.HttpServlet; |
| 10 | +import javax.servlet.http.HttpServletRequest; |
| 11 | +import javax.servlet.http.HttpServletResponse; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.OutputStream; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by zhangbin on 16/2/14. |
| 17 | + */ |
| 18 | +public class CaptchaImageServlet extends HttpServlet { |
| 19 | + private ApplicationContext context; |
| 20 | + private static final long serialVersionUID = 5274323889605521606L; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void init() throws ServletException { |
| 24 | + super.init(); |
| 25 | + context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); |
| 26 | + } |
| 27 | + |
| 28 | + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 29 | + String key = request.getParameter("key"); |
| 30 | + if (key == null || key.length() == 0) { |
| 31 | + response.sendError(404, "No Captcha Key Found"); |
| 32 | + } else { |
| 33 | + AccountService service = (AccountService) context.getBean("account-Service"); |
| 34 | + try { |
| 35 | + response.setContentType("image/jpeg"); |
| 36 | + OutputStream out = response.getOutputStream(); |
| 37 | + out.write(service.generateCaptchaImage(key)); |
| 38 | + out.close(); |
| 39 | + } catch (AccountServiceException e) { |
| 40 | + response.sendError(404, e.getMessage()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments