HttpUtils.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.ydtech.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ydtech.core.page.HttpResult;
  4. import org.springframework.web.context.request.RequestContextHolder;
  5. import org.springframework.web.context.request.ServletRequestAttributes;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * HTTP工具类
  11. *
  12. * @author Louis
  13. * @date Jun 29, 2019
  14. */
  15. public class HttpUtils {
  16. /**
  17. * 获取HttpServletRequest对象
  18. *
  19. * @return
  20. */
  21. public static HttpServletRequest getHttpServletRequest() {
  22. return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  23. }
  24. /**
  25. * 输出信息到浏览器
  26. *
  27. * @param response
  28. * @param message
  29. * @throws IOException
  30. */
  31. public static void write(HttpServletResponse response, Object data) throws IOException {
  32. response.setContentType("application/json; charset=utf-8");
  33. HttpResult result = HttpResult.ok(data);
  34. String json = JSONObject.toJSONString(result);
  35. response.getWriter().print(json);
  36. response.getWriter().flush();
  37. response.getWriter().close();
  38. }
  39. }