Other.java -> CommonController.java
This commit is contained in:
@@ -1,190 +1,190 @@
|
|||||||
package cn.celess.blog.controller;
|
package cn.celess.blog.controller;
|
||||||
|
|
||||||
import cn.celess.blog.enmu.ResponseEnum;
|
import cn.celess.blog.enmu.ResponseEnum;
|
||||||
import cn.celess.blog.entity.Response;
|
import cn.celess.blog.entity.Response;
|
||||||
import cn.celess.blog.entity.model.QiniuResponse;
|
import cn.celess.blog.entity.model.QiniuResponse;
|
||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.service.CountService;
|
import cn.celess.blog.service.CountService;
|
||||||
import cn.celess.blog.service.QiniuService;
|
import cn.celess.blog.service.QiniuService;
|
||||||
import cn.celess.blog.util.HttpUtil;
|
import cn.celess.blog.util.HttpUtil;
|
||||||
import cn.celess.blog.util.RedisUtil;
|
import cn.celess.blog.util.RedisUtil;
|
||||||
import cn.celess.blog.util.VeriCodeUtil;
|
import cn.celess.blog.util.VeriCodeUtil;
|
||||||
import net.sf.json.JSONArray;
|
import net.sf.json.JSONArray;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : xiaohai
|
* @author : xiaohai
|
||||||
* @date : 2019/04/02 22:03
|
* @date : 2019/04/02 22:03
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class Other {
|
public class CommonController {
|
||||||
public static final Logger logger = LoggerFactory.getLogger(Object.class);
|
public static final Logger logger = LoggerFactory.getLogger(Object.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
CountService countService;
|
CountService countService;
|
||||||
@Autowired
|
@Autowired
|
||||||
QiniuService qiniuService;
|
QiniuService qiniuService;
|
||||||
@Autowired
|
@Autowired
|
||||||
RedisUtil redisUtil;
|
RedisUtil redisUtil;
|
||||||
@Autowired
|
@Autowired
|
||||||
HttpServletRequest request;
|
HttpServletRequest request;
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/counts")
|
@GetMapping("/counts")
|
||||||
public Response allCount() {
|
public Response allCount() {
|
||||||
Map<String, Long> countMap = new HashMap<>();
|
Map<String, Long> countMap = new HashMap<>();
|
||||||
countMap.put("articleCount", countService.getArticleCount());
|
countMap.put("articleCount", countService.getArticleCount());
|
||||||
countMap.put("commentCount", countService.getCommentCount());
|
countMap.put("commentCount", countService.getCommentCount());
|
||||||
countMap.put("categoryCount", countService.getCategoriesCount());
|
countMap.put("categoryCount", countService.getCategoriesCount());
|
||||||
countMap.put("tagCount", countService.getTagsCount());
|
countMap.put("tagCount", countService.getTagsCount());
|
||||||
countMap.put("visitorCount", countService.getVisitorCount());
|
countMap.put("visitorCount", countService.getVisitorCount());
|
||||||
return Response.success(countMap);
|
return Response.success(countMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取header的全部参数
|
* 获取header的全部参数
|
||||||
*
|
*
|
||||||
* @param request HttpServletRequest
|
* @param request HttpServletRequest
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
@GetMapping("/headerInfo")
|
@GetMapping("/headerInfo")
|
||||||
public Response headerInfo(HttpServletRequest request) {
|
public Response headerInfo(HttpServletRequest request) {
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
Enumeration<String> headerNames = request.getHeaderNames();
|
Enumeration<String> headerNames = request.getHeaderNames();
|
||||||
String str = null;
|
String str = null;
|
||||||
while (headerNames.hasMoreElements()) {
|
while (headerNames.hasMoreElements()) {
|
||||||
str = headerNames.nextElement();
|
str = headerNames.nextElement();
|
||||||
map.put(str, request.getHeader(str));
|
map.put(str, request.getHeader(str));
|
||||||
}
|
}
|
||||||
map.put("sessionID", request.getSession().getId());
|
map.put("sessionID", request.getSession().getId());
|
||||||
map.put("request.getRemoteAddr()", request.getRemoteAddr());
|
map.put("request.getRemoteAddr()", request.getRemoteAddr());
|
||||||
return Response.success(map);
|
return Response.success(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回验证码
|
* 返回验证码
|
||||||
*
|
*
|
||||||
* @param response HttpServletResponse
|
* @param response HttpServletResponse
|
||||||
* @throws IOException IOException
|
* @throws IOException IOException
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/imgCode", produces = MediaType.IMAGE_PNG_VALUE)
|
@GetMapping(value = "/imgCode", produces = MediaType.IMAGE_PNG_VALUE)
|
||||||
public void getImg(HttpServletResponse response) throws IOException {
|
public void getImg(HttpServletResponse response) throws IOException {
|
||||||
Object[] obj = VeriCodeUtil.createImage();
|
Object[] obj = VeriCodeUtil.createImage();
|
||||||
request.getSession().setAttribute("code", obj[0]);
|
request.getSession().setAttribute("code", obj[0]);
|
||||||
//将图片输出给浏览器
|
//将图片输出给浏览器
|
||||||
BufferedImage image = (BufferedImage) obj[1];
|
BufferedImage image = (BufferedImage) obj[1];
|
||||||
response.setContentType("image/png");
|
response.setContentType("image/png");
|
||||||
OutputStream os = response.getOutputStream();
|
OutputStream os = response.getOutputStream();
|
||||||
ImageIO.write(image, "png", os);
|
ImageIO.write(image, "png", os);
|
||||||
os.close();
|
os.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证 验证码的正确性
|
* 验证 验证码的正确性
|
||||||
*
|
*
|
||||||
* @param code 传进来的验证码
|
* @param code 传进来的验证码
|
||||||
* @param request HttpServletRequest
|
* @param request HttpServletRequest
|
||||||
* @return Session中写入验证状态
|
* @return Session中写入验证状态
|
||||||
*/
|
*/
|
||||||
@PostMapping("/verCode")
|
@PostMapping("/verCode")
|
||||||
public Response verCode(@RequestParam("code") String code, HttpServletRequest request) {
|
public Response verCode(@RequestParam("code") String code, HttpServletRequest request) {
|
||||||
request.getSession().setAttribute("verImgCodeStatus", false);
|
request.getSession().setAttribute("verImgCodeStatus", false);
|
||||||
String codeStr = (String) request.getSession().getAttribute("code");
|
String codeStr = (String) request.getSession().getAttribute("code");
|
||||||
if (code == null) {
|
if (code == null) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
if (codeStr == null) {
|
if (codeStr == null) {
|
||||||
throw new MyException(ResponseEnum.IMG_CODE_TIMEOUT);
|
throw new MyException(ResponseEnum.IMG_CODE_TIMEOUT);
|
||||||
}
|
}
|
||||||
code = code.toLowerCase();
|
code = code.toLowerCase();
|
||||||
codeStr = codeStr.toLowerCase();
|
codeStr = codeStr.toLowerCase();
|
||||||
if (code.equals(codeStr)) {
|
if (code.equals(codeStr)) {
|
||||||
request.getSession().removeAttribute("code");
|
request.getSession().removeAttribute("code");
|
||||||
request.getSession().setAttribute("verImgCodeStatus", true);
|
request.getSession().setAttribute("verImgCodeStatus", true);
|
||||||
return Response.success("验证成功");
|
return Response.success("验证成功");
|
||||||
} else {
|
} else {
|
||||||
request.getSession().removeAttribute("code");
|
request.getSession().removeAttribute("code");
|
||||||
return Response.failure("验证失败,请重新获取验证码");
|
return Response.failure("验证失败,请重新获取验证码");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FIXME :: 单张图片多次上传的问题
|
* FIXME :: 单张图片多次上传的问题
|
||||||
* editor.md图片上传的接口
|
* editor.md图片上传的接口
|
||||||
* FUCK !!!
|
* FUCK !!!
|
||||||
*
|
*
|
||||||
* @param file 文件
|
* @param file 文件
|
||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@PostMapping("/imgUpload")
|
@PostMapping("/imgUpload")
|
||||||
public void upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("editormd-image-file") MultipartFile file) throws IOException {
|
public void upload(HttpServletRequest request, HttpServletResponse response, @RequestParam("editormd-image-file") MultipartFile file) throws IOException {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
String uploadTimesStr = redisUtil.get(request.getRemoteAddr() + "-ImgUploadTimes");
|
String uploadTimesStr = redisUtil.get(request.getRemoteAddr() + "-ImgUploadTimes");
|
||||||
int uploadTimes = 0;
|
int uploadTimes = 0;
|
||||||
if (uploadTimesStr != null) {
|
if (uploadTimesStr != null) {
|
||||||
uploadTimes = Integer.parseInt(uploadTimesStr);
|
uploadTimes = Integer.parseInt(uploadTimesStr);
|
||||||
}
|
}
|
||||||
if (uploadTimes == 10) {
|
if (uploadTimes == 10) {
|
||||||
throw new MyException(ResponseEnum.FAILURE.getCode(), "上传次数已达10次,请2小时后在上传");
|
throw new MyException(ResponseEnum.FAILURE.getCode(), "上传次数已达10次,请2小时后在上传");
|
||||||
}
|
}
|
||||||
request.setCharacterEncoding("utf-8");
|
request.setCharacterEncoding("utf-8");
|
||||||
response.setContentType("text/html");
|
response.setContentType("text/html");
|
||||||
if (file.isEmpty()) {
|
if (file.isEmpty()) {
|
||||||
jsonObject.put("success", 0);
|
jsonObject.put("success", 0);
|
||||||
jsonObject.put("message", "上传失败,请选择文件");
|
jsonObject.put("message", "上传失败,请选择文件");
|
||||||
response.getWriter().println(jsonObject.toString());
|
response.getWriter().println(jsonObject.toString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String fileName = file.getOriginalFilename();
|
String fileName = file.getOriginalFilename();
|
||||||
String mime = fileName.substring(fileName.lastIndexOf("."));
|
String mime = fileName.substring(fileName.lastIndexOf("."));
|
||||||
if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) ||
|
if (".png".equals(mime.toLowerCase()) || ".jpg".equals(mime.toLowerCase()) ||
|
||||||
".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) {
|
".jpeg".equals(mime.toLowerCase()) || ".bmp".equals(mime.toLowerCase())) {
|
||||||
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
QiniuResponse qiniuResponse = qiniuService.uploadFile(file.getInputStream(), "img_" + System.currentTimeMillis() + mime);
|
||||||
jsonObject.put("success", 1);
|
jsonObject.put("success", 1);
|
||||||
jsonObject.put("message", "上传成功");
|
jsonObject.put("message", "上传成功");
|
||||||
jsonObject.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
jsonObject.put("url", "http://cdn.celess.cn/" + qiniuResponse.key);
|
||||||
response.getWriter().println(jsonObject.toString());
|
response.getWriter().println(jsonObject.toString());
|
||||||
redisUtil.setEx(request.getRemoteAddr() + "-ImgUploadTimes", uploadTimes + 1 + "", 2, TimeUnit.HOURS);
|
redisUtil.setEx(request.getRemoteAddr() + "-ImgUploadTimes", uploadTimes + 1 + "", 2, TimeUnit.HOURS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
jsonObject.put("success", 0);
|
jsonObject.put("success", 0);
|
||||||
jsonObject.put("message", "上传失败,请上传图片文件");
|
jsonObject.put("message", "上传失败,请上传图片文件");
|
||||||
response.getWriter().println(jsonObject.toString());
|
response.getWriter().println(jsonObject.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/bingPic")
|
@GetMapping("/bingPic")
|
||||||
public Response bingPic() {
|
public Response bingPic() {
|
||||||
|
|
||||||
JSONObject imageObj;
|
JSONObject imageObj;
|
||||||
try {
|
try {
|
||||||
imageObj = JSONObject.fromObject(HttpUtil.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN"));
|
imageObj = JSONObject.fromObject(HttpUtil.get("https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN"));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return Response.failure(null);
|
return Response.failure(null);
|
||||||
}
|
}
|
||||||
JSONArray jsonArray = imageObj.getJSONArray("images");
|
JSONArray jsonArray = imageObj.getJSONArray("images");
|
||||||
String imageName = jsonArray.getJSONObject(0).getString("url");
|
String imageName = jsonArray.getJSONObject(0).getString("url");
|
||||||
return Response.success("https://cn.bing.com" + imageName);
|
return Response.success("https://cn.bing.com" + imageName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user