Helius
2020-12-15 2914588a65371a3ce43f678cde0a26cd8da26611
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.matrix.system.hive.plugin.util;
 
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
 
/**
 * 后台返回给前台数据处理工具类
 * @author Matrix-J
 * 
 */
public class ResponseUtils {
    public static Log log = LogFactory.getLog(ResponseUtils.class);
    
    /**
     * 
     * 发送json格式数据到页面
     * 过时的方法,不建议使用,在返回值中直接使用
     * @responsebody 可以达到相同的效果
     * @author Matrix-J
     * @param response
     * @param content
     */
    public static void send(HttpServletResponse response, Object result ) {
        
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out = null;
        String content = JSONObject.
                fromObject(result).toString();
        try {
            out = response.getWriter();
            //若发送数据为null 则默认为""
            if(content == null){
                content = "";
            }
            out.write( content);
        }
        catch (IOException e) {
            log.error(e.getLocalizedMessage(), e);
        }
        finally {
            if (out != null)
                out.close();
        }
    }
    
    
    @Deprecated
    public static void socketSend(HttpServletResponse response,String msg ) {
        response.setContentType("text/event-stream");
        PrintWriter out = null;
            try {
                out = response.getWriter();
            } catch (IOException e) {
 
                e.printStackTrace();
            }
            //若发送数据为null 则默认为""
            out.write(msg);
            out.flush();
    }
    
}