BaseController.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.fdkankan.ucenter.common;
  2. import com.fdkankan.common.util.DateEditor;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.propertyeditors.StringTrimmerEditor;
  5. import org.springframework.data.domain.PageRequest;
  6. import org.springframework.data.domain.Sort;
  7. import org.springframework.data.domain.Sort.Direction;
  8. import org.springframework.util.StringUtils;
  9. import org.springframework.web.bind.WebDataBinder;
  10. import org.springframework.web.bind.annotation.InitBinder;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.*;
  14. import java.util.Date;
  15. public class BaseController {
  16. @Autowired
  17. protected HttpServletRequest request;
  18. @Autowired
  19. protected HttpServletResponse response;
  20. @InitBinder
  21. protected void initBinder(WebDataBinder webDataBinder) {
  22. webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
  23. webDataBinder.registerCustomEditor(Date.class, new DateEditor(true));
  24. }
  25. /**
  26. * 带参重定向
  27. *
  28. * @param path
  29. * @return
  30. */
  31. protected String redirect(String path) {
  32. return "redirect:" + path;
  33. }
  34. /**
  35. * 不带参重定向
  36. *
  37. * @param response
  38. * @param path
  39. * @return
  40. */
  41. protected String redirect(HttpServletResponse response, String path) {
  42. try {
  43. response.sendRedirect(path);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. return null;
  48. }
  49. public static void output(HttpServletResponse resp, File file) {
  50. OutputStream os = null;
  51. BufferedInputStream bis = null;
  52. byte[] buff = new byte[1024];
  53. try {
  54. os = resp.getOutputStream();
  55. bis = new BufferedInputStream(new FileInputStream(file));
  56. int i = 0;
  57. while ((i = bis.read(buff)) != -1) {
  58. os.write(buff, 0, i);
  59. os.flush();
  60. }
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. } finally {
  64. try {
  65. bis.close();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. protected String getToken(){
  72. return request.getHeader("token");
  73. }
  74. }