Java tutorial
/* * Copyright 2015 dactiv * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.dactiv.fear.service.web; import com.github.dactiv.universe.captcha.entity.Captcha; import com.github.dactiv.universe.captcha.entity.support.JpegImgCaptchaToken; import com.github.dactiv.universe.captcha.support.HttpSessionCaptchaManager; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; /** * * * * @author maurice */ @Controller public class SystemCommonController { @Value("${ftl.template.path}") private String ftlTemplatePath; @Autowired private HttpSessionCaptchaManager captchaManager; /** * ? freemarker ? * * @param name ??? * * @return ? */ @RequestMapping("get-ftl-template") public ResponseEntity<byte[]> getFtlTemplate(@RequestParam String name, HttpServletRequest request) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String path = request.getSession().getServletContext().getRealPath("") + ftlTemplatePath + name + ".ftl"; byte[] bytes = FileUtils.readFileToByteArray(new File(path)); return new ResponseEntity<>(bytes, headers, HttpStatus.OK); } /** * ????? * * @param token ?? * * @return ?? byte * * @throws IOException */ @RequestMapping("get-captcha") public ResponseEntity<byte[]> getCaptcha(JpegImgCaptchaToken token, HttpSession session) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); captchaManager.setCurrentSession(session); Captcha captcha = captchaManager.create(token); return new ResponseEntity<>(captcha.getStream(), headers, HttpStatus.OK); } }