forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpUtils.java
More file actions
38 lines (34 loc) · 1.17 KB
/
HttpUtils.java
File metadata and controls
38 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.bruis.springsecurity.utils;
import com.alibaba.fastjson.JSONObject;
import com.bruis.springsecurity.model.HttpResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author LuoHaiYang
*/
public class HttpUtils {
/**
* 获取HttpServletRequest对象
* @return
*/
public static HttpServletRequest getHttpServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
/**
* 输出信息到浏览器
* @param response
* @param data
* @throws IOException
*/
public static void write(HttpServletResponse response, Object data) throws IOException {
response.setContentType("application/json; charset=utf-8");
HttpResult result = HttpResult.ok(data);
String json = JSONObject.toJSONString(result);
response.getWriter().print(json);
response.getWriter().flush();
response.getWriter().close();
}
}