BaseController.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.fdkankan.ucenter.common;
  2. import com.fdkankan.common.util.DateEditor;
  3. import com.fdkankan.ucenter.common.constants.NacosProperty;
  4. import com.fdkankan.ucenter.util.DateUserUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  8. import org.springframework.data.domain.PageRequest;
  9. import org.springframework.data.domain.Sort;
  10. import org.springframework.data.domain.Sort.Direction;
  11. import org.springframework.util.StringUtils;
  12. import org.springframework.web.bind.WebDataBinder;
  13. import org.springframework.web.bind.annotation.InitBinder;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.io.*;
  17. import java.util.Date;
  18. @Slf4j
  19. public class BaseController {
  20. @Autowired
  21. protected HttpServletRequest request;
  22. @Autowired
  23. protected HttpServletResponse response;
  24. @InitBinder
  25. protected void initBinder(WebDataBinder webDataBinder) {
  26. webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  27. webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
  28. }
  29. /**
  30. * 带参重定向
  31. *
  32. * @param path
  33. * @return
  34. */
  35. protected String redirect(String path) {
  36. return "redirect:" + path;
  37. }
  38. /**
  39. * 不带参重定向
  40. *
  41. * @param response
  42. * @param path
  43. * @return
  44. */
  45. protected String redirect(HttpServletResponse response, String path) {
  46. try {
  47. response.sendRedirect(path);
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. return null;
  52. }
  53. public static void output(HttpServletResponse resp, File file) {
  54. OutputStream os = null;
  55. BufferedInputStream bis = null;
  56. byte[] buff = new byte[1024];
  57. try {
  58. os = resp.getOutputStream();
  59. bis = new BufferedInputStream(new FileInputStream(file));
  60. int i = 0;
  61. while ((i = bis.read(buff)) != -1) {
  62. os.write(buff, 0, i);
  63. os.flush();
  64. }
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. } finally {
  68. try {
  69. bis.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. protected String getToken(){
  76. return request.getHeader("token");
  77. }
  78. protected String getLang(){
  79. return request.getHeader("lang");
  80. }
  81. protected String getSign(){
  82. return request.getHeader("sign");
  83. }
  84. protected Integer getTimeZone(){
  85. Integer minute = 0;
  86. String timeZone = request.getHeader("timeZone");
  87. try {
  88. if(!StringUtils.isEmpty(timeZone)){
  89. minute = Integer.parseInt(timeZone);
  90. }
  91. }catch (Exception e){
  92. log.error("getClientTime-error:requestHeaderTimeZone:{}",timeZone,e);
  93. }
  94. if(!NacosProperty.activeFile.contains("eur")){
  95. minute += 480;
  96. }
  97. return minute;
  98. }
  99. protected String getClientTime(){
  100. return DateUserUtil.AddMinute(new Date(),getTimeZone());
  101. }
  102. }